1
0
mirror of https://github.com/nginx-proxy/nginx-proxy synced 2024-09-16 17:01:51 +02:00

feat: custom default error page (#2430)

* feat: customizable error page

* fix: use regex on catchall root location to fix DEFAULT_ROOT=none test

* docs: custom error pages

* fix: don't use default nginx image error page

* docs: small fix
This commit is contained in:
Nicolas Duchon 2024-05-22 08:23:48 +02:00 committed by GitHub
parent b4c7ea603e
commit fb9c3a646a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 69 additions and 3 deletions

View File

@ -22,7 +22,8 @@ RUN echo -e "\ninclude /etc/nginx/toplevel.conf.d/*.conf;" >> /etc/nginx/nginx.c
&& sed -i -e '/^\}$/{s//\}\nworker_rlimit_nofile 20480;/;:a' -e '$!N;$!ba' -e '}' /etc/nginx/nginx.conf \
&& mkdir -p '/etc/nginx/toplevel.conf.d' \
&& mkdir -p '/etc/nginx/dhparam' \
&& mkdir -p '/etc/nginx/certs'
&& mkdir -p '/etc/nginx/certs' \
&& mkdir -p '/usr/share/nginx/html/errors'
# Install Forego + docker-gen
COPY --from=forego /usr/local/bin/forego /usr/local/bin/forego

View File

@ -19,7 +19,8 @@ RUN echo "\ninclude /etc/nginx/toplevel.conf.d/*.conf;" >> /etc/nginx/nginx.conf
&& sed -i -e '/^\}$/{s//\}\nworker_rlimit_nofile 20480;/;:a' -e '$!N;$!ba' -e '}' /etc/nginx/nginx.conf \
&& mkdir -p '/etc/nginx/toplevel.conf.d' \
&& mkdir -p '/etc/nginx/dhparam' \
&& mkdir -p '/etc/nginx/certs'
&& mkdir -p '/etc/nginx/certs' \
&& mkdir -p '/usr/share/nginx/html/errors'
# Install Forego + docker-gen
COPY --from=forego /usr/local/bin/forego /usr/local/bin/forego

View File

@ -797,6 +797,21 @@ location / {
Per virtual-host `servers_tokens` directive can be configured by passing appropriate value to the `SERVER_TOKENS` environment variable. Please see the [nginx http_core module configuration](https://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens) for more details.
### Custom error page
To override the default error page displayed on 50x errors, mount your custom HTML error page inside the container at `/usr/share/nginx/html/errors/50x.html`:
```console
docker run --detach \
--name nginx-proxy \
--publish 80:80 \
--volume /var/run/docker.sock:/tmp/docker.sock:ro \
--volume /path/to/error.html:/usr/share/nginx/html/errors/50x.html:ro \
nginxproxy/nginx-proxy:1.5
```
Note that this will not replace your own services error pages.
⬆️ [back to table of contents](#table-of-contents)
## TCP and UDP stream

View File

@ -702,7 +702,17 @@ server {
return 444;
}
{{- end }}
return 503;
{{- if (exists "/usr/share/nginx/html/errors/50x.html") }}
error_page 500 502 503 504 /50x.html;
location /50x.html {
root /usr/share/nginx/html/errors;
internal;
}
{{- end }}
location ^~ / {
return 503;
}
}
{{- end }}
{{- end }}

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
<style>
html {
color-scheme: light dark;
}
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Damn, there's some maintenance in progress.</h1>
<p>
Our apologies for this temporary inconvenience. Regular service
performance will be re-established shortly.
</p>
</body>
</html>

View File

@ -0,0 +1,8 @@
import pytest
import re
def test_custom_error_page(docker_compose, nginxproxy):
r = nginxproxy.get("http://unknown.nginx-proxy.tld")
assert r.status_code == 503
assert re.search(r"Damn, there's some maintenance in progress.", r.text)

View File

@ -0,0 +1,8 @@
version: "2"
services:
sut:
image: nginxproxy/nginx-proxy:test
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./50x.html:/usr/share/nginx/html/errors/50x.html:ro