1
0
Fork 0
mirror of https://github.com/nginx-proxy/nginx-proxy synced 2024-05-18 05:26:05 +02:00

feat: Add user customizable default root response

This commit is contained in:
Alexander Lieret 2021-07-06 15:26:02 +02:00 committed by Nicolas Duchon
parent 28c73e5b52
commit 9df330e51e
No known key found for this signature in database
GPG Key ID: 91EF7BB1EECB961A
6 changed files with 53 additions and 2 deletions

View File

@ -1,5 +1,5 @@
# setup build arguments for version of dependencies to use
ARG DOCKER_GEN_VERSION=0.7.7
ARG DOCKER_GEN_VERSION=main
ARG FOREGO_VERSION=v0.17.0
# Use a specific version of golang to build both binaries

View File

@ -1,5 +1,5 @@
# setup build arguments for version of dependencies to use
ARG DOCKER_GEN_VERSION=0.7.7
ARG DOCKER_GEN_VERSION=main
ARG FOREGO_VERSION=v0.17.0
# Use a specific version of golang to build both binaries

View File

@ -123,6 +123,16 @@ You can have multiple containers proxied by the same `VIRTUAL_HOST` by adding a
The full request URI will be forwarded to the serving container in the `X-Forwarded-Path` header.
**NOTE**: Your application needs to be able to generate links starting with `VIRTUAL_PATH`. This can be achieved by it being natively on this path or havin an option to prepend this path. The application does not need to expect this path in the request.
#### DEFAULT_ROOT
This environment variable of the nginx proxy container can be used to customize the return error page if no matching path is found. Furthermore it is possible to use anything which is compatible with the `return` statement of nginx.
For example `DEFAUL_ROOT=418` will return a 418 error page instead of the normal 404 one.
Another example is `DEFAULT_ROOT="301 https://github.com/nginx-proxy/nginx-proxy/blob/main/README.md"` which would redirect an invalid request to this documentation.
### Multiple Networks
With the addition of [overlay networking](https://docs.docker.com/engine/userguide/networking/get-started-overlay/) in Docker 1.9, your `nginx-proxy` container may need to connect to backend containers on multiple networks. By default, if you don't pass the `--net` flag when your `nginx-proxy` container is created, it will only be attached to the default `bridge` network. This means that it will not be able to connect to containers on networks other than `bridge`.

View File

@ -5,6 +5,7 @@
{{ $external_https_port := coalesce $.Env.HTTPS_PORT "443" }}
{{ $debug_all := $.Env.DEBUG }}
{{ $sha1_upstream_name := parseBool (coalesce $.Env.SHA1_UPSTREAM_NAME "false") }}
{{ $default_root_response := coalesce $.Env.DEFAULT_ROOT "404" }}
{{ define "ssl_policy" }}
{{ if eq .ssl_policy "Mozilla-Modern" }}
@ -392,6 +393,11 @@ server {
{{ $upstream := printf "%s-%s" $upstream_name $sum }}
{{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "Vhostroot" $vhost_root) }}
{{ end }}
{{ if (not (contains $paths "/")) }}
location / {
return {{ $default_root_response }};
}
{{ end }}
{{ end }}
}
@ -429,6 +435,11 @@ server {
{{ $upstream := printf "%s-%s" $upstream_name $sum }}
{{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "Vhostroot" $vhost_root) }}
{{ end }}
{{ if (not (contains $paths "/")) }}
location / {
return {{ $default_root_response }};
}
{{ end }}
{{ end }}
}

View File

@ -0,0 +1,6 @@
import pytest
def test_default_root_response(docker_compose, nginxproxy):
r = nginxproxy.get("http://nginx-proxy.test/")
assert r.status_code == 418

View File

@ -0,0 +1,24 @@
web1:
image: web
expose:
- "81"
environment:
WEB_PORTS: "81"
VIRTUAL_HOST: "nginx-proxy.test"
VIRTUAL_PATH: "/web1/"
web2:
image: web
expose:
- "82"
environment:
WEB_PORTS: "82"
VIRTUAL_HOST: "nginx-proxy.test"
VIRTUAL_PATH: "/web2/"
sut:
image: nginxproxy/nginx-proxy:test
environment:
DEFAULT_ROOT: 418
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ../lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro