8000 Honor VIRTUAL_PORT + DEBUG flag + fallback entry · nginx-proxy/nginx-proxy@0966b2b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0966b2b

Browse files
committed
Honor VIRTUAL_PORT + DEBUG flag + fallback entry
The VIRTUAL_PORT environment variable should always be honored. Even when the related port is not exposed. Fix for nging-proxy/nginx-proxy#1132. This commit also add the DEBUG environment variable which enables more verbose comments in the nginx comfiguration file to help troubleshooting unreachable containers. Finaly it fixes nging-proxy/nginx-proxy#1105 as well by defining only one fallback entry per upstream block.
1 parent 3afdd9d commit 0966b2b

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

README.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,20 @@ NginX does not support scoped IPv6 resolvers. In [docker-entrypoint.sh](./docker
8282

8383
By default, docker uses IPv6-to-IPv4 NAT. This means all client connections from IPv6 addresses will show docker's internal IPv4 host address. To see true IPv6 client IP addresses, you must [enable IPv6](https://docs.docker.com/config/daemon/ipv6/) and use [ipv6nat](https://github.com/robbertkl/docker-ipv6nat). You must also disable the userland proxy by adding `"userland-proxy": false` to `/etc/docker/daemon.json` and restarting the daemon.
8484

85-
### Multiple Ports
85+
### Multiple Hosts
8686

87-
If your container exposes multiple ports, nginx-proxy will default to the service running on port 80. If you need to specify a different port, you can set a VIRTUAL_PORT env var to select a different one. If your container only exposes one port and it has a VIRTUAL_HOST env var set, that port will be selected.
87+
If you need to support multiple virtual hosts for a container, you can separate each entry with commas. For example, `foo.bar.com,baz.bar.com,bar.com` and each host will be setup the same.
8888

89-
[1]: https://github.com/jwilder/docker-gen
90-
[2]: http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/
89+
### Virtual Ports
9190

92-
### Multiple Hosts
91+
When your container exposes only one port, nginx-proxy will default to this port, else to port 80.
9392

94-
If you need to support multiple virtual hosts for a container, you can separate each entry with commas. For example, `foo.bar.com,baz.bar.com,bar.com` and each host will be setup the same.
93+
If you need to specify a different port, you can set a `VIRTUAL_PORT` env var to select a different one. This variable cannot be set to more than one port.
94+
95+
For each host defined into `VIRTUAL_HOST`, the associated virtual port is retrieved by order of precedence:
96+
1. From the `VIRTUAL_PORT` environment variable
97+
1. From the container's exposed port if there is only one
98+
1. From the default port 80 when none of the above methods apply
9599

96100
### Wildcard Hosts
97101

@@ -424,6 +428,33 @@ will be used on any virtual host which does not have a `/etc/nginx/vhost.d/{VIRT
424428
#### Per-VIRTUAL_HOST `server_tokens` configuration
425429
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.
426430

431+
### Troubleshooting
432+
433+
In case you can't access your VIRTUAL_HOST, set `DEBUG=true` in the client container's environment and have a look at the generated nginx configuration file `/etc/nginx/conf.d/default`:
434+
435+
```
436+
$ docker exec <nginx-proxy-instance> cat /etc/nginx/conf.d/default
437+
```
438+
Especially at `upstream` definition blocks which should look like:
439+
440+
```
441+
# foo.example.com
442+
upstream foo.example.com {
443+
## Can be connected with "my_network" network
444+
# Exposed ports: [{ <exposed_port1> tcp } { <exposed_port2> tcp } ...]
445+
# Default virtual port: <exposed_port|80>
446+
# VIRTUAL_PORT: <VIRTUAL_PORT>
447+
# foo
448+
server 172.18.0.9:<Port>;
449+
# Fallback entry
450+
server 127.0.0.1 down;
451+
}
452+
```
453+
454+
The effective `Port` is retrieved by order of precedence:
455+
1. From the `VIRTUAL_PORT` environment variable
456+
1. From the container's exposed port if there is only one
457+
1. From the default port 80 when none of the above methods apply
427458

428459
### Contributing
429460

nginx.tmpl

+25Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22

33
{{ $external_http_port := coalesce $.Env.HTTP_PORT "80" }}
44
{{ $external_https_port := coalesce $.Env.HTTPS_PORT "443" }}
5+
{{ $debug_all := $.Env.DEBUG }}
56

67
{{ define "upstream" }}
78
{{ if .Address }}
89
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
910
{{ if and .Container.Node.ID .Address.HostPort }}
10-
# {{ .Container.Node.Name }}/{{ .Container.Name }}
11+
# {{ .Container.Node.Name }}/{{ .Container.Name }}
1112
server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }};
1213
{{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
1314
{{ else if .Network }}
14-
# {{ .Container.Name }}
15-
server {{ .Network.IP }}:{{ .Address.Port }};
15+
# {{ .Container.Name }}
16+
server {{ .Network.IP }}:{{ .Address.Port }};
1617
{{ end }}
1718
{{ else if .Network }}
18-
# {{ .Container.Name }}
19+
# {{ .Container.Name }}
1920
{{ if .Network.IP }}
20-
server {{ .Network.IP }} down;
21+
server {{ .Network.IP }}:{{ .VirtualPort }};
2122
{{ else }}
22-
server 127.0.0.1 down;
23+
# /!\ No IP for this network!
2324
{{ end }}
2425
{{ end }}
2526

@@ -180,29 +181,32 @@ server {
180181
upstream {{ $upstream_name }} {
181182

182183
{{ range $container := $containers }}
183-
{{ $addrLen := len $container.Addresses }}
184-
184+
{{ $debug := (eq (coalesce $container.Env.DEBUG $debug_all "false") "true") }}
185+
{{/* If only 1 port exposed, use that as a default, else 80 */}}
186+
{{ $defaultPort := (when (eq (len $container.Addresses) 1) (first $container.Addresses) (dict "Port" "80")).Port }}
185187
{{ range $knownNetwork := $CurrentContainer.Networks }}
186188
{{ range $containerNetwork := $container.Networks }}
187189
{{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }}
188-
## Can be connected with "{{ $containerNetwork.Name }}" network
189-
190-
{{/* If only 1 port exposed, use that */}}
191-
{{ if eq $addrLen 1 }}
192-
{{ $address := index $container.Addresses 0 }}
193-
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
194-
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
195-
{{ else }}
196-
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
197-
{{ $address := where $container.Addresses "Port" $port | first }}
198-
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
190+
## Can be connected with "{{ $containerNetwork.Name }}" network
191+
{{ $port := (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }}
192+
{{ $address := where $container.Addresses "Port" $port | first }}
193+
{{ if $debug }}
194+
# Exposed ports: {{ $container.Addresses }}
195+
# Default virtual port: {{ $defaultPort }}
196+
# VIRTUAL_PORT: {{ $container.Env.VIRTUAL_PORT }}
197+
{{ if not $address }}
198+
# /!\ Virtual port not exposed
199+
{{ end }}
199200
{{ end }}
201+
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork "VirtualPort" $port) }}
200202
{{ else }}
201-
# Cannot connect to network of this container
202-
server 127.0.0.1 down;
203+
# Cannot connect to network '{{ $containerNetwork.Name }}' of this container
203204
{{ end }}
204205
{{ end }}
205206
{{ end }}
207+
{{/* nginx-proxy/nginx-proxy#1105 */}}
208+
# Fallback entry
209+
server 127.0.0.1 down;
206210
{{ end }}
207211
}
208212

0 commit comments

Comments
 (0)
0