nginx.tmpl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 X-Forwarded-Port, pass it through; otherwise, pass along the
  25. # server port the client connected to
  26. map $http_x_forwarded_port $proxy_x_forwarded_port {
  27. default $http_x_forwarded_port;
  28. '' $server_port;
  29. }
  30. # If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
  31. # Connection header that may have been passed to this server
  32. map $http_upgrade $proxy_connection {
  33. default upgrade;
  34. '' close;
  35. }
  36. # Apply fix for very long server names
  37. server_names_hash_bucket_size 128;
  38. # Default dhparam
  39. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  40. # Set appropriate X-Forwarded-Ssl header
  41. map $scheme $proxy_x_forwarded_ssl {
  42. default off;
  43. https on;
  44. }
  45. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  46. log_format vhost '$host $remote_addr - $remote_user [$time_local] '
  47. '"$request" $status $body_bytes_sent '
  48. '"$http_referer" "$http_user_agent"';
  49. access_log off;
  50. {{ if $.Env.RESOLVERS }}
  51. resolver {{ $.Env.RESOLVERS }};
  52. {{ end }}
  53. {{ if (exists "/etc/nginx/proxy.conf") }}
  54. include /etc/nginx/proxy.conf;
  55. {{ else }}
  56. # HTTP 1.1 support
  57. proxy_http_version 1.1;
  58. proxy_buffering off;
  59. proxy_set_header Host $http_host;
  60. proxy_set_header Upgrade $http_upgrade;
  61. proxy_set_header Connection $proxy_connection;
  62. proxy_set_header X-Real-IP $remote_addr;
  63. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  64. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  65. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  66. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  67. # Mitigate httpoxy attack (see README for details)
  68. proxy_set_header Proxy "";
  69. {{ end }}
  70. {{ $enable_ipv6 := eq (or ($.Env.ENABLE_IPV6) "") "true" }}
  71. server {
  72. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  73. listen 80;
  74. {{ if $enable_ipv6 }}
  75. listen [::]:80;
  76. {{ end }}
  77. access_log /var/log/nginx/access.log vhost;
  78. return 503;
  79. }
  80. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  81. server {
  82. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  83. listen 443 ssl http2;
  84. {{ if $enable_ipv6 }}
  85. listen [::]:443 ssl http2;
  86. {{ end }}
  87. access_log /var/log/nginx/access.log vhost;
  88. return 503;
  89. ssl_session_tickets off;
  90. ssl_certificate /etc/nginx/certs/default.crt;
  91. ssl_certificate_key /etc/nginx/certs/default.key;
  92. }
  93. {{ end }}
  94. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  95. {{ $host := trim $host }}
  96. {{ $is_regexp := hasPrefix "~" $host }}
  97. {{ $upstream_name := when $is_regexp (sha1 $host) $host }}
  98. # {{ $host }}
  99. upstream {{ $upstream_name }} {
  100. {{ range $container := $containers }}
  101. {{ $addrLen := len $container.Addresses }}
  102. {{ range $knownNetwork := $CurrentContainer.Networks }}
  103. {{ range $containerNetwork := $container.Networks }}
  104. {{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }}
  105. ## Can be connect with "{{ $containerNetwork.Name }}" network
  106. {{/* If only 1 port exposed, use that */}}
  107. {{ if eq $addrLen 1 }}
  108. {{ $address := index $container.Addresses 0 }}
  109. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
  110. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
  111. {{ else }}
  112. {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
  113. {{ $address := where $container.Addresses "Port" $port | first }}
  114. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
  115. {{ end }}
  116. {{ end }}
  117. {{ end }}
  118. {{ end }}
  119. {{ end }}
  120. }
  121. {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
  122. {{ $default_server := index (dict $host "" $default_host "default_server") $host }}
  123. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  124. {{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
  125. {{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
  126. {{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  127. {{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
  128. {{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  129. {{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}}
  130. {{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }}
  131. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  132. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  133. {{/* Get the best matching cert by name for the vhost. */}}
  134. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  135. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  136. {{ $vhostCert := trimSuffix ".crt" $vhostCert }}
  137. {{ $vhostCert := trimSuffix ".key" $vhostCert }}
  138. {{/* Use the cert specified on the container or fallback to the best vhost match */}}
  139. {{ $cert := (coalesce $certName $vhostCert) }}
  140. {{ $is_https := (and (ne $https_method "nohttps") (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  141. {{ if $is_https }}
  142. {{ if eq $https_method "redirect" }}
  143. server {
  144. server_name {{ $host }};
  145. listen 80 {{ $default_server }};
  146. {{ if $enable_ipv6 }}
  147. listen [::]:80 {{ $default_server }};
  148. {{ end }}
  149. access_log /var/log/nginx/access.log vhost;
  150. return 301 https://$host$request_uri;
  151. }
  152. {{ end }}
  153. server {
  154. server_name {{ $host }};
  155. listen 443 ssl http2 {{ $default_server }};
  156. {{ if $enable_ipv6 }}
  157. listen [::]:443 ssl http2 {{ $default_server }};
  158. {{ end }}
  159. access_log /var/log/nginx/access.log vhost;
  160. {{ if eq $network_tag "internal" }}
  161. # Only allow traffic from internal clients
  162. include /etc/nginx/network_internal.conf;
  163. {{ end }}
  164. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  165. 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:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:!DSS';
  166. ssl_prefer_server_ciphers on;
  167. ssl_session_timeout 5m;
  168. ssl_session_cache shared:SSL:50m;
  169. ssl_session_tickets off;
  170. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  171. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  172. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  173. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  174. {{ end }}
  175. {{ if (exists (printf "/etc/nginx/certs/%s.chain.crt" $cert)) }}
  176. ssl_stapling on;
  177. ssl_stapling_verify on;
  178. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.crt" $cert }};
  179. {{ end }}
  180. {{ if (ne $https_method "noredirect") }}
  181. add_header Strict-Transport-Security "max-age=31536000";
  182. {{ end }}
  183. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  184. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  185. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  186. include /etc/nginx/vhost.d/default;
  187. {{ end }}
  188. location / {
  189. {{ if eq $proto "uwsgi" }}
  190. include uwsgi_params;
  191. uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
  192. {{ else }}
  193. proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
  194. {{ end }}
  195. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  196. auth_basic "Restricted {{ $host }}";
  197. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  198. {{ end }}
  199. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  200. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  201. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  202. include /etc/nginx/vhost.d/default_location;
  203. {{ end }}
  204. }
  205. }
  206. {{ end }}
  207. {{ if or (not $is_https) (eq $https_method "noredirect") }}
  208. server {
  209. server_name {{ $host }};
  210. listen 80 {{ $default_server }};
  211. {{ if $enable_ipv6 }}
  212. listen [::]:80 {{ $default_server }};
  213. {{ end }}
  214. access_log /var/log/nginx/access.log vhost;
  215. {{ if eq $network_tag "internal" }}
  216. # Only allow traffic from internal clients
  217. include /etc/nginx/network_internal.conf;
  218. {{ end }}
  219. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  220. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  221. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  222. include /etc/nginx/vhost.d/default;
  223. {{ end }}
  224. location / {
  225. {{ if eq $proto "uwsgi" }}
  226. include uwsgi_params;
  227. uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
  228. {{ else }}
  229. proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
  230. {{ end }}
  231. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  232. auth_basic "Restricted {{ $host }}";
  233. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  234. {{ end }}
  235. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  236. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  237. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  238. include /etc/nginx/vhost.d/default_location;
  239. {{ end }}
  240. }
  241. }
  242. {{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  243. server {
  244. server_name {{ $host }};
  245. listen 443 ssl http2 {{ $default_server }};
  246. {{ if $enable_ipv6 }}
  247. listen [::]:443 ssl http2 {{ $default_server }};
  248. {{ end }}
  249. access_log /var/log/nginx/access.log vhost;
  250. return 500;
  251. ssl_certificate /etc/nginx/certs/default.crt;
  252. ssl_certificate_key /etc/nginx/certs/default.key;
  253. }
  254. {{ end }}
  255. {{ end }}
  256. {{ end }}