From 65e0dc07058c6650d0cc40b94bff7ef3ef9517eb Mon Sep 17 00:00:00 2001 From: Donal Byrne Date: Wed, 3 Dec 2014 18:13:59 +0100 Subject: [PATCH] Removed VIRTUAL_PORT, now splits the VIRTUAL_HOST env on ':' and sets port to the last element in the returned slice --- README.md | 6 +++++- nginx.tmpl | 10 ++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6a27951..ec52140 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,11 @@ Provided your DNS is setup to forward foo.bar.com to the a host running nginx-pr ### Multiple Ports -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. +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 add VIRTUAL_HOST as :. + + $ docker run -e VIRTUAL_HOST=foo.bar.com:8080 ... + +If your container only exposes one port and it has a VIRTUAL_HOST env var set, that port will be selected. [1]: https://github.com/jwilder/docker-gen [2]: http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/ diff --git a/nginx.tmpl b/nginx.tmpl index 3de1843..0312ecf 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -35,7 +35,9 @@ server { return 503; } -{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} +{{ range $hostfull, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} +{{ $host := first ( split $hostfull ":" ) }} +{{ $port := last ( split $hostfull ":" ) }} upstream {{ $host }} { {{ range $container := $containers }} @@ -46,10 +48,10 @@ upstream {{ $host }} { # {{$container.Name}} server {{ $address.IP }}:{{ $address.Port }}; {{ end }} - {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}} - {{ else if $container.Env.VIRTUAL_PORT }} + {{/* If more than one port exposed, use the one matching after the colon in VIRTUAL_HOST env var */}} + {{ else if $port }} {{ range $address := .Addresses }} - {{ if eq $address.Port $container.Env.VIRTUAL_PORT }} + {{ if eq $address.Port $port }} # {{$container.Name}} server {{ $address.IP }}:{{ $address.Port }}; {{ end }}