# ------------------------------------------------------------
# domain.com
# ------------------------------------------------------------

server {

  listen 80;
  listen [::]:80;

  server_name  ~\.?[0-9a-zA-Z]$;

  access_log /data/logs/proxy-host-1_access.log proxy;
  error_log /data/logs/proxy-host-1_error.log warn;

  if ($http_referer ~* /w9deployment/) { 
    rewrite ^/locales/(.*) /w9deployment/locales/$1 break; 
  }

  location / {
      # Proxy!
      include conf.d/include/proxy.conf;
  }

  # proxy for portainer
  location /w9deployment/ {
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-Scheme $scheme;
      proxy_set_header X-Forwarded-Proto  $scheme;
      proxy_set_header X-Forwarded-For    $remote_addr;
      proxy_set_header X-Real-IP		$remote_addr;
      proxy_set_header Accept-Encoding \"\";
      proxy_pass       http://websoft9-deployment:9000/;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $http_connection;
      proxy_http_version 1.1;
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
      if ($request_method = OPTIONS) {
          return 204;
      }
      set $quot_tmp "\"";
      # set $portainer_jwt "${quot_tmp}${arg_portainer_jwt}${quot_tmp}";
      set $portainer_jwt "${quot_tmp}${cookie_portainerJWT}${quot_tmp}";
      sub_filter '</head>' "<script>($portainer_jwt)?window.localStorage.setItem('portainer.JWT', '$portainer_jwt'):null;</script></head>";
      sub_filter_once on;
      sub_filter_types *;
    }

  # proxy for Nginx proxy Manager
  location /w9proxy/ {
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-Scheme $scheme;
      proxy_set_header X-Forwarded-Proto  $scheme;
      proxy_set_header X-Forwarded-For    $remote_addr;
      proxy_set_header X-Real-IP		$remote_addr;
      proxy_pass       http://websoft9-proxy:81;
      rewrite  ^/w9proxy/?(.*)$ /$1 break;
      proxy_http_version 1.1;
      proxy_redirect off;
      proxy_set_header Upgrade $http_upgrade;
      proxy_cache_bypass $http_upgrade;
      proxy_set_header Connection $http_connection;
      proxy_cache_bypass $http_secret_header;
      proxy_set_header Accept-Encoding \"\";
      add_header Pragma "no-cache";
      add_header Cache-Control "no-cache";
      if ($request_method = OPTIONS) {
          return 204;
      }
      sub_filter '</head>' "<script>var tokens='$cookie_nginx_tokens';(tokens)?window.localStorage.setItem('nginx-proxy-manager-tokens', '[{\"t\":\"$cookie_nginx_tokens\",\"n\":\"$cookie_nginx_nikeName\"}]'):null;</script></head>";
      # source changes
      sub_filter 'href="/' 'href="/w9proxy/';
      sub_filter 'src="/' 'src="/w9proxy/';
      sub_filter '/api' '/w9proxy/api';
      sub_filter '/assets' '/w9proxy/assets';
      sub_filter '/js/' '/w9proxy/js/';
      # script changes
      sub_filter 'r.p="/' 'r.p="/w9proxy/';
      sub_filter '"/login' '"/w9proxy/login';
      sub_filter 'case"/logout"' 'case"/w9proxy/logout"';
      sub_filter 'window.location="/"' 'window.location="/w9proxy/"';
      sub_filter 'history.start({pushState:!0})' 'history.start({pushState:!0,root: "/w9proxy/"})';
      sub_filter 'i.history.navigate(e.' 'i.history.navigate(e.replace("/w9proxy","").';
      sub_filter_types *;
      sub_filter_once off;
    }

  # proxy for Gitea
  location /w9git/ {
      proxy_pass http://websoft9-git:3000/;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_hide_header X-Frame-Options;
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
      if ($request_method = OPTIONS) {
          return 204;
      }
    }

  location /apidocs/static {
      proxy_pass       http://websoft9-apphub:8080/static;
    }

    location /apidocs/openapi.json {
      proxy_pass       http://websoft9-apphub:8080/;
    }

  location /apidocs/ {
      proxy_pass       http://websoft9-apphub:8080/redoc;
      sub_filter 'src="/static' 'src="/apidocs/static';
      sub_filter 'spec-url="/' 'spec-url="/apidocs/openapi.json';
    }

  # Custom
  include /data/nginx/custom/server_proxy[.]conf;
}