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


server {

  listen 80;
  listen [::]:80;

  server_name domain.com;

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

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

  location /portainer/ {
    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-portainer: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}";
    # sub_filter '</head>' "<script>window.localStorage.setItem('portainer.JWT', '$portainer_jwt');</script></head>";
    sub_filter '</head>' "<script>if (!window.localStorage.getItem('portainer.JWT')) { window.localStorage.setItem('portainer.JWT', '$portainer_jwt'); }</script></head>";
    sub_filter_once on;
    sub_filter_types *;
  }

  location /nginxproxymanager/ {
    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-nginxproxymanager:81;
    rewrite  ^/nginxproxymanager/?(.*)$ /$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 token='$arg_Token';(token)?window.localStorage.setItem('nginx-proxy-manager-tokens', '[{\"t\":\"$arg_Token\",\"n\":\"$arg_Name\"}]'):null;</script></head>";
    # source changes
    sub_filter 'href="/' 'href="/nginxproxymanager/';
    sub_filter 'src="/' 'src="/nginxproxymanager/';
    sub_filter '/api' '/nginxproxymanager/api';
    sub_filter '/assets' '/nginxproxymanager/assets';
    sub_filter '/js/' '/nginxproxymanager/js/';
    # script changes
    sub_filter 'r.p="/' 'r.p="/nginxproxymanager/';
    sub_filter '"/login' '"/nginxproxymanager/login';
    sub_filter 'case"/logout"' 'case"/nginxproxymanager/logout"';
    sub_filter 'window.location="/"' 'window.location="/nginxproxymanager/"';
    sub_filter 'history.start({pushState:!0})' 'history.start({pushState:!0,root: "/nginxproxymanager/"})';
    sub_filter 'i.history.navigate(e.' 'i.history.navigate(e.replace("/nginxproxymanager","").';
    sub_filter_types *;
    sub_filter_once off;
  }

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

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