nginx.tmpl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. {{ end }}
  48. server {
  49. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  50. listen 80;
  51. access_log /var/log/nginx/access.log vhost;
  52. return 503;
  53. }
  54. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  55. server {
  56. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  57. listen 443 ssl http2;
  58. access_log /var/log/nginx/access.log vhost;
  59. return 503;
  60. ssl_certificate /etc/nginx/certs/default.crt;
  61. ssl_certificate_key /etc/nginx/certs/default.key;
  62. }
  63. {{ end }}
  64. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  65. upstream {{ $host }} {
  66. {{ range $container := $containers }}
  67. {{ $addrLen := len $container.Addresses }}
  68. {{ range $knownNetwork := $CurrentContainer.Networks }}
  69. {{ range $containerNetwork := $container.Networks }}
  70. {{ if eq $knownNetwork.Name $containerNetwork.Name }}
  71. ## Can be connect with "{{ $containerNetwork.Name }}" network
  72. {{/* If only 1 port exposed, use that */}}
  73. {{ if eq $addrLen 1 }}
  74. {{ $address := index $container.Addresses 0 }}
  75. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
  76. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
  77. {{ else }}
  78. {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
  79. {{ $address := where $container.Addresses "Port" $port | first }}
  80. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
  81. {{ end }}
  82. {{ end }}
  83. {{ end }}
  84. {{ end }}
  85. {{ end }}
  86. }
  87. {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
  88. {{ $default_server := index (dict $host "" $default_host "default_server") $host }}
  89. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  90. {{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }}
  91. {{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}}
  92. {{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }}
  93. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  94. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  95. {{/* Get the best matching cert by name for the vhost. */}}
  96. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  97. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  98. {{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
  99. {{ $vhostCert := replace $vhostCert ".key" "" -1 }}
  100. {{/* Use the cert specifid on the container or fallback to the best vhost match */}}
  101. {{ $cert := (coalesce $certName $vhostCert) }}
  102. {{ $is_https := (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  103. {{ if $is_https }}
  104. {{ if eq $https_method "redirect" }}
  105. server {
  106. server_name {{ $host }};
  107. listen 80 {{ $default_server }};
  108. access_log /var/log/nginx/access.log vhost;
  109. return 301 https://$host$request_uri;
  110. }
  111. {{ end }}
  112. server {
  113. server_name {{ $host }};
  114. listen 443 ssl http2 {{ $default_server }};
  115. access_log /var/log/nginx/access.log vhost;
  116. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  117. 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;
  118. ssl_prefer_server_ciphers on;
  119. ssl_session_timeout 5m;
  120. ssl_session_cache shared:SSL:50m;
  121. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  122. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  123. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  124. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  125. {{ end }}
  126. add_header Strict-Transport-Security "max-age=31536000";
  127. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  128. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  129. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  130. include /etc/nginx/vhost.d/default;
  131. {{ end }}
  132. location / {
  133. proxy_pass {{ trim $proto }}://{{ trim $host }};
  134. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  135. auth_basic "Restricted {{ $host }}";
  136. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  137. {{ end }}
  138. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  139. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  140. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  141. include /etc/nginx/vhost.d/default_location;
  142. {{ end }}
  143. }
  144. }
  145. {{ end }}
  146. {{ if or (not $is_https) (eq $https_method "noredirect") }}
  147. server {
  148. server_name {{ $host }};
  149. listen 80 {{ $default_server }};
  150. access_log /var/log/nginx/access.log vhost;
  151. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  152. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  153. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  154. include /etc/nginx/vhost.d/default;
  155. {{ end }}
  156. location / {
  157. proxy_pass {{ trim $proto }}://{{ trim $host }};
  158. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  159. auth_basic "Restricted {{ $host }}";
  160. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  161. {{ end }}
  162. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  163. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  164. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  165. include /etc/nginx/vhost.d/default_location;
  166. {{ end }}
  167. }
  168. }
  169. {{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  170. server {
  171. server_name {{ $host }};
  172. listen 443 ssl http2 {{ $default_server }};
  173. access_log /var/log/nginx/access.log vhost;
  174. return 503;
  175. ssl_certificate /etc/nginx/certs/default.crt;
  176. ssl_certificate_key /etc/nginx/certs/default.key;
  177. }
  178. {{ end }}
  179. {{ end }}
  180. {{ end }}