2
0

nginx.tmpl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. {{ define "upstream" }}
  2. {{ if .Address }}
  3. {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
  4. {{ if and .Container.Node.ID .Address.HostPort }}
  5. # {{ .Container.Node.Name }}/{{ .Container.Name }}
  6. server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }};
  7. {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
  8. {{ else }}
  9. # {{ .Container.Name }}
  10. server {{ .Address.IP }}:{{ .Address.Port }};
  11. {{ end }}
  12. {{ else }}
  13. # {{ .Container.Name }}
  14. server {{ .Container.IP }} down;
  15. {{ end }}
  16. {{ end }}
  17. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  18. # scheme used to connect to this server
  19. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  20. default $http_x_forwarded_proto;
  21. '' $scheme;
  22. }
  23. # If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
  24. # Connection header that may have been passed to this server
  25. map $http_upgrade $proxy_connection {
  26. default upgrade;
  27. '' close;
  28. }
  29. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  30. log_format vhost '$host $remote_addr - $remote_user [$time_local] '
  31. '"$request" $status $body_bytes_sent '
  32. '"$http_referer" "$http_user_agent"';
  33. access_log off;
  34. {{ if (exists "/etc/nginx/proxy.conf") }}
  35. include /etc/nginx/proxy.conf;
  36. {{ else }}
  37. # HTTP 1.1 support
  38. proxy_http_version 1.1;
  39. proxy_buffering off;
  40. proxy_set_header Host $http_host;
  41. proxy_set_header Upgrade $http_upgrade;
  42. proxy_set_header Connection $proxy_connection;
  43. proxy_set_header X-Real-IP $remote_addr;
  44. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  45. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  46. {{ end }}
  47. server {
  48. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  49. listen 80;
  50. access_log /var/log/nginx/access.log vhost;
  51. return 503;
  52. }
  53. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  54. server {
  55. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  56. listen 443 ssl http2;
  57. access_log /var/log/nginx/access.log vhost;
  58. return 503;
  59. ssl_certificate /etc/nginx/certs/default.crt;
  60. ssl_certificate_key /etc/nginx/certs/default.key;
  61. }
  62. {{ end }}
  63. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  64. upstream {{ $host }} {
  65. {{ range $container := $containers }}
  66. {{ $addrLen := len $container.Addresses }}
  67. {{/* If only 1 port exposed, use that */}}
  68. {{ if eq $addrLen 1 }}
  69. {{ $address := index $container.Addresses 0 }}
  70. {{ template "upstream" (dict "Container" $container "Address" $address) }}
  71. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
  72. {{ else }}
  73. {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
  74. {{ $address := where $container.Addresses "Port" $port | first }}
  75. {{ template "upstream" (dict "Container" $container "Address" $address) }}
  76. {{ end }}
  77. {{ end }}
  78. }
  79. {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
  80. {{ $default_server := index (dict $host "" $default_host "default_server") $host }}
  81. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  82. {{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }}
  83. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  84. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  85. {{/* Get the best matching cert by name for the vhost. */}}
  86. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  87. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  88. {{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
  89. {{ $vhostCert := replace $vhostCert ".key" "" -1 }}
  90. {{/* Use the cert specifid on the container or fallback to the best vhost match */}}
  91. {{ $cert := (coalesce $certName $vhostCert) }}
  92. {{ if (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  93. server {
  94. server_name {{ $host }};
  95. listen 80 {{ $default_server }};
  96. access_log /var/log/nginx/access.log vhost;
  97. return 301 https://$host$request_uri;
  98. }
  99. server {
  100. server_name {{ $host }};
  101. listen 443 ssl http2 {{ $default_server }};
  102. access_log /var/log/nginx/access.log vhost;
  103. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  104. 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;
  105. ssl_prefer_server_ciphers on;
  106. ssl_session_timeout 5m;
  107. ssl_session_cache shared:SSL:50m;
  108. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  109. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  110. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  111. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  112. {{ end }}
  113. add_header Strict-Transport-Security "max-age=31536000";
  114. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  115. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  116. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  117. include /etc/nginx/vhost.d/default;
  118. {{ end }}
  119. location / {
  120. proxy_pass {{ $proto }}://{{ $host }};
  121. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  122. auth_basic "Restricted {{ $host }}";
  123. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  124. {{ end }}
  125. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  126. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  127. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  128. include /etc/nginx/vhost.d/default_location;
  129. {{ end }}
  130. }
  131. }
  132. {{ else }}
  133. server {
  134. server_name {{ $host }};
  135. listen 80 {{ $default_server }};
  136. access_log /var/log/nginx/access.log vhost;
  137. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  138. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  139. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  140. include /etc/nginx/vhost.d/default;
  141. {{ end }}
  142. location / {
  143. proxy_pass {{ $proto }}://{{ $host }};
  144. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  145. auth_basic "Restricted {{ $host }}";
  146. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  147. {{ end }}
  148. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  149. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  150. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  151. include /etc/nginx/vhost.d/default_location;
  152. {{ end }}
  153. }
  154. }
  155. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  156. server {
  157. server_name {{ $host }};
  158. listen 443 ssl http2 {{ $default_server }};
  159. access_log /var/log/nginx/access.log vhost;
  160. return 503;
  161. ssl_certificate /etc/nginx/certs/default.crt;
  162. ssl_certificate_key /etc/nginx/certs/default.key;
  163. }
  164. {{ end }}
  165. {{ end }}
  166. {{ end }}