diff --git a/docker/rootfs/etc/nginx/conf.d/include/proxy-variables.conf b/docker/rootfs/etc/nginx/conf.d/include/proxy-variables.conf new file mode 100644 index 000000000..45be6042a --- /dev/null +++ b/docker/rootfs/etc/nginx/conf.d/include/proxy-variables.conf @@ -0,0 +1,34 @@ +# Variables used by conf.d/include/proxy.conf, conf.d/include/assets.conf and the +# generated host configs. nginx.conf includes this file just before the custom +# http_top.conf, so maps there can redefine these variables. For an nginx.conf kept +# from an older release, which has no such include, the startup script loads it +# through conf.d/*.conf instead (prepare/40-dynamic.sh). + +# Forwarded Host: nginx's normalised $host (lowercased, taken from the request line for +# absolute-form requests) plus the client's port, so upstreams that compare Host with +# Origin/Referer on non-standard ports keep working. The port is only carried over when +# the Host header names the same host nginx routed on; otherwise (no Host, absolute-form +# URI naming a different host) it falls back to bare $host. Using $host rather than the +# raw header keeps case variants and mismatched Host values out of upstream requests and +# the asset cache key. +map "$host|$http_host" $forward_host { + "~*^(?[^|]+)\|\k(?:[0-9]+)$" "$host$fwd_port"; + default $host; +} + +# Default upstream scheme +map $host $forward_scheme { + default http; +} + +# Handle upstream X-Forwarded-Proto and X-Forwarded-Scheme header +map $http_x_forwarded_proto $x_forwarded_proto { + "http" "http"; + "https" "https"; + default $scheme; +} +map $http_x_forwarded_scheme $x_forwarded_scheme { + "http" "http"; + "https" "https"; + default $scheme; +} diff --git a/docker/rootfs/etc/nginx/nginx.conf b/docker/rootfs/etc/nginx/nginx.conf index 1b25cb5fc..01c1c681c 100644 --- a/docker/rootfs/etc/nginx/nginx.conf +++ b/docker/rootfs/etc/nginx/nginx.conf @@ -52,37 +52,7 @@ http { # Dynamically generated resolvers file include /etc/nginx/conf.d/include/resolvers[.]conf; - # Forwarded Host: nginx's normalised $host (lowercased, taken from the request line for - # absolute-form requests) plus the client's port, so upstreams that compare Host with - # Origin/Referer on non-standard ports keep working. The port is only carried over when - # the Host header names the same host nginx routed on; otherwise (no Host, absolute-form - # URI naming a different host) it falls back to bare $host. Using $host rather than the - # raw header keeps case variants and mismatched Host values out of upstream requests and - # the asset cache key. - map "$host|$http_host" $forward_host { - "~*^(?[^|]+)\|\k(?:[0-9]+)$" "$host$fwd_port"; - default $host; - } - - # Default upstream scheme - map $host $forward_scheme { - default http; - } - - # Handle upstream X-Forwarded-Proto and X-Forwarded-Scheme header - map $http_x_forwarded_proto $x_forwarded_proto { - "http" "http"; - "https" "https"; - default $scheme; - } - map $http_x_forwarded_scheme $x_forwarded_scheme { - "http" "http"; - "https" "https"; - default $scheme; - } - # Real IP Determination - # Local subnets: set_real_ip_from 10.0.0.0/8; set_real_ip_from 172.16.0.0/12; # Includes Docker subnet @@ -93,6 +63,9 @@ http { real_ip_header X-Real-IP; real_ip_recursive on; + # $forward_host, $forward_scheme and $x_forwarded_*, before http_top.conf so it can redefine them + include /etc/nginx/conf.d/include/proxy-variables.conf; + # Custom include /data/nginx/custom/http_top[.]conf;