nginx.tmpl 8.8 KB

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