metwork-framework / mfserv

metwork/mfserv module
http://metwork-framework.org
BSD 3-Clause "New" or "Revised" License
12 stars 6 forks source link

add support of the nginx port_in_redirect directive #625

Closed matthieumarrast closed 7 months ago

matthieumarrast commented 8 months ago

Problem

NGINX triggers HTTP 301 redirection with the "local" port (i.e 18868 by default) when the HTTP header X-Forwarded-Port is not provided by the upstream server (sometimes a misconfiguration).

Example: http://mywebsite.mycompany.fr/static/image will redirect to http://mywebsite.mycompany.fr:18868/static/image/

Two problems:

Analysis

The user should be able to set the directive port_in_redirect off; to disable this behaviour, also the local listening port is not added in the redirected location.

cf. http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect

Note: we can also use absolute_redirect on/off (http://nginx.org/en/docs/http/ngx_http_core_module.html#absolute_redirect), result is same.

Solution

Add following configuration in the mfserv default config.ini:

[nginx]
# Enables or disables specifying the port in absolute redirects issued by nginx.
# see http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect
# (1 => on, 0 => off)
port_in_redirect=1

and in nginx.conf: {% if MFSERV_NGINX_PORT_IN_REDIRECT == "1" %} port_in_redirect on; {% else %} port_in_redirect off; {% endif %}

matthieumarrast commented 6 months ago

correction: even if X-Forwarded-Port is provided by the upstream server, the local port is used by the http 301 redirection so the port_in_redirect directive is important !