|
| 1 | +{{ $CurrentContainer := where $ "Hostname" .Env.HOSTNAME | first }} |
| 2 | + |
| 3 | +# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the |
| 4 | +# scheme used to connect to this server |
| 5 | +map $http_x_forwarded_proto $proxy_x_forwarded_proto { |
| 6 | + default $http_x_forwarded_proto; |
| 7 | + '' $scheme; |
| 8 | +} |
| 9 | + |
| 10 | +# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any |
| 11 | +# Connection header that may have been passed to this server |
| 12 | +map $http_upgrade $proxy_connection { |
| 13 | + default upgrade; |
| 14 | + '' close; |
| 15 | +} |
| 16 | + |
| 17 | +gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; |
| 18 | + |
| 19 | +log_format vhost '$host $remote_addr - $remote_user [$time_local] ' |
| 20 | + '"$request" $status $body_bytes_sent ' |
| 21 | + '"$http_referer" "$http_user_agent"'; |
| 22 | + |
| 23 | +access_log off; |
| 24 | + |
| 25 | +{{ if (exists "/etc/nginx/proxy.conf") }} |
| 26 | +include /etc/nginx/proxy.conf; |
| 27 | +{{ else }} |
| 28 | +# HTTP 1.1 support |
| 29 | +proxy_http_version 1.1; |
| 30 | +proxy_buffering off; |
| 31 | +proxy_set_header Host $http_host; |
| 32 | +proxy_set_header Upgrade $http_upgrade; |
| 33 | +proxy_set_header Connection $proxy_connection; |
| 34 | +proxy_set_header X-Real-IP $remote_addr; |
| 35 | +proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 36 | +proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto; |
| 37 | +{{ end }} |
| 38 | + |
| 39 | +{{ define "server" }} |
| 40 | +{{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}} |
| 41 | +{{ $proto := or (first (groupByKeys .Containers "Env.VIRTUAL_PROTO")) "http" }} |
| 42 | + |
| 43 | +upstream {{ .Host }} { |
| 44 | +{{ range $container := .Containers }} |
| 45 | + {{ $port := coalesce $container.Env.VIRTUAL_PORT (first $container.Addresses).Port "80" }} |
| 46 | + {{ $address := coalesce (first $container.Addresses).IP (first $container.Networks).IP }} |
| 47 | + server {{ $address }}:{{ $port }}; |
| 48 | +{{ end }} |
| 49 | +} |
| 50 | + |
| 51 | +server { |
| 52 | + server_name {{ .Host }}; |
| 53 | + listen 80; |
| 54 | + access_log /var/log/nginx/access.log vhost; |
| 55 | + |
| 56 | + {{ if (exists (printf "/etc/nginx/vhost.d/%s" .Host)) }} |
| 57 | + include {{ printf "/etc/nginx/vhost.d/%s" .Host }}; |
| 58 | + {{ else if (exists "/etc/nginx/vhost.d/default") }} |
| 59 | + include /etc/nginx/vhost.d/default; |
| 60 | + {{ end }} |
| 61 | + |
| 62 | + location / { |
| 63 | + proxy_pass {{ trim $proto }}://{{ trim .Host }}; |
| 64 | + {{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }} |
| 65 | + auth_basic "Restricted {{ .Host }}"; |
| 66 | + auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }}; |
| 67 | + {{ end }} |
| 68 | + {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }} |
| 69 | + include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}}; |
| 70 | + {{ else if (exists "/etc/nginx/vhost.d/default_location") }} |
| 71 | + include /etc/nginx/vhost.d/default_location; |
| 72 | + {{ end }} |
| 73 | + } |
| 74 | +} |
| 75 | +{{ end }} |
| 76 | + |
| 77 | +{{ $explicit := whereExist $ "Env.VIRTUAL_HOST" }} |
| 78 | +{{ range $host, $containers := groupByMulti $explicit "Env.VIRTUAL_HOST" "," }} |
| 79 | + {{ template "server" (dict "Containers" $containers "Host" $host) }} |
| 80 | +{{ end }} |
| 81 | + |
| 82 | +{{ range $project, $projContainers := groupByLabel $ "com.docker.compose.project" }} |
| 83 | + {{ range $service, $containers := groupByLabel $projContainers "com.docker.compose.service" }} |
| 84 | + {{ $container := first $containers }} |
| 85 | + {{ $host := printf "%s.%s.%s" $service $project "docker" }} |
| 86 | + {{ template "server" (dict "Containers" $containers "Host" $host) }} |
| 87 | + {{ end }} |
| 88 | +{{ end }} |
0 commit comments