diff --git a/templates/catch-all-server.conf b/templates/catch-all-server.conf index f78a89f..64f6eb1 100644 --- a/templates/catch-all-server.conf +++ b/templates/catch-all-server.conf @@ -1,9 +1,18 @@ # This is the default catch all server, that intercept all requests # not served by others server block declarations. -# We create a map for a valid domains to be redirected. -map $scheme://$host$request_uri $new_domain_uri { - include /etc/nginx/conf.d/redirects[.]map; +## Match redirects, in the following order: +## https://www.example.com/foobar +## www.example.com/test +## /test +map $scheme://$host$request_uri $new_scheme_host_and_uri { + include /etc/nginx/conf.d/redirects[.]map; +} +map $host$request_uri $new_host_and_uri { + include /etc/nginx/conf.d/redirects[.]map; +} +map $request_uri $new_uri { + include /etc/nginx/conf.d/redirects[.]map; } server { @@ -16,7 +25,13 @@ server { #hstsheader # If the URL is a valid redirect URL it will be handled here - if ($new_domain_uri) { + if ($new_scheme_host_and_uri) { + return 301 $new_domain_uri; + } + if ($new_host_and_uri) { + return 301 $new_domain_uri; + } + if ($new_uri) { return 301 $new_domain_uri; } diff --git a/templates/default.conf b/templates/default.conf index f08e0c3..cfd91ca 100644 --- a/templates/default.conf +++ b/templates/default.conf @@ -8,12 +8,16 @@ map $http_x_forwarded_proto $fastcgi_https { https on; } -# We should use $host$request_uri to support redirects when nginx is serving multiple domains. +## Match redirects, in the following order: +## https://www.example.com/foobar +## www.example.com/test +## /test +map $scheme://$host$request_uri $new_scheme_host_and_uri { + include /etc/nginx/conf.d/redirects[.]map; +} map $host$request_uri $new_host_and_uri { include /etc/nginx/conf.d/redirects[.]map; } - -# We should use $request_uri to support redirects with query parameters. map $request_uri $new_uri { include /etc/nginx/conf.d/redirects[.]map; } @@ -23,21 +27,23 @@ include /etc/nginx/conf.d/custom/*.conf; server { server_name ${NGINX_DEFAULT_SERVER_NAME}; - # Please note that nginx always has a default server. - # In the absence of any server block explicitly marked - # as default_server , nginx will use the first server + # Please note that nginx always has a default server. + # In the absence of any server block explicitly marked + # as default_server , nginx will use the first server # with a matching listen directive. # If defaul_server is set, this server will be the # default, catch all server, regardless of server_name. listen ${NGINX_DEFAULT_SERVER_PORT} ${DEFAULT_SERVER}; #hstsheader + if ($new_scheme_host_and_uri) { + return 301 $new_domain_uri; + } if ($new_host_and_uri) { - return 301 $new_host_and_uri; + return 301 $new_domain_uri; } - if ($new_uri) { - return 301 $new_uri; + return 301 $new_domain_uri; } root ${NGINX_DEFAULT_ROOT};