From 5b9e8c4554c7c3d9c086e4678c167201efb84217 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sat, 25 Oct 2014 22:06:54 -0700 Subject: [PATCH 1/5] Move settings that don't differ per container to the top level --- nginx.tmpl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 42599f7..5de8b6c 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -12,6 +12,15 @@ map $http_upgrade $proxy_connection { '' ''; } +# HTTP 1.1 support +proxy_http_version 1.1; +proxy_set_header Host $http_host; +proxy_set_header Upgrade $http_upgrade; +proxy_set_header Connection $proxy_connection; +proxy_set_header X-Real-IP $remote_addr; +proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; +proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto; + server { listen 80 default_server; server_name _; # This is just an invalid value which will never trigger on a real hostname. @@ -64,15 +73,6 @@ server { location / { proxy_pass http://{{ $host }}; - proxy_set_header Host $http_host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto; - - # HTTP 1.1 support - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $proxy_connection; } } {{ end }} From e1bbe8cde098d80c03bf76e8ecd5eee25dfa18d9 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sat, 25 Oct 2014 22:12:03 -0700 Subject: [PATCH 2/5] Raise proxy_buffering statement to http level --- nginx.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nginx.tmpl b/nginx.tmpl index 5de8b6c..fe9ea9d 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -14,6 +14,7 @@ map $http_upgrade $proxy_connection { # HTTP 1.1 support proxy_http_version 1.1; +proxy_buffering off; proxy_set_header Host $http_host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $proxy_connection; @@ -67,7 +68,6 @@ server { gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; server_name {{ $host }}; - proxy_buffering off; error_log /proc/self/fd/2; access_log /proc/self/fd/1; From 3414a02edf438f76b721b785bdec0e7b64808d6d Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sat, 25 Oct 2014 23:04:54 -0700 Subject: [PATCH 3/5] Make template more readable * $value -> $container --- nginx.tmpl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index fe9ea9d..cbe5fe8 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -33,30 +33,30 @@ server { {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} upstream {{ $host }} { -{{ range $index, $value := $containers }} +{{ range $index, $container := $containers }} - {{ $addrLen := len $value.Addresses }} + {{ $addrLen := len $container.Addresses }} {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} - {{ with $address := index $value.Addresses 0 }} - # {{$value.Name}} + {{ with $address := index $container.Addresses 0 }} + # {{$container.Name}} server {{ $address.IP }}:{{ $address.Port }}; {{ end }} {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}} - {{ else if $value.Env.VIRTUAL_PORT }} - {{ range $i, $address := $value.Addresses }} - {{ if eq $address.Port $value.Env.VIRTUAL_PORT }} - # {{$value.Name}} + {{ else if $container.Env.VIRTUAL_PORT }} + {{ range $index, $address := $container.Addresses }} + {{ if eq $address.Port $container.Env.VIRTUAL_PORT }} + # {{$container.Name}} server {{ $address.IP }}:{{ $address.Port }}; {{ end }} {{ end }} {{/* Else default to standard web port 80 */}} {{ else }} - {{ range $i, $address := $value.Addresses }} + {{ range $index, $address := $container.Addresses }} {{ if eq $address.Port "80" }} - # {{$value.Name}} + # {{$container.Name}} server {{ $address.IP }}:{{ $address.Port }}; {{ end }} {{ end }} From a84aee4a842afbf0dc9686fce3bd7304358ac55b Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sun, 2 Nov 2014 05:23:33 -0800 Subject: [PATCH 4/5] Drop unused index variables from range statement --- nginx.tmpl | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index cbe5fe8..fa2a7cb 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -32,9 +32,7 @@ server { {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} upstream {{ $host }} { - -{{ range $index, $container := $containers }} - +{{ range $container := $containers }} {{ $addrLen := len $container.Addresses }} {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} @@ -42,19 +40,17 @@ 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 }} - {{ range $index, $address := $container.Addresses }} + {{ range $address := .Addresses }} {{ if eq $address.Port $container.Env.VIRTUAL_PORT }} # {{$container.Name}} server {{ $address.IP }}:{{ $address.Port }}; {{ end }} {{ end }} - {{/* Else default to standard web port 80 */}} {{ else }} - {{ range $index, $address := $container.Addresses }} + {{ range $address := $container.Addresses }} {{ if eq $address.Port "80" }} # {{$container.Name}} server {{ $address.IP }}:{{ $address.Port }}; From 0306692b31f8cf5f74c511d468e15800dd1f8fc9 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Sun, 2 Nov 2014 05:29:32 -0800 Subject: [PATCH 5/5] Move gzip_types, access_log, and error_log to http --- nginx.tmpl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index fa2a7cb..2f686a3 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -12,6 +12,11 @@ map $http_upgrade $proxy_connection { '' ''; } +gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; + +access_log /proc/self/fd/1; +error_log /proc/self/fd/2; + # HTTP 1.1 support proxy_http_version 1.1; proxy_buffering off; @@ -61,11 +66,7 @@ upstream {{ $host }} { } server { - gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; - server_name {{ $host }}; - error_log /proc/self/fd/2; - access_log /proc/self/fd/1; location / { proxy_pass http://{{ $host }};