Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: redirect non-GET methods using 308 instead of 301 #1737

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 573,8 @@ The default behavior for the proxy when port 80 and 443 are exposed is as follow
- If the virtual host does not have a usable cert, but `default.crt` and `default.key` exist, those will be used as the virtual host's certificate.
- If the virtual host does not have a usable cert, and `default.crt` and `default.key` do not exist, or if the virtual host is configured not to trust the default certificate, SSL handshake will be rejected (see [Default and Missing Certificate](#default-and-missing-certificate) below).

The redirection from HTTP to HTTPS use by default a [`301`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301) response for every HTTP methods (except `CONNECT` and `TRACE` which are disabled on nginx). If you wish to use a custom redirection response for the `OPTIONS`, `POST`, `PUT`, `PATCH` and `DELETE` HTTP methods, you can either do it globally with the environment variable `NON_GET_REDIRECT` on the proxy container or per virtual host with the `com.github.nginx-proxy.nginx-proxy.non-get-redirect` label on proxied containers. Valid values are [`307`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307) and [`308`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308).

To serve traffic in both SSL and non-SSL modes without redirecting to SSL, you can include the environment variable `HTTPS_METHOD=noredirect` (the default is `HTTPS_METHOD=redirect`). You can also disable the non-SSL site entirely with `HTTPS_METHOD=nohttp`, or disable the HTTPS site with `HTTPS_METHOD=nohttps`. `HTTPS_METHOD` can be specified on each container for which you want to override the default behavior or on the proxy container to set it globally. If `HTTPS_METHOD=noredirect` is used, Strict Transport Security (HSTS) is disabled to prevent HTTPS users from being redirected by the client. If you cannot get to the HTTP site after changing this setting, your browser has probably cached the HSTS policy and is automatically redirecting you back to HTTPS. You will need to clear your browser's HSTS cache or use an incognito window / different browser.

By default, [HTTP Strict Transport Security (HSTS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security) is enabled with `max-age=31536000` for HTTPS sites. You can disable HSTS with the environment variable `HSTS=off` or use a custom HSTS configuration like `HSTS=max-age=31536000; includeSubDomains; preload`.
Expand Down
18 changes: 13 additions & 5 deletions nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 32,7 @@
{{- $_ := set $config "enable_http3" ($globals.Env.ENABLE_HTTP3 | default "false") }}
{{- $_ := set $config "enable_http_on_missing_cert" ($globals.Env.ENABLE_HTTP_ON_MISSING_CERT | default "true") }}
{{- $_ := set $config "https_method" ($globals.Env.HTTPS_METHOD | default "redirect") }}
{{- $_ := set $config "non_get_redirect" ($globals.Env.NON_GET_REDIRECT | default "301") }}
{{- $_ := set $config "default_host" $globals.Env.DEFAULT_HOST }}
{{- $_ := set $config "resolvers" $globals.Env.RESOLVERS }}
{{- /* LOG_JSON is a shorthand that sets logging defaults to JSON format */}}
Expand Down Expand Up @@ -718,8 719,11 @@ proxy_set_header Proxy "";
{{- if and $https_method_disable_http (not $cert_ok) $enable_http_on_missing_cert }}
{{- $https_method = "noredirect" }}
{{- end }}
{{- $non_get_redirect := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.non-get-redirect" | keys | first | default $globals.config.non_get_redirect }}

{{- $http2_enabled := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http2.enable" | keys | first | default $globals.config.enable_http2 | parseBool }}
{{- $http3_enabled := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http3.enable" | keys | first | default $globals.config.enable_http3 | parseBool }}

{{- $acme_http_challenge := groupByKeys $vhost_containers "Env.ACME_HTTP_CHALLENGE_LOCATION" | first | default $globals.config.acme_http_challenge }}
{{- $acme_http_challenge_legacy := eq $acme_http_challenge "legacy" }}
{{- $acme_http_challenge_enabled := false }}
Expand All @@ -746,6 750,7 @@ proxy_set_header Proxy "";
"default" $default
"hsts" $hsts
"https_method" $https_method
"non_get_redirect" $non_get_redirect
"http2_enabled" $http2_enabled
"http3_enabled" $http3_enabled
"is_regexp" $is_regexp
Expand Down Expand Up @@ -886,11 891,14 @@ server {
{{- end }}

location / {
{{- if eq $globals.config.external_https_port "443" }}
return 301 https://$host$request_uri;
{{- else }}
return 301 https://$host:{{ $globals.config.external_https_port }}$request_uri;
{{- end }}
{{- $redirect_uri := "https://$host$request_uri" }}
{{- if ne $globals.config.external_https_port "443" }}
{{- $redirect_uri = printf "https://$host:%s$request_uri" $globals.config.external_https_port }}
{{- end}}
if ($request_method ~ (OPTIONS|POST|PUT|PATCH|DELETE)) {
return {{ $vhost.non_get_redirect }} {{ $redirect_uri }};
}
return 301 {{ $redirect_uri }};
}
}
{{- end }}
Expand Down
38 changes: 38 additions & 0 deletions test/test_ssl/test_redirect_custom.py
Original file line number Diff line number Diff line change
@@ -0,0 1,38 @@
import pytest


@pytest.mark.parametrize("host,http_method,expected_code", [
("nginx-proxy.tld", "GET", 301),
("nginx-proxy.tld", "HEAD", 301),
("nginx-proxy.tld", "POST", 308),
("nginx-proxy.tld", "PUT", 308),
("nginx-proxy.tld", "PATCH", 308),
("nginx-proxy.tld", "DELETE", 308),
("nginx-proxy.tld", "OPTIONS", 308),
("nginx-proxy.tld", "CONNECT", 405),
("nginx-proxy.tld", "TRACE", 405),
("web2.nginx-proxy.tld", "GET", 301),
("web2.nginx-proxy.tld", "HEAD", 301),
("web2.nginx-proxy.tld", "POST", 307),
("web2.nginx-proxy.tld", "PUT", 307),
("web2.nginx-proxy.tld", "PATCH", 307),
("web2.nginx-proxy.tld", "DELETE", 307),
("web2.nginx-proxy.tld", "OPTIONS", 307),
("web2.nginx-proxy.tld", "CONNECT", 405),
("web2.nginx-proxy.tld", "TRACE", 405),
])
def test_custom_redirect_by_method(
docker_compose,
nginxproxy,
host: str,
http_method: str,
expected_code: int,
):
r = nginxproxy.request(
method=http_method,
url=f'http://{host}',
allow_redirects=False,
)
assert r.status_code == expected_code
if expected_code in { 301, 302, 307, 308 }:
assert r.headers['Location'] == f'https://{host}/'
26 changes: 26 additions & 0 deletions test/test_ssl/test_redirect_custom.yml
Original file line number Diff line number Diff line change
@@ -0,0 1,26 @@
services:
web1:
image: web
expose:
- "81"
environment:
WEB_PORTS: "81"
VIRTUAL_HOST: "nginx-proxy.tld"

web2:
image: web
expose:
- "82"
environment:
WEB_PORTS: "82"
VIRTUAL_HOST: "web2.nginx-proxy.tld"
labels:
com.github.nginx-proxy.nginx-proxy.non-get-redirect: 307

sut:
image: nginxproxy/nginx-proxy:test
environment:
NON_GET_REDIRECT: 308
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./certs:/etc/nginx/certs:ro
28 changes: 28 additions & 0 deletions test/test_ssl/test_redirect_default.py
Original file line number Diff line number Diff line change
@@ -0,0 1,28 @@
import pytest


@pytest.mark.parametrize("http_method,expected_code", [
("GET", 301),
("HEAD", 301),
("POST", 301),
("PUT", 301),
("PATCH", 301),
("DELETE", 301),
("OPTIONS", 301),
("CONNECT", 405),
("TRACE", 405),
])
def test_default_redirect_by_method(
docker_compose,
nginxproxy,
http_method: str,
expected_code: int,
):
r = nginxproxy.request(
method=http_method,
url='http://nginx-proxy.tld',
allow_redirects=False,
)
assert r.status_code == expected_code
if expected_code in { 301, 302, 307, 308 }:
assert r.headers['Location'] == 'https://nginx-proxy.tld/'
14 changes: 14 additions & 0 deletions test/test_ssl/test_redirect_default.yml
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@
services:
web1:
image: web
expose:
- "81"
environment:
WEB_PORTS: "81"
VIRTUAL_HOST: "nginx-proxy.tld"

sut:
image: nginxproxy/nginx-proxy:test
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./certs:/etc/nginx/certs:ro
Loading