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
This commit is contained in:
Charles Wilkinson 2023-03-19 11:09:50 +00:00
parent e9a6b7b2c6
commit 214efca62c

View file

@ -8,7 +8,27 @@
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $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 "_access.conf" %}
{% include "_assets.conf" %} {% include "_assets.conf" %}