8000 feat: variable ACME_HTTP_CHALLENGE_LOCATION (#2468) · nginx-proxy/nginx-proxy@9cf736f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9cf736f

Browse files
authored
feat: variable ACME_HTTP_CHALLENGE_LOCATION (#2468)
Values: * `legacy` (default): generate location blocks for ACME HTP Challenge excepted when `HTTPS_METHOD=noredirect` or there is no certificate for the domain * `true`: generate location blocks for ACME HTP Challenge in all cases * `false`: do not generate location blocks for ACME HTP Challenge This feature is currently needed because acme-companion may generate the HTTP Challenge configuration while it was done already by nginx-proxy (see #2465#issuecomment-2136361373). Also sometimes a hardcoded ACME challenge location is not wanted because the challenge validation is not done with acme-companion / Let's Encrypt, and with a challenge location setup differently.
1 parent e904471 commit 9cf736f

12 files changed

+312
-3
lines changed

docs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@ If you are running the container in a virtualized environment (Hyper-V, VirtualB
421421

422422
[acme-companion](https://github.com/nginx-proxy/acme-companion) is a lightweight companion container for the nginx-proxy. It allows the automated creation/renewal of SSL certificates using the ACME protocol.
423423

424+
By default nginx-proxy generates location blocks to handle ACME HTTP Challenge, excepted when `HTTPS_METHOD=noredirect` or there is no certificate for the domain. Ths behavior can be changed with environment variable `ACME_HTTP_CHALLENGE_LOCATION`. It accepts these values:
425+
* `legacy`: default value; current default behavior
426+
* `true`: handle ACME HTTP Challenge in all cases
427+
* `false`: do not handle ACME HTTP Chalenge at all.
428+
424429
### Diffie-Hellman Groups
425430

426431
[RFC7919 groups](https://datatracker.ietf.org/doc/html/rfc7919#appendix-A) with key lengths of 2048, 3072, and 4096 bits are [provided by `nginx-proxy`](https://github.com/nginx-proxy/nginx-proxy/dhparam). The ENV `DHPARAM_BITS` can be set to `2048` or `3072` to change from the default 4096-bit key. The DH key file will be located in the container at `/etc/nginx/dhparam/dhparam.pem`. Mounting a different `dhparam.pem` file at that location will override the RFC7919 key.

nginx.tmpl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,12 @@ proxy_set_header Proxy "";
596596
{{- end }}
597597
{{- $http2_enabled := parseBool (or (first (keys (groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http2.enable"))) $globals.Env.ENABLE_HTTP2 "true")}}
598598
{{- $http3_enabled := parseBool (or (first (keys (groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http3.enable"))) $globals.Env.ENABLE_HTTP3 "false")}}
599+
{{- $acme_http_challenge := or (first (groupByKeys $vhost_containers "Env.ACME_HTTP_CHALLENGE_LOCATION")) $globals.Env.ACME_HTTP_CHALLENGE_LOCATION "legacy" }}
600+
{{- $acme_http_challenge_legacy := eq $acme_http_challenge "legacy" }}
601+
{{- $acme_http_challenge_enabled := false }}
602+
{{- if (not $acme_http_challenge_legacy) }}
603+
{{- $acme_http_challenge_enabled = parseBool $acme_http_challenge }}
604+
{{- end }}
599605

600606
{{- /* Get the SERVER_TOKENS defined by containers w/ the same vhost, falling back to "". */}}
601607
{{- $server_tokens := trim (or (first (groupByKeys $vhost_containers "Env.SERVER_TOKENS")) "") }}
@@ -617,6 +623,8 @@ proxy_set_header Proxy "";
617623
"https_method" $https_method
618624
"http2_enabled" $http2_enabled
619625
"http3_enabled" $http3_enabled
626+
"acme_http_challenge_legacy" $acme_http_challenge_legacy
627+
"acme_http_challenge_enabled" $acme_http_challenge_enabled
620628
"server_tokens" $server_tokens
621629
"ssl_policy" $ssl_policy
622630
"vhost_root" $vhost_root
@@ -741,6 +749,7 @@ server {
741749
listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
742750
{{- end }}
743751

752+
{{- if (or $vhost.acme_http_challenge_legacy $vhost.acme_http_challenge_enabled) }}
744753
# Do not HTTPS redirect Let's Encrypt ACME challenge
745754
location ^~ /.well-known/acme-challenge/ {
746755
auth_basic off;
@@ -750,6 +759,7 @@ server {
750759
try_files $uri =404;
751760
break;
752761
}
762+
{{- end }}
753763

754764
location / {
755765
{{- if eq $globals.external_https_port "443" }}
@@ -776,7 +786,7 @@ server {
776786
listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
777787
{{- end }}
778788

779-
{{- if (eq $vhost.https_method "noredirect") }}
789+
{{- if (and (eq $vhost.https_method "noredirect") $vhost.acme_http_challenge_enabled) }}
780790
location /.well-known/acme-challenge/ {
781791
auth_basic off;
782792
allow all;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
challenge-teststring
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Certificate:
2+
Data:
3+
Version: 3 (0x2)
4+
Serial Number: 4096 (0x1000)
5+
Signature Algorithm: sha256WithRSAEncryption
6+
Issuer: O=nginx-proxy test suite, CN=www.nginx-proxy.tld
7+
Validity
8+
Not Before: Jan 10 00:08:52 2017 GMT
9+
Not After : May 28 00:08:52 2044 GMT
10+
Subject: CN=*.nginx-proxy.tld
11+
Subject Public Key Info:
12+
Public Key Algorithm: rsaEncryption
13+
Public-Key: (2048 bit)
14+
Modulus:
15+
00:cb:45:f4:14:9b:fe:64:85:79:4a:36:8d:3d:d1:
16+
27:d0:7c:36:28:30:e6:73:80:6f:7c:49:23:d0:6c:
17+
17:e4:44:c0:77:4d:9a:c2:bc:24:84:e3:a5:4d:ba:
18+
d2:da:51:7b:a1:2a:12:d4:c0:19:55:69:2c:22:27:
19+
2d:1a:f6:fc:4b:7f:e9:cb:a8:3c:e8:69:b8:d2:4f:
20+
de:4e:50:e2:d0:74:30:7c:42:5a:ae:aa:85:a5:b1:
21+
71:4d:c9:7e:86:8b:62:8c:3e:0d:e3:3b:c3:f5:81:
22+
0b:8c:68:79:fe:bf:10:fb:ae:ec:11:49:6d:64:5e:
23+
1a:7d:b3:92:93:4e:96:19:3a:98:04:a7:66:b2:74:
24+
61:2d:41:13:0c:a4:54:0d:2c:78:fd:b4:a3:e8:37:
25+
78:9a:de:fa:bc:2e:a8:0f:67:14:58:ce:c3:87:d5:
26+
14:0e:8b:29:7d:48:19:b2:a9:f5:b4:e8:af:32:21:
27+
67:15:7e:43:52:8b:20:cf:9f:38:43:bf:fd:c8:24:
28+
7f:52:a3:88:f2:f1:4a:14:91:2a:6e:91:6f:fb:7d:
29+
6a:78:c6:6d:2e:dd:1e:4c:2b:63:bb:3a:43:9c:91:
30+
f9:df:d3:08:13:63:86:7d:ce:e8:46:cf:f1:6c:1f:
31+
ca:f7:4c:de:d8:4b:e0:da:bc:06:d9:87:0f:ff:96:
32+
45:85
33+
Exponent: 65537 (0x10001)
34+
X509v3 extensions:
35+
X509v3 Subject Alternative Name:
36+
DNS:*.nginx-proxy.tld
37+
Signature Algorithm: sha256WithRSAEncryption
38+
6e:a5:0e:e4:d3:cc:d5:b7:fc:34:75:89:4e:98:8c:e7:08:06:
39+
a8:5b:ec:13:7d:83:99:a2:61:b8:d5:12:6e:c5:b4:53:4e:9a:
40+
22:cd:ad:14:30:6a:7d:58:d7:23:d9:a4:2a:96:a0:40:9e:50:
41+
9f:ce:f2:fe:8c:dd:9a:ac:99:39:5b:89:2d:ca:e5:3e:c3:bc:
42+
03:04:1c:12:d9:6e:b8:9f:f0:3a:be:12:44:7e:a4:21:86:73:
43+
af:d5:00:51:3f:2c:56:70:34:8f:26:b0:7f:b0:cf:cf:7f:f9:
44+
40:6f:00:29:c4:cf:c3:b7:c2:49:3d:3f:b0:26:78:87:b9:c7:
45+
6c:1b:aa:6a:1a:dd:c5:eb:f2:69:ba:6d:46:0b:92:49:b5:11:
46+
3c:eb:48:c7:2f:fb:33:a6:6a:82:a2:ab:f8:1e:5f:7d:e3:b7:
47+
f2:fd:f5:88:a5:09:4d:a0:bc:f4:3b:cd:d2:8b:d7:57:1f:86:
48+
3b:d2:3e:a4:92:21:b0:02:0b:e9:e0:c4:1c:f1:78:e2:58:a7:
49+
26:5f:4c:29:c8:23:f0:6e:12:3f:bd:ad:44:7b:0b:bd:db:ba:
50+
63:8d:07:c6:9d:dc:46:cc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TaC89DvN0ovXVx+GO9I+pJIhsAIL6eDEHPF44linJl9MKcgj8G4SP72tRHsL
69+
vdu6Y40Hxp3cRsxjQLpeRYLdmuVQ6OfXJ4j8bx2K51xJKKoQKXUox1Le+Q==
70+
-----END CERTIFICATE-----
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIIEowIBAAKCAQEAy0X0FJv+ZIV5SjaNPdEn0Hw2KDDmc4BvfEkj0GwX5ETAd02a
3+
wrwkhOOlTbrS2lF7oSoS1MAZVWksIictGvb8S3/py6g86Gm40k/eTlDi0HQwfEJa
4+
rqqFpbFxTcl+hotijD4N4zvD9YELjGh5/r8Q+67sEUltZF4afbOSk06WGTqYBKdm
5+
snRhLUETDKRUDSx4/bSj6Dd4mt76vC6oD2cUWM7Dh9UUDospfUgZsqn1tOivMiFn
6+
FX5DUosgz584Q7/9yCR/UqOI8vFKFJEqbpFv+31qeMZtLt0eTCtjuzpDnJH539MI
7+
E2OGfc7oRs/xbB/K90ze2Evg2rwG2YcP/5ZFhQIDAQABAoIBAQCjAro2PNLJMfCO
8+
fyjNRgmzu6iCmpR0U68T8GN0JPsT576g7e8J828l0pkhuIyW33lRSThIvLSUNf9a
9+
dChL032H3lBTLduKVh4NKleQXnVFzaeEPoISSFVdButiAhAhPW4OIUVp0OfY3V+x
10+
fac3j2nDLAfL5SKAtqZv363Py9m66EBYm5BmGTQqT/frQWeCEBvlErQef5RIaU8p
11+
e2zMWgSNNojVai8U3nKNRvYHWeWXM6Ck7lCvkHhMF+RpbmCZuqhbEARVnehU/Jdn
12+
QHJ3nxeA2OWpoWKXvAHtSnno49yxq1UIstiQvY+ng5C5i56UlB60UiU2NJ6doZkB
13+
uQ7/1MaBAoGBAORdcFtgdgRALjXngFWhpCp0CseyUehn1KhxDCG+D1pJ142/ymcf
14+
oJOzKJPMRNDdDUBMnR1GBfy7rmwvYevI/SMNy2Qs7ofcXPbdtwwvTCToZ1V9/54k
15+
VfuPBFT+3QzWRvG1tjTV3E4L2VV3nrl2qNPhE5DlfIaU3nQq5Fl0HprJAoGBAOPf
16+
MWOTGev61CdODO5KN3pLAoamiPs5lEUlz3kM3L1Q52YLITxNDjRj9hWBUATJZOS2
17+
pLOoYRwmhD7vrnimMc41+NuuFX+4T7hWPc8uSuOxX0VijYtULyNRK57mncG1Fq9M
18+
RMLbOJ7FD+8jdXNsSMqpQ+pxLJRX/A10O2fOQnbdAoGAL5hV4YWSM0KZHvz332EI
19+
ER0MXiCJN7HkPZMKH0I4eu3m8hEmAyYxVndBnsQ1F37q0xrkqAQ/HTSUntGlS/og
20+
4Bxw5pkCwegoq/77tpto+ExDtSrEitYx4XMmSPyxX4qNULU5m3tzJgUML+b1etwD
21+
Rd2kMU/TC02dq4KBAy/TbRkCgYAl1xN5iJz+XenLGR/2liZ+TWR+/bqzlU006mF4
22+
pZUmbv/uJxz+yYD5XDwqOA4UrWjuvhG9r9FoflDprp2XdWnB556KxG7XhcDfSJr9
23+
A5/2DadXe1Ur9O/a+oi2228JEsxQkea9QPA3FVxfBtFjOHEiDlez39VaUP4PMeUH
24+
iO3qlQKBgFQhdTb7HeYnApYIDHLmd1PvjRvp8XKR1CpEN0nkw8HpHcT1q1MUjQCr
25+
iT6FQupULEvGmO3frQsgVeRIQDbEdZK3C5xCtn6qOw70sYATVf361BbTtidmU9yV
26+
THFxwDSVLiVZgFryoY/NtAc27sVdJnGsPRjjaeVgALAsLbmZ1K/H
27+
-----END RSA PRIVATE KEY-----
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
4+
def test_redirect_acme_challenge_location_disabled(docker_compose, nginxproxy, acme_challenge_path):
5+
r = nginxproxy.get(
6+
f"http://web1.nginx-proxy.tld/{acme_challenge_path}",
7+
allow_redirects=False
8+
)
9+
assert r.status_code == 301
10+
11+
def test_redirect_acme_challenge_location_enabled(docker_compose, nginxproxy, acme_challenge_path):
12+
r = nginxproxy.get(
13+
f"http://web2.nginx-proxy.tld/{acme_challenge_path}",
14+
allow_redirects=False
15+
)
16+
assert r.status_code == 200
17+
18+
def test_noderirect_acme_challenge_location_disabled(docker_compose, nginxproxy, acme_challenge_path):
19+
r = nginxproxy.get(
20+
f"http://web3.nginx-proxy.tld/{acme_challenge_path}",
21+
allow_redirects=False
22+
)
23+
assert r.status_code == 404
24+
25+
def test_noderirect_acme_challenge_location_enabled(docker_compose, nginxproxy, acme_challenge_path):
26+
r = nginxproxy.get(
27+
f"http://web4.nginx-proxy.tld/{acme_challenge_path}",
28+
allow_redirects=False
29+
)
30+
assert r.status_code == 200
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
version: "2"
2+
3+
services:
4+
web1:
5+
image: web
6+
expose:
7+
- "81"
8+
environment:
9+
WEB_PORTS: "81"
10+
VIRTUAL_HOST: "web1.nginx-proxy.tld"
11+
12+
web2:
13+
image: web
14+
expose:
15+
- "82"
16+
environment:
17+
WEB_PORTS: "82"
18+
VIRTUAL_HOST: "web2.nginx-proxy.tld"
19+
ACME_HTTP_CHALLENGE_LOCATION: "true"
20+
21+
web3:
22+
image: web
23+
expose:
24+
- "83"
25+
environment:
26+
WEB_PORTS: "83"
27+
VIRTUAL_HOST: "web3.nginx-proxy.tld"
28+
HTTPS_METHOD: noredirect
29+
30+
web4:
31+
image: web
32+
expose:
33+
- "84"
34+
environment:
35+
WEB_PORTS: "84"
36+
VIRTUAL_HOST: "web4.nginx-proxy.tld"
37+
HTTPS_METHOD: noredirect
38+
ACME_HTTP_CHALLENGE_LOCATION: "true"
39+
40+
sut:
41+
image: nginxproxy/nginx-proxy:test
42+
environment:
43+
ACME_HTTP_CHALLENGE_LOCATION: "false"
44+
volumes:
45+
- /var/run/docker.sock:/tmp/docker.sock:ro
46+
- ./certs:/etc/nginx/certs:ro
47+
- ./acme_root:/usr/share/nginx/html:ro
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
4+
def test_redirect_acme_challenge_location_enabled(docker_compose, nginxproxy, acme_challenge_path):
5+
r = nginxproxy.get(
6+
f"http://web1.nginx-proxy.tld/{acme_challenge_path}",
7+
allow_redirects=False
8+
)
9+
assert r.status_code == 200
10+
11+
def test_redirect_acme_challenge_location_disabled(docker_compose, nginxproxy, acme_challenge_path):
12+
r = nginxproxy.get(
13+
f"http://web2.nginx-proxy.tld/{acme_challenge_path}",
14+
allow_redirects=False
15+
)
16+
assert r.status_code == 301
17+
18+
def test_noderirect_acme_challenge_location_enabled(docker_compose, nginxproxy, acme_challenge_path):
19+
r = nginxproxy.get(
20+
f"http://web3.nginx-proxy.tld/{acme_challenge_path}",
21+
allow_redirects=False
22+
)
23+
assert r.status_code == 200
24+
25+
def test_noderirect_acme_challenge_location_disabled(docker_compose, nginxproxy, acme_challenge_path):
26+
r = nginxproxy.get(
27+
f"http://web4.nginx-proxy.tld/{acme_challenge_path}",
28+
allow_redirects=False
29+
)
30+
assert r.status_code == 404
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
version: "2"
2+
3+
services:
4+
web1:
5+
image: web
6+
expose:
7+
- "81"
8+
environment:
9+
WEB_PORTS: "81"
10+
VIRTUAL_HOST: "web1.nginx-proxy.tld"
11+
12+
web2:
13+
image: web
14+
expose:
15+
- "82"
16+
environment:
17+
WEB_PORTS: "82"
18+
VIRTUAL_HOST: "web2.nginx-proxy.tld"
19+
ACME_HTTP_CHALLENGE_LOCATION: "false"
20+
21+
web3:
22+
image: web
23+
expose:
24+
- "83"
25+
environment:
26+
WEB_PORTS: "83"
27+
VIRTUAL_HOST: "web3.nginx-proxy.tld"
28+
HTTPS_METHOD: noredirect
29+
30+
web4:
31+
image: web
32+
expose:
33+
- "84"
34+
environment:
35+
WEB_PORTS: "84"
36+
VIRTUAL_HOST: "web4.nginx-proxy.tld"
37+
HTTPS_METHOD: noredirect
38+
ACME_HTTP_CHALLENGE_LOCATION: "false"
39+
40+
sut:
41+
image: nginxproxy/nginx-proxy:test
42+
environment:
43+
ACME_HTTP_CHALLENGE_LOCATION: "true"
44+
volumes:
45+
- /var/run/docker.sock:/tmp/docker.sock:ro
46+
- ./certs:/etc/nginx/certs:ro
47+
- ./acme_root:/usr/share/nginx/html:ro
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
4+
def test_redirect_acme_challenge_location_legacy(docker_compose, nginxproxy, acme_challenge_path):
5+
r = nginxproxy.get(
6+
f"http://web1.nginx-proxy.tld/{acme_challenge_path}",
7+
allow_redirects=False
8+
)
9+
assert r.status_code == 200
10+
11+
def test_noderirect_acme_challenge_location_legacy(docker_compose, nginxproxy, acme_challenge_path):
12+
r = nginxproxy.get(
13+
f"http://web2.nginx-proxy.tld/{acme_challenge_path}",
14+
allow_redirects=False
15+
)
16+
assert r.status_code == 404

0 commit comments

Comments
 (0)
0