Skip to content

Commit fdfb0be

Browse files
authored
Merge pull request nginx-proxy#480 from jwilder/jw-certs
Fix template error when /etc/nginx/certs does not exist
2 parents adf4573 + fdf93ca commit fdfb0be

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
nginx: nginx
2-
dockergen: docker-gen -watch -only-exposed -notify "nginx -s reload" /app/nginx.tmpl /etc/nginx/conf.d/default.conf
2+
dockergen: docker-gen -watch -notify "nginx -s reload" /app/nginx.tmpl /etc/nginx/conf.d/default.conf

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ The containers being proxied must [expose](https://docs.docker.com/reference/run
1919

2020
Provided your DNS is setup to forward foo.bar.com to the a host running nginx-proxy, the request will be routed to a container with the VIRTUAL_HOST env var set.
2121

22+
### Docker Compose
23+
24+
```yaml
25+
version: '2'
26+
services:
27+
nginx-proxy:
28+
image: jwilder/nginx-proxy
29+
container_name: nginx-proxy
30+
ports:
31+
- "80:80"
32+
volumes:
33+
- /var/run/docker.sock:/tmp/docker.sock:ro
34+
35+
whoami:
36+
image: jwilder/whoami
37+
container_name: whoami
38+
environment:
39+
- VIRTUAL_HOST=whoami.local
40+
```
41+
42+
```shell
43+
$ docker-compose up
44+
$ curl -H "Host: whoami.local" localhost
45+
I''m 5b129ab83266
46+
```
47+
2248
### Multiple Ports
2349

2450
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.

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: '2'
2+
services:
3+
nginx-proxy:
4+
image: jwilder/nginx-proxy
5+
container_name: nginx-proxy
6+
ports:
7+
- "80:80"
8+
volumes:
9+
- /var/run/docker.sock:/tmp/docker.sock:ro
10+
11+
whoami:
12+
image: jwilder/whoami
13+
environment:
14+
- VIRTUAL_HOST=whoami.local
15+

nginx.tmpl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ upstream {{ $host }} {
112112
{{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
113113

114114
{{/* Get the best matching cert by name for the vhost. */}}
115+
{{ $vhostCert := "" }}
116+
{{ if exists "/etc/nginx/certs" }}
115117
{{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
116-
117-
{{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
118-
{{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
119-
{{ $vhostCert := replace $vhostCert ".key" "" -1 }}
118+
{{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
119+
{{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
120+
{{ $vhostCert := replace $vhostCert ".key" "" -1 }}
121+
{{ end }}
120122

121123
{{/* Use the cert specifid on the container or fallback to the best vhost match */}}
122124
{{ $cert := (coalesce $certName $vhostCert) }}

0 commit comments

Comments
 (0)