2
0

nginx.tmpl 9.1 KB

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