Skip to content

Commit c582d3e

Browse files
authored
Merge pull request #417 from coreruleset/fix/nginx-x-forwarded-port-docs-and-http-level
fix: add NGINX_PORT_IN_REDIRECT var and document NGINX_X_FORWARDED_PORT
2 parents 6d651ae + f6c6245 commit c582d3e

5 files changed

Lines changed: 8 additions & 6 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ Both nginx and httpd containers now run with an **unprivileged user**. This mean
175175
### Nginx `port_in_redirect` breaking change
176176

177177
> [!WARNING]
178-
> nginx now has [`port_in_redirect`](https://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect) set to `off` in all server blocks.
178+
> [`port_in_redirect`](https://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect) is set to `off` via `NGINX_PORT_IN_REDIRECT` in the `http` block (applies globally, including any custom server blocks you mount).
179179
180180
Previously, nginx's default `port_in_redirect on` caused the internal listening port (e.g., `8080` or `8443`) to be included in redirect `Location` headers (e.g., when nginx adds a trailing slash: `/address``http://example.com:8080/address/`). This broke setups where the container is behind a reverse proxy and the external port differs from the internal port.
181181

182-
With `port_in_redirect off`, nginx omits the port from redirect URLs, so clients follow redirects using the correct external port. **If you relied on the port being included in nginx-generated redirects, you will need to mount a custom `default.conf.template` and re-enable this directive.**
182+
With `port_in_redirect off` (the default), nginx omits the port from redirect URLs, so clients follow redirects using the correct external port. **If you relied on the port being included in nginx-generated redirects, set `NGINX_PORT_IN_REDIRECT=on`.**
183183

184184
### Common ENV Variables
185185

@@ -252,6 +252,8 @@ These variables are common to image variants and will set defaults based on the
252252
| HTTP2 | A string value indicating whether HTTP/2 should be enabled (for all locations) (Allowed values: `on`, `off`. Default: `on`) |
253253
| KEEPALIVE_TIMEOUT | Number of seconds for a keep-alive client connection to stay open on the server side (Default: `60s`) |
254254
| NGINX_ALWAYS_TLS_REDIRECT | A string value indicating if http should redirect to https (Allowed values: `on`, `off`. Default: `off`) |
255+
| NGINX_PORT_IN_REDIRECT | Controls nginx's [`port_in_redirect`](https://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect) directive in the `http` block. When `off`, nginx omits the internal port (e.g., `8080`) from redirect URLs — correct for most reverse-proxy deployments. Set to `on` only if you need nginx to include its listening port in redirects. (Allowed values: `on`, `off`. Default: `off`) |
256+
| NGINX_X_FORWARDED_PORT | A string indicating the port of the initial request, sent as the `X-Forwarded-Port` header to the upstream backend. Can be set to a fixed port (e.g., `443`) when the container is behind a reverse proxy. (Default: `$server_port`) |
255257
| NGINX_X_FORWARDED_PROTO | A string indicating the transfer protocol of the initial request (Default: `$scheme`) |
256258
| PORT | An int value indicating the port where the webserver is listening to | `8080` | We run as unprivileged user. |
257259
| PROXY_SSL_VERIFY_DEPTH | An integer value indicating the verification depth for the client certificate chain (Default: `1`) |

nginx/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ ENV \
183183
MODSEC_UPLOAD_KEEP_FILES=Off \
184184
NGINX_ALWAYS_TLS_REDIRECT=off \
185185
NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx \
186+
NGINX_PORT_IN_REDIRECT=off \
186187
NGINX_X_FORWARDED_PORT='$server_port' \
187188
NGINX_X_FORWARDED_PROTO='$scheme' \
188189
PORT=8080 \

nginx/Dockerfile-alpine

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ ENV \
185185
MODSEC_UPLOAD_KEEP_FILES=Off \
186186
NGINX_ALWAYS_TLS_REDIRECT=off \
187187
NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx \
188+
NGINX_PORT_IN_REDIRECT=off \
188189
NGINX_X_FORWARDED_PORT='$server_port' \
189190
NGINX_X_FORWARDED_PROTO='$scheme' \
190191
PORT=8080 \

nginx/templates/conf.d/default.conf.template

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ server {
1313
server_name ${SERVER_NAME};
1414
set $always_redirect ${NGINX_ALWAYS_TLS_REDIRECT};
1515

16-
port_in_redirect off;
17-
1816
PROXY_SSL_CONFIG
1917

2018
location / {
@@ -40,8 +38,6 @@ server {
4038

4139
server_name ${SERVER_NAME};
4240

43-
port_in_redirect off;
44-
4541
ssl_certificate ${SSL_CERT_FILE};
4642
ssl_certificate_key ${SSL_CERT_KEY_FILE};
4743
ssl_session_timeout 1d;

nginx/templates/nginx.conf.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ http {
1616
sendfile on;
1717
http2 ${HTTP2};
1818

19+
port_in_redirect ${NGINX_PORT_IN_REDIRECT};
20+
1921
resolver RESOLVERS RESOLVER_CONFIG;
2022
include /etc/nginx/conf.d/*.conf;
2123
}

0 commit comments

Comments
 (0)