2
0

nginx.tmpl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 /proc/self/fd/1 vhost;
  34. error_log /proc/self/fd/2;
  35. # HTTP 1.1 support
  36. proxy_http_version 1.1;
  37. proxy_buffering off;
  38. proxy_set_header Host $http_host;
  39. proxy_set_header Upgrade $http_upgrade;
  40. proxy_set_header Connection $proxy_connection;
  41. proxy_set_header X-Real-IP $remote_addr;
  42. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  43. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  44. server {
  45. listen 80;
  46. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  47. return 503;
  48. }
  49. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  50. upstream {{ $host }} {
  51. {{ range $container := $containers }}
  52. {{ $addrLen := len $container.Addresses }}
  53. {{/* If only 1 port exposed, use that */}}
  54. {{ if eq $addrLen 1 }}
  55. {{ $address := index $container.Addresses 0 }}
  56. {{ template "upstream" (dict "Container" $container "Address" $address) }}
  57. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
  58. {{ else }}
  59. {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
  60. {{ $address := where $container.Addresses "Port" $port | first }}
  61. {{ template "upstream" (dict "Container" $container "Address" $address) }}
  62. {{ end }}
  63. {{ end }}
  64. }
  65. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  66. {{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }}
  67. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  68. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  69. {{/* Get the best matching cert by name for the vhost. */}}
  70. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  71. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  72. {{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
  73. {{ $vhostCert := replace $vhostCert ".key" "" -1 }}
  74. {{/* Use the cert specifid on the container or fallback to the best vhost match */}}
  75. {{ $cert := (coalesce $certName $vhostCert) }}
  76. {{ if (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  77. server {
  78. server_name {{ $host }};
  79. return 301 https://$host$request_uri;
  80. }
  81. server {
  82. server_name {{ $host }};
  83. listen 443 ssl spdy;
  84. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  85. 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;
  86. ssl_prefer_server_ciphers on;
  87. ssl_session_timeout 5m;
  88. ssl_session_cache shared:SSL:50m;
  89. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  90. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  91. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  92. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  93. {{ end }}
  94. add_header Strict-Transport-Security "max-age=31536000";
  95. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  96. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  97. {{ end }}
  98. location / {
  99. proxy_pass {{ $proto }}://{{ $host }};
  100. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  101. auth_basic "Restricted {{ $host }}";
  102. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  103. {{ end }}
  104. }
  105. }
  106. {{ else }}
  107. server {
  108. {{ if $.Env.DEFAULT_HOST }}
  109. {{ if eq $.Env.DEFAULT_HOST $host }}
  110. listen 80 default_server;
  111. server_name {{ $host }};
  112. {{ else }}
  113. server_name {{ $host }};
  114. {{ end }}
  115. {{ else }}
  116. server_name {{ $host }};
  117. {{ end }}
  118. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  119. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  120. {{ end }}
  121. location / {
  122. proxy_pass {{ $proto }}://{{ $host }};
  123. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  124. auth_basic "Restricted {{ $host }}";
  125. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  126. {{ end }}
  127. }
  128. }
  129. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  130. server {
  131. server_name {{ $host }};
  132. listen 443 ssl spdy;
  133. return 503;
  134. ssl_certificate /etc/nginx/certs/default.crt;
  135. ssl_certificate_key /etc/nginx/certs/default.key;
  136. }
  137. {{ end }}
  138. {{ end }}
  139. {{ end }}