nginx.tmpl 9.9 KB

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