From f8ae0a4b0020e3e59a3ba02ad889206d00d85857 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 18 Jan 2023 18:27:35 -0500 Subject: [PATCH] feat: `DEFAULT_ROOT=none` disables the default `location /` block --- README.md | 15 +++++++++++---- nginx.tmpl | 2 +- test/test_default-root-none.py | 8 ++++++++ test/test_default-root-none.yml | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 test/test_default-root-none.py create mode 100644 test/test_default-root-none.yml diff --git a/README.md b/README.md index 82b2dcf..951e2f6 100644 --- a/README.md +++ b/README.md @@ -152,10 +152,17 @@ The filename of the previous example would be `example.tld_8610f6c344b4096614eab 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 `DEFAULT_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. -Nginx variables such as $scheme, $host, and $request_uri can be used. However, care must be taken to make sure the $ signs are escaped properly. -If you want to use `301 $scheme://$host/myapp1$request_uri` you should use: +Exception: If this is set to the string `none`, no default `location /` directive will be generated. This makes it possible for you to provide your own `location /` directive in your [`/etc/nginx/vhost.d/VIRTUAL_HOST`](#per-virtual_host) or [`/etc/nginx/vhost.d/default`](#per-virtual_host-default-configuration) files. + +If unspecified, `DEFAULT_ROOT` defaults to `404`. + +Examples (YAML syntax): + + * `DEFAULT_ROOT: "none"` prevents `nginx-proxy` from generating a default `location /` directive. + * `DEFAULT_ROOT: "418"` returns a 418 error page instead of the normal 404 one. + * `DEFAULT_ROOT: "301 https://github.com/nginx-proxy/nginx-proxy/blob/main/README.md"` redirects the client to this documentation. + +Nginx variables such as `$scheme`, `$host`, and `$request_uri` can be used. However, care must be taken to make sure the `$` signs are escaped properly. For example, if you want to use `301 $scheme://$host/myapp1$request_uri` you should use: * Bash: `DEFAULT_ROOT='301 $scheme://$host/myapp1$request_uri'` * Docker Compose yaml: `- DEFAULT_ROOT: 301 $$scheme://$$host/myapp1$$request_uri` diff --git a/nginx.tmpl b/nginx.tmpl index 2abfb7c..18a2d23 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -448,7 +448,7 @@ server { {{- end }} {{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag) }} {{- end }} - {{- if (not (contains $paths "/")) }} + {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}} location / { return {{ $globals.default_root_response }}; } diff --git a/test/test_default-root-none.py b/test/test_default-root-none.py new file mode 100644 index 0000000..742d87b --- /dev/null +++ b/test/test_default-root-none.py @@ -0,0 +1,8 @@ +import re + + +def test_default_root_none(docker_compose, nginxproxy): + conf = nginxproxy.get_conf().decode() + assert re.search(r"(?m)^\s*location\s+/path\s+\{", conf) + assert not re.search(r"(?m)^\s*location\s+/\s+\{", conf) + diff --git a/test/test_default-root-none.yml b/test/test_default-root-none.yml new file mode 100644 index 0000000..309d2ab --- /dev/null +++ b/test/test_default-root-none.yml @@ -0,0 +1,15 @@ +services: + sut: + image: nginxproxy/nginx-proxy:test + volumes: + - /var/run/docker.sock:/tmp/docker.sock:ro + environment: + DEFAULT_ROOT: none + web: + image: web + expose: + - "80" + environment: + WEB_PORTS: "80" + VIRTUAL_HOST: web.nginx-proxy.test + VIRTUAL_PATH: /path