Skip to content
This repository was archived by the owner on Nov 5, 2022. It is now read-only.

Commit

Permalink
Support paths with VIRTUAL_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
md5 committed Oct 9, 2015
1 parent a81bb96 commit f2ec4b8
Showing 1 changed file with 56 additions and 38 deletions.
94 changes: 56 additions & 38 deletions nginx.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
{{ define "upstream-block" }}
upstream {{ .Host }}{{ .Suffix }} {
{{ range $container := .Containers }}
{{ $addrLen := len $container.Addresses }}
{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
{{ $address := index $container.Addresses 0 }}
{{ template "upstream" (dict "Container" $container "Address" $address) }}
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
{{ else }}
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
{{ $address := where $container.Addresses "Port" $port | first }}
{{ template "upstream" (dict "Container" $container "Address" $address) }}
{{ end }}
{{ end }}
}
{{ end }}

{{ define "upstream" }}
{{ if .Address }}
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
Expand All @@ -15,6 +33,21 @@
{{ end }}
{{ end }}

{{ define "location" }}
location {{ .Path }} {
proxy_pass {{ .Proto }}://{{ .Host }}{{ .Suffix }};
{{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
auth_basic "Restricted {{ .Host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
{{ end }}
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
include {{ printf "/etc/nginx/vhost.d/%s_location" .Host }};
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ end }}

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
Expand Down Expand Up @@ -71,22 +104,17 @@ server {
{{ end }}

{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}

upstream {{ $host }} {
{{ range $container := $containers }}
{{ $addrLen := len $container.Addresses }}
{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
{{ $address := index $container.Addresses 0 }}
{{ template "upstream" (dict "Container" $container "Address" $address) }}
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
{{ else }}
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
{{ $address := where $container.Addresses "Port" $port | first }}
{{ template "upstream" (dict "Container" $container "Address" $address) }}
{{ $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
{{ $pathCount := len $paths }}
{{ if eq $pathCount 0 }}
{{ template "upstream-block" dict "Host" $host "Suffix" "" "Containers" $containers }}
{{ else }}
{{ range $path, $containers := $paths }}
{{ $sum := sha1 $path }}
{{ $suffix := printf "-%s" $sum }}
{{ template "upstream-block" dict "Host" $host "Suffix" $suffix "Containers" $containers }}
{{ end }}
{{ end }}
}

{{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
{{ $default_server := index (dict $host "" $default_host "default_server") $host }}
Expand Down Expand Up @@ -143,18 +171,13 @@ server {
include /etc/nginx/vhost.d/default;
{{ end }}

location / {
proxy_pass {{ $proto }}://{{ $host }};
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
{{ end }}
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ if eq $pathCount 0 }}
{{ template "location" (dict "Path" "/" "Proto" $proto "Host" $host "Suffix" "" ) }}
{{ else }}
{{ range $path, $containers := $paths }}
{{ template "location" (dict "Path" $path "Proto" $proto "Host" $host "Suffix" (printf "-%s" (sha1 $path)) ) }}
{{ end }}
{{ end }}
}
{{ else }}

Expand All @@ -169,18 +192,13 @@ server {
include /etc/nginx/vhost.d/default;
{{ end }}

location / {
proxy_pass {{ $proto }}://{{ $host }};
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
{{ end }}
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ if eq $pathCount 0 }}
{{ template "location" (dict "Path" "/" "Proto" $proto "Host" $host "Suffix" "" ) }}
{{ else }}
{{ range $path, $containers := $paths }}
{{ template "location" (dict "Path" $path "Proto" $proto "Host" $host "Suffix" (printf "-%s" (sha1 $path)) ) }}
{{ end }}
{{ end }}
}

{{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
Expand Down

0 comments on commit f2ec4b8

Please sign in to comment.