nginx.tmpl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. {{ define "upstream" }}
  2. {{ if .Address }}
  3. {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
  4. {{ if and .Container.Node.ID .Address.HostPort }}
  5. # {{ .Container.Node.Name }}/{{ .Container.Name }}
  6. server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }};
  7. {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
  8. {{ else }}
  9. # {{ .Container.Name }}
  10. server {{ .Address.IP }}:{{ .Address.Port }};
  11. {{ end }}
  12. {{ else }}
  13. # {{ .Container.Name }}
  14. server {{ .Container.IP }} down;
  15. {{ end }}
  16. {{ end }}
  17. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  18. # scheme used to connect to this server
  19. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  20. default $http_x_forwarded_proto;
  21. '' $scheme;
  22. }
  23. # If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
  24. # Connection header that may have been passed to this server
  25. map $http_upgrade $proxy_connection {
  26. default upgrade;
  27. '' close;
  28. }
  29. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  30. log_format vhost '$host $remote_addr - $remote_user [$time_local] '
  31. '"$request" $status $body_bytes_sent '
  32. '"$http_referer" "$http_user_agent"';
  33. access_log /proc/self/fd/1 vhost;
  34. error_log /proc/self/fd/2;
  35. {{ if (exists "/etc/nginx/proxy.conf") }}
  36. include /etc/nginx/proxy.conf;
  37. {{ else }}
  38. # HTTP 1.1 support
  39. proxy_http_version 1.1;
  40. proxy_buffering off;
  41. proxy_set_header Host $http_host;
  42. proxy_set_header Upgrade $http_upgrade;
  43. proxy_set_header Connection $proxy_connection;
  44. proxy_set_header X-Real-IP $remote_addr;
  45. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  46. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  47. {{ end }}
  48. server {
  49. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  50. listen 80;
  51. return 503;
  52. }
  53. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  54. upstream {{ $host }} {
  55. {{ range $container := $containers }}
  56. {{ $addrLen := len $container.Addresses }}
  57. {{/* If only 1 port exposed, use that */}}
  58. {{ if eq $addrLen 1 }}
  59. {{ $address := index $container.Addresses 0 }}
  60. {{ template "upstream" (dict "Container" $container "Address" $address) }}
  61. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
  62. {{ else }}
  63. {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
  64. {{ $address := where $container.Addresses "Port" $port | first }}
  65. {{ template "upstream" (dict "Container" $container "Address" $address) }}
  66. {{ end }}
  67. {{ end }}
  68. }
  69. {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
  70. {{ $default_server := index (dict $host "" $default_host "default_server") $host }}
  71. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  72. {{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }}
  73. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  74. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  75. {{/* Get the best matching cert by name for the vhost. */}}
  76. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  77. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  78. {{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
  79. {{ $vhostCert := replace $vhostCert ".key" "" -1 }}
  80. {{/* Use the cert specifid on the container or fallback to the best vhost match */}}
  81. {{ $cert := (coalesce $certName $vhostCert) }}
  82. {{ if (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  83. server {
  84. server_name {{ $host }};
  85. listen 80 {{ $default_server }};
  86. return 301 https://$host$request_uri;
  87. }
  88. server {
  89. server_name {{ $host }};
  90. listen 443 ssl spdy {{ $default_server }};
  91. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  92. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA;
  93. ssl_prefer_server_ciphers on;
  94. ssl_session_timeout 5m;
  95. ssl_session_cache shared:SSL:50m;
  96. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  97. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  98. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  99. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  100. {{ end }}
  101. add_header Strict-Transport-Security "max-age=31536000";
  102. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  103. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  104. {{ else if (exists /etc/vhost.d/default) }}
  105. include /etc/nginx/vhost.d/default
  106. {{ end }}
  107. location / {
  108. proxy_pass {{ $proto }}://{{ $host }};
  109. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  110. auth_basic "Restricted {{ $host }}";
  111. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  112. {{ end }}
  113. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  114. include {{ printf "/etc/nginx/vhost.d/%s_location"}}
  115. {{ else if (exists /etc/vhost.d/default_location) }}
  116. include /etc/nginx/vhost.d/default_location
  117. {{ end }}
  118. }
  119. }
  120. {{ else }}
  121. server {
  122. server_name {{ $host }};
  123. listen 80 {{ $default_server }};
  124. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  125. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  126. {{ else if (exists /etc/vhost.d/default) }}
  127. include /etc/nginx/vhost.d/default
  128. {{ end }}
  129. location / {
  130. proxy_pass {{ $proto }}://{{ $host }};
  131. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  132. auth_basic "Restricted {{ $host }}";
  133. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  134. {{ end }}
  135. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  136. include {{ printf "/etc/nginx/vhost.d/%s_location"}}
  137. {{ else if (exists /etc/vhost.d/default_location) }}
  138. include /etc/nginx/vhost.d/default_location
  139. {{ end }}
  140. }
  141. }
  142. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  143. server {
  144. server_name {{ $host }};
  145. listen 443 ssl spdy {{ $default_server }};
  146. return 503;
  147. ssl_certificate /etc/nginx/certs/default.crt;
  148. ssl_certificate_key /etc/nginx/certs/default.key;
  149. }
  150. {{ end }}
  151. {{ end }}
  152. {{ end }}