server {
    listen %%interface%%:%%port%% default_server;
    include /etc/nginx/includes/server_params.conf;
    include /etc/nginx/includes/proxy_params.conf;

    location / {

      # Proxy pass
      proxy_pass http://localhost:8080/;
      rewrite ^%%ingress_entry%%/(.*)$ /$1 break;

      # Disable buffering
      proxy_buffering off;
      proxy_request_buffering off;
      proxy_cache off;

      # Next three lines allow websockets
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      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;

      # Prevent timeouts
      proxy_read_timeout 86400;
      proxy_send_timeout 86400;
    
      # SSE-specific headers
      add_header Cache-Control no-cache;
      add_header Content-Type text/event-stream;

      # Correct base_url
      proxy_set_header Accept-Encoding "";
      sub_filter_once off;

      # Fix subpath
      sub_filter_types *;
      sub_filter href=\"/ href=\"%%ingress_entry%%/;
      sub_filter src=\"/ src=\"%%ingress_entry%%/;
      sub_filter src=\"'/ src=\"'%%ingress_entry%%/;
      sub_filter hx-get=\"/ hx-get=\"%%ingress_entry%%/;
      sub_filter hx-post=\"/ hx-post=\"%%ingress_entry%%/;
      sub_filter action=\"/ action=\"%%ingress_entry%%/;
      sub_filter EventSource('/ EventSource('%%ingress_entry%%/;
      sub_filter fetch('/ fetch('%%ingress_entry%%/;
      sub_filter `/api/v `%%ingress_entry%%/api/v;
      sub_filter "'/api/v" "'%%ingress_entry%%/api/v";
      sub_filter \"/api/v \"%%ingress_entry%%/api/v;
      sub_filter `/u `%%ingress_entry%%/u;
      sub_filter "'/u" "'%%ingress_entry%%/u";
      sub_filter \"/u \"%%ingress_entry%%/u;
      sub_filter `/asset `%%ingress_entry%%/asset;
      sub_filter "'/asset" "'%%ingress_entry%%/asset";
      sub_filter \"/asset \"%%ingress_entry%%/asset;

      # Fix streaming
      sub_filter window.location.origin} window.location.origin}%%ingress_entry%%;
      error_page 405 =200 $uri;

      # Define date for frontpage
      set $today "";
      if ($time_iso8601 ~ "^(\d{4}-\d{2}-\d{2})") {
          set $today $1;
      }
      sub_filter 'id="datePicker"' 'id="datePicker" value="$today"';

      }
}
