nginx.tmpl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. {{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }}
  2. {{ define "upstream" }}
  3. {{ if .Address }}
  4. {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
  5. {{ if and .Container.Node.ID .Address.HostPort }}
  6. # {{ .Container.Node.Name }}/{{ .Container.Name }}
  7. server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }};
  8. {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
  9. {{ else if .Network }}
  10. # {{ .Container.Name }}
  11. server {{ .Network.IP }}:{{ .Address.Port }};
  12. {{ end }}
  13. {{ else if .Network }}
  14. # {{ .Container.Name }}
  15. server {{ .Network.IP }} down;
  16. {{ end }}
  17. {{ end }}
  18. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  19. # scheme used to connect to this server
  20. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  21. default $http_x_forwarded_proto;
  22. '' $scheme;
  23. }
  24. # If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
  25. # Connection header that may have been passed to this server
  26. map $http_upgrade $proxy_connection {
  27. default upgrade;
  28. '' close;
  29. }
  30. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  31. log_format vhost '$host $remote_addr - $remote_user [$time_local] '
  32. '"$request" $status $body_bytes_sent '
  33. '"$http_referer" "$http_user_agent"';
  34. access_log off;
  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. # Mitigate httpoxy attack (see README for details)
  48. proxy_set_header Proxy "";
  49. {{ end }}
  50. server {
  51. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  52. listen 80;
  53. access_log /var/log/nginx/access.log vhost;
  54. return 503;
  55. }
  56. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  57. server {
  58. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  59. listen 443 ssl http2;
  60. access_log /var/log/nginx/access.log vhost;
  61. return 503;
  62. ssl_certificate /etc/nginx/certs/default.crt;
  63. ssl_certificate_key /etc/nginx/certs/default.key;
  64. }
  65. {{ end }}
  66. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  67. upstream {{ $host }} {
  68. {{ range $container := $containers }}
  69. {{ $addrLen := len $container.Addresses }}
  70. {{ range $knownNetwork := $CurrentContainer.Networks }}
  71. {{ range $containerNetwork := $container.Networks }}
  72. {{ if eq $knownNetwork.Name $containerNetwork.Name }}
  73. ## Can be connect with "{{ $containerNetwork.Name }}" network
  74. {{/* If only 1 port exposed, use that */}}
  75. {{ if eq $addrLen 1 }}
  76. {{ $address := index $container.Addresses 0 }}
  77. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
  78. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
  79. {{ else }}
  80. {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
  81. {{ $address := where $container.Addresses "Port" $port | first }}
  82. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
  83. {{ end }}
  84. {{ end }}
  85. {{ end }}
  86. {{ end }}
  87. {{ end }}
  88. }
  89. {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
  90. {{ $default_server := index (dict $host "" $default_host "default_server") $host }}
  91. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  92. {{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }}
  93. {{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}}
  94. {{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }}
  95. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  96. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  97. {{/* Get the best matching cert by name for the vhost. */}}
  98. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  99. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  100. {{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
  101. {{ $vhostCert := replace $vhostCert ".key" "" -1 }}
  102. {{/* Use the cert specified on the container or fallback to the best vhost match */}}
  103. {{ $cert := (coalesce $certName $vhostCert) }}
  104. {{ $is_https := (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  105. {{ if $is_https }}
  106. {{ if eq $https_method "redirect" }}
  107. server {
  108. server_name {{ $host }};
  109. listen 80 {{ $default_server }};
  110. access_log /var/log/nginx/access.log vhost;
  111. return 301 https://$host$request_uri;
  112. }
  113. {{ end }}
  114. server {
  115. server_name {{ $host }};
  116. listen 443 ssl http2 {{ $default_server }};
  117. access_log /var/log/nginx/access.log vhost;
  118. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  119. 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;
  120. ssl_prefer_server_ciphers on;
  121. ssl_session_timeout 5m;
  122. ssl_session_cache shared:SSL:50m;
  123. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  124. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  125. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  126. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  127. {{ end }}
  128. {{ if (ne $https_method "noredirect") }}
  129. add_header Strict-Transport-Security "max-age=31536000";
  130. {{ end }}
  131. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  132. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  133. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  134. include /etc/nginx/vhost.d/default;
  135. {{ end }}
  136. location / {
  137. proxy_pass {{ trim $proto }}://{{ trim $host }};
  138. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  139. auth_basic "Restricted {{ $host }}";
  140. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  141. {{ end }}
  142. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  143. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  144. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  145. include /etc/nginx/vhost.d/default_location;
  146. {{ end }}
  147. }
  148. }
  149. {{ end }}
  150. {{ if or (not $is_https) (eq $https_method "noredirect") }}
  151. server {
  152. server_name {{ $host }};
  153. listen 80 {{ $default_server }};
  154. access_log /var/log/nginx/access.log vhost;
  155. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  156. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  157. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  158. include /etc/nginx/vhost.d/default;
  159. {{ end }}
  160. location / {
  161. proxy_pass {{ trim $proto }}://{{ trim $host }};
  162. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  163. auth_basic "Restricted {{ $host }}";
  164. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  165. {{ end }}
  166. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  167. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  168. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  169. include /etc/nginx/vhost.d/default_location;
  170. {{ end }}
  171. }
  172. }
  173. {{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  174. server {
  175. server_name {{ $host }};
  176. listen 443 ssl http2 {{ $default_server }};
  177. access_log /var/log/nginx/access.log vhost;
  178. return 500;
  179. ssl_certificate /etc/nginx/certs/default.crt;
  180. ssl_certificate_key /etc/nginx/certs/default.key;
  181. }
  182. {{ end }}
  183. {{ end }}
  184. {{ end }}