浏览代码

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.
Alexander Lieret 3 年之前
父节点
当前提交
4b85e95824
共有 4 个文件被更改,包括 26 次插入5 次删除
  1. 14 0
      README.md
  2. 7 5
      nginx.tmpl
  3. 3 0
      test/test_virtual-path/test_custom_conf.yml
  4. 2 0
      test/test_virtual-path/test_virtual_paths.yml

+ 14 - 0
README.md

@@ -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.
 
+#### 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
 
 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.

+ 7 - 5
nginx.tmpl

@@ -62,7 +62,7 @@ location {{ .Path }} {
 	{{ else if eq .Proto "grpc" }}
 		grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
 	{{ else }}
-		proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}/;
+		proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
 	{{ end }}
 
 	{{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
@@ -386,12 +386,13 @@ server {
 	{{ end }}
 
 	{{ 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 }}
 		{{ range $path, $container := $paths }}
 			{{ $sum := sha1 $path }}
 			{{ $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 }}
 		{{ if (not (contains $paths "/")) }}
 			location / {
@@ -428,12 +429,13 @@ server {
 	{{ end }}
 
 	{{ 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 }}
 		{{ range $path, $container := $paths }}
 			{{ $sum := sha1 $path }}
 			{{ $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 }}
 		{{ if (not (contains $paths "/")) }}
 			location / {

+ 3 - 0
test/test_virtual-path/test_custom_conf.yml

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

+ 2 - 0
test/test_virtual-path/test_virtual_paths.yml

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