2
0

nginx.tmpl 11 KB

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