From 88e0295a06361082e589209e3d4bd5df5c2d1ac4 Mon Sep 17 00:00:00 2001 From: Charles Wilkinson Date: Sun, 12 Mar 2023 12:36:42 +0000 Subject: [PATCH 1/5] Add variables for scheme, host & port Declaring and using a variable for the forward_host in the proxy_pass directive means that Nginx will no longer fail to start if that host is down. A 503 will be returned for any requests instead. I have also added variables for the forward_scheme and forward_port elements also as these could be useful variables to have access to for any custom elements users may wish to add. I have not declared a variable for forward_path as it is not mandatory and an empty value would cause the set directive to fail. --- backend/templates/_location.conf | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/templates/_location.conf b/backend/templates/_location.conf index 5a7a6abeb..105641829 100644 --- a/backend/templates/_location.conf +++ b/backend/templates/_location.conf @@ -1,10 +1,14 @@ location {{ path }} { + set $forward_scheme {{ forward_scheme }}; + set $forward_host {{ forward_host }}; + set $forward_port {{ forward_port }}; + 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 {{ forward_scheme }}://{{ forward_host }}:{{ forward_port }}{{ forward_path }}; + proxy_pass $forward_scheme://$forward_host:$forward_port{{ forward_path }}; {% if access_list_id > 0 %} {% if access_list.items.length > 0 %} @@ -39,7 +43,5 @@ proxy_http_version 1.1; {% endif %} - {{ advanced_config }} } - From a0c945a6148b7686fe9d332b233c55b879a5e167 Mon Sep 17 00:00:00 2001 From: Charles Wilkinson Date: Sun, 12 Mar 2023 12:37:55 +0000 Subject: [PATCH 2/5] Add trailing whitespace --- backend/templates/_location.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/templates/_location.conf b/backend/templates/_location.conf index 105641829..9b7eff70c 100644 --- a/backend/templates/_location.conf +++ b/backend/templates/_location.conf @@ -45,3 +45,4 @@ {{ advanced_config }} } + From 58c42a6ce8bdb58f6917918c5ee50a51ca3a2244 Mon Sep 17 00:00:00 2001 From: Charles Wilkinson Date: Sun, 12 Mar 2023 13:42:45 +0000 Subject: [PATCH 3/5] Add UI hints Inform the user of the new variables available to them. --- frontend/js/app/nginx/proxy/location-item.ejs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/frontend/js/app/nginx/proxy/location-item.ejs b/frontend/js/app/nginx/proxy/location-item.ejs index 39445f7be..22e066c13 100644 --- a/frontend/js/app/nginx/proxy/location-item.ejs +++ b/frontend/js/app/nginx/proxy/location-item.ejs @@ -51,6 +51,12 @@
+

<%- i18n('all-hosts', 'advanced-config-var-headline') %>

+
    +
  • $forward_scheme <%- i18n('proxy-hosts', 'forward-scheme') %>
  • +
  • $forward_host <%- i18n('proxy-hosts', 'forward-host') %>
  • +
  • $forward_port <%- i18n('proxy-hosts', 'forward-port') %>
  • +
From 214efca62c236be01a866602561e96e3a78be677 Mon Sep 17 00:00:00 2001 From: Charles Wilkinson Date: Sun, 19 Mar 2023 11:09:50 +0000 Subject: [PATCH 4/5] Fix proper handling of paths and parameters Nginx proxy_pass does not handle paths and parameters in a graceful way when using variables for the host. As such we have to handle it manually. See here: https://dev.to/danielkun/nginx-everything-about-proxypass-2ona#let-nginx-start-even-when-not-all-upstream-hosts-are-available --- backend/templates/_location.conf | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/backend/templates/_location.conf b/backend/templates/_location.conf index 63982f177..086d40eaf 100644 --- a/backend/templates/_location.conf +++ b/backend/templates/_location.conf @@ -8,7 +8,27 @@ 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 $forward_scheme://$forward_host:$forward_port{{ forward_path }}; + + {% assign location_path_end_char = path | slice: -1 %} + {% assign forward_path_end_char = forward_path | slice: -1 %} + {% if location_path_end_char == "/" and forward_path_end_char != "/" %} + if ($request_uri ~ "{{ path }}(.*$)") { + # Location path ends with / but forward path doesn't, so we prefix $path_remainder with a slash. + set $uri_remainder /$1; + } + {% elsif location_path_end_char != "/" and forward_path_end_char == "/" %} + # Location path does not have a trailing / but forward path does, so we make sure to capture $uri_remainder without a leading slash. + if ($request_uri ~ "{{ path }}/(.*$)") { + set $uri_remainder $1; + } + {% else %} + # Either both location path and forward path have a trailing /, or neither do, so we make sure to capture $uri_remainder with a leading slash (if it has one). + if ($request_uri ~ "{{ path }}(.*$)") { + set $uri_remainder $1; + } + {% endif %} + + proxy_pass $forward_scheme://$forward_host:$forward_port{{ forward_path }}$uri_remainder; {% include "_access.conf" %} {% include "_assets.conf" %} From cf3a3bc4b9ae75949d46aa6d3a8b67de2a96f610 Mon Sep 17 00:00:00 2001 From: Charles Wilkinson Date: Sun, 19 Mar 2023 20:30:30 +0000 Subject: [PATCH 5/5] Improve comments for clarity --- backend/templates/_location.conf | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/templates/_location.conf b/backend/templates/_location.conf index 086d40eaf..dec486c4e 100644 --- a/backend/templates/_location.conf +++ b/backend/templates/_location.conf @@ -13,16 +13,21 @@ {% assign forward_path_end_char = forward_path | slice: -1 %} {% if location_path_end_char == "/" and forward_path_end_char != "/" %} if ($request_uri ~ "{{ path }}(.*$)") { - # Location path ends with / but forward path doesn't, so we prefix $path_remainder with a slash. + # Location path ends with / so the regex match will exclude that slash from the match, + # but forward path doesn't, so we must prefix $path_remainder with a slash. set $uri_remainder /$1; } {% elsif location_path_end_char != "/" and forward_path_end_char == "/" %} - # Location path does not have a trailing / but forward path does, so we make sure to capture $uri_remainder without a leading slash. + # Location path does not have a trailing / but forward path does, + # so we make sure to capture $uri_remainder without a leading slash. if ($request_uri ~ "{{ path }}/(.*$)") { set $uri_remainder $1; } {% else %} - # Either both location path and forward path have a trailing /, or neither do, so we make sure to capture $uri_remainder with a leading slash (if it has one). + # Either both location path and forward path have a trailing /, or neither do. + # If both do, then we need to capture $uri_remainder without a leading /, but if neither do, + # then we need to capture $uri_remainder with a leading slash (if it has one - it could just be some GET parameters). + # The code for both scenarios happens to be the same. if ($request_uri ~ "{{ path }}(.*$)") { set $uri_remainder $1; }