nginx.tmpl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 if .Network }}
  9. # {{ .Container.Name }}
  10. server {{ .Network.IP }}:{{ .Address.Port }};
  11. {{ end }}
  12. {{ else if .Network }}
  13. # {{ .Container.Name }}
  14. server {{ .Network.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. {{ $network := index $container.Networks 0 }}
  68. {{/* If only 1 port exposed, use that */}}
  69. {{ if eq $addrLen 1 }}
  70. {{ $address := index $container.Addresses 0 }}
  71. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $network) }}
  72. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
  73. {{ else }}
  74. {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
  75. {{ $address := where $container.Addresses "Port" $port | first }}
  76. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $network) }}
  77. {{ end }}
  78. {{ end }}
  79. }
  80. {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
  81. {{ $default_server := index (dict $host "" $default_host "default_server") $host }}
  82. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  83. {{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }}
  84. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  85. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  86. {{/* Get the best matching cert by name for the vhost. */}}
  87. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  88. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  89. {{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
  90. {{ $vhostCert := replace $vhostCert ".key" "" -1 }}
  91. {{/* Use the cert specifid on the container or fallback to the best vhost match */}}
  92. {{ $cert := (coalesce $certName $vhostCert) }}
  93. {{ if (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  94. server {
  95. server_name {{ $host }};
  96. listen 80 {{ $default_server }};
  97. access_log /var/log/nginx/access.log vhost;
  98. return 301 https://$host$request_uri;
  99. }
  100. server {
  101. server_name {{ $host }};
  102. listen 443 ssl http2 {{ $default_server }};
  103. access_log /var/log/nginx/access.log vhost;
  104. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  105. 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;
  106. ssl_prefer_server_ciphers on;
  107. ssl_session_timeout 5m;
  108. ssl_session_cache shared:SSL:50m;
  109. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  110. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  111. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  112. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  113. {{ end }}
  114. add_header Strict-Transport-Security "max-age=31536000";
  115. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  116. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  117. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  118. include /etc/nginx/vhost.d/default;
  119. {{ end }}
  120. location / {
  121. proxy_pass {{ trim $proto }}://{{ trim $host }};
  122. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  123. auth_basic "Restricted {{ $host }}";
  124. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  125. {{ end }}
  126. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  127. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  128. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  129. include /etc/nginx/vhost.d/default_location;
  130. {{ end }}
  131. }
  132. }
  133. {{ else }}
  134. server {
  135. server_name {{ $host }};
  136. listen 80 {{ $default_server }};
  137. access_log /var/log/nginx/access.log vhost;
  138. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  139. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  140. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  141. include /etc/nginx/vhost.d/default;
  142. {{ end }}
  143. location / {
  144. proxy_pass {{ trim $proto }}://{{ trim $host }};
  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. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  157. server {
  158. server_name {{ $host }};
  159. listen 443 ssl http2 {{ $default_server }};
  160. access_log /var/log/nginx/access.log vhost;
  161. return 503;
  162. ssl_certificate /etc/nginx/certs/default.crt;
  163. ssl_certificate_key /etc/nginx/certs/default.key;
  164. }
  165. {{ end }}
  166. {{ end }}
  167. {{ end }}