1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2025-02-24 09:48:14 +00:00

feat: Replace path stripping with variable

This commit removes the automatic path stripping and replaces it with a
user configurable environment variable. This can be set individually for
each container.
This commit is contained in:
Alexander Lieret 2021-07-06 15:36:06 +02:00 committed by Nicolas Duchon
parent 9df330e51e
commit 4b85e95824
No known key found for this signature in database
GPG Key ID: 91EF7BB1EECB961A
4 changed files with 26 additions and 5 deletions

View File

@ -125,6 +125,20 @@ The full request URI will be forwarded to the serving container in the `X-Forwar
**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. **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.
#### VIRTUAL_DEST
This environment variable can be used to rewrite the `VIRTUAL_PATH` part of the requested URL to proxied application. The default value is empty (off).
Make sure that your settings won't result in the slash missing or being doubled. Both these versions can cause troubles.
If the application runs natively on this sub-path or has a setting to do so, `VIRTUAL_DEST` should not be set or empty.
If the requests are expected to not contain a sub-path and the generated links contain the sub-path, `VIRTUAL_DEST=/` should be used.
```console
$ docker run -d -e VIRTUAL_HOST=example.tld -e VIRTUAL_PATH=/app1/ -e VIRTUAL_DEST=/ --name app1 app
```
In this example, the incoming request `http://example.tld/app1/foo` will be proxied as `http://app1/foo` instead of `http://app1/app1/foo`.
#### DEFAULT_ROOT #### 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. 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.

View File

@ -62,7 +62,7 @@ location {{ .Path }} {
{{ else if eq .Proto "grpc" }} {{ else if eq .Proto "grpc" }}
grpc_pass {{ trim .Proto }}://{{ trim .Upstream }}; grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
{{ else }} {{ else }}
proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}/; proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
{{ end }} {{ end }}
{{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }} {{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
@ -386,12 +386,13 @@ server {
{{ end }} {{ end }}
{{ if eq $nPaths 0 }} {{ if eq $nPaths 0 }}
{{ template "location" (dict "Path" "/" "Proto" $proto "Upstream" $upstream_name "Host" $host "Vhostroot" $vhost_root) }} {{ template "location" (dict "Path" "/" "Proto" $proto "Upstream" $upstream_name "Host" $host "Vhostroot" $vhost_root "Dest" "") }}
{{ else }} {{ else }}
{{ range $path, $container := $paths }} {{ range $path, $container := $paths }}
{{ $sum := sha1 $path }} {{ $sum := sha1 $path }}
{{ $upstream := printf "%s-%s" $upstream_name $sum }} {{ $upstream := printf "%s-%s" $upstream_name $sum }}
{{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "Vhostroot" $vhost_root) }} {{ $dest := (or (first (groupByKeys $container "Env.VIRTUAL_DEST")) "") }}
{{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "Vhostroot" $vhost_root "Dest" $dest) }}
{{ end }} {{ end }}
{{ if (not (contains $paths "/")) }} {{ if (not (contains $paths "/")) }}
location / { location / {
@ -428,12 +429,13 @@ server {
{{ end }} {{ end }}
{{ if eq $nPaths 0 }} {{ if eq $nPaths 0 }}
{{ template "location" (dict "Path" "/" "Proto" $proto "Upstream" $upstream_name "Host" $host "Vhostroot" $vhost_root) }} {{ template "location" (dict "Path" "/" "Proto" $proto "Upstream" $upstream_name "Host" $host "Vhostroot" $vhost_root "Dest" "") }}
{{ else }} {{ else }}
{{ range $path, $container := $paths }} {{ range $path, $container := $paths }}
{{ $sum := sha1 $path }} {{ $sum := sha1 $path }}
{{ $upstream := printf "%s-%s" $upstream_name $sum }} {{ $upstream := printf "%s-%s" $upstream_name $sum }}
{{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "Vhostroot" $vhost_root) }} {{ $dest := (or (first (groupByKeys $container "Env.VIRTUAL_DEST")) "") }}
{{ template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "Vhostroot" $vhost_root "Dest" $dest) }}
{{ end }} {{ end }}
{{ if (not (contains $paths "/")) }} {{ if (not (contains $paths "/")) }}
location / { location / {

View File

@ -6,6 +6,7 @@ web1:
WEB_PORTS: "81" WEB_PORTS: "81"
VIRTUAL_HOST: "nginx-proxy.test" VIRTUAL_HOST: "nginx-proxy.test"
VIRTUAL_PATH: "/web1/" VIRTUAL_PATH: "/web1/"
VIRTUAL_DEST: "/"
web2: web2:
image: web image: web
@ -15,6 +16,8 @@ web2:
WEB_PORTS: "82" WEB_PORTS: "82"
VIRTUAL_HOST: "nginx-proxy.test" VIRTUAL_HOST: "nginx-proxy.test"
VIRTUAL_PATH: "/web2/" VIRTUAL_PATH: "/web2/"
VIRTUAL_DEST: "/"
sut: sut:
image: nginxproxy/nginx-proxy:test image: nginxproxy/nginx-proxy:test
environment: environment:

View File

@ -15,6 +15,7 @@ web1:
WEB_PORTS: "81" WEB_PORTS: "81"
VIRTUAL_HOST: "nginx-proxy.test" VIRTUAL_HOST: "nginx-proxy.test"
VIRTUAL_PATH: "/web1/" VIRTUAL_PATH: "/web1/"
VIRTUAL_DEST: "/"
web2: web2:
image: web image: web
@ -24,6 +25,7 @@ web2:
WEB_PORTS: "82" WEB_PORTS: "82"
VIRTUAL_HOST: "nginx-proxy.test" VIRTUAL_HOST: "nginx-proxy.test"
VIRTUAL_PATH: "/web2/" VIRTUAL_PATH: "/web2/"
VIRTUAL_DEST: "/"
web3: web3:
image: web image: web