1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2025-07-01 14:25:46 +00:00

Merge pull request #1737 from junderw/fix-redirect

feat: redirect non-GET methods using 308 instead of 301
This commit is contained in:
Nicolas Duchon
2025-01-18 22:03:27 +01:00
committed by GitHub
6 changed files with 111 additions and 5 deletions

View File

@ -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}/'

View File

@ -0,0 +1,22 @@
services:
nginx-proxy:
environment:
NON_GET_REDIRECT: 308
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

View File

@ -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/'

View File

@ -0,0 +1,8 @@
services:
web1:
image: web
expose:
- "81"
environment:
WEB_PORTS: "81"
VIRTUAL_HOST: "nginx-proxy.tld"