nginx.tmpl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  2. # scheme used to connect to this server
  3. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  4. default $http_x_forwarded_proto;
  5. '' $scheme;
  6. }
  7. # If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
  8. # Connection header that may have been passed to this server
  9. map $http_upgrade $proxy_connection {
  10. default upgrade;
  11. '' '';
  12. }
  13. gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  14. access_log /proc/self/fd/1;
  15. error_log /proc/self/fd/2;
  16. # HTTP 1.1 support
  17. proxy_http_version 1.1;
  18. proxy_buffering off;
  19. proxy_set_header Host $http_host;
  20. proxy_set_header Upgrade $http_upgrade;
  21. proxy_set_header Connection $proxy_connection;
  22. proxy_set_header X-Real-IP $remote_addr;
  23. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  24. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  25. server {
  26. listen 80 default_server;
  27. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  28. return 503;
  29. }
  30. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  31. upstream {{ $host }} {
  32. {{ range $container := $containers }}
  33. {{ $addrLen := len $container.Addresses }}
  34. {{/* If only 1 port exposed, use that */}}
  35. {{ if eq $addrLen 1 }}
  36. {{ with $address := index $container.Addresses 0 }}
  37. # {{$container.Name}}
  38. server {{ $address.IP }}:{{ $address.Port }};
  39. {{ end }}
  40. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}}
  41. {{ else if $container.Env.VIRTUAL_PORT }}
  42. {{ range $address := .Addresses }}
  43. {{ if eq $address.Port $container.Env.VIRTUAL_PORT }}
  44. # {{$container.Name}}
  45. server {{ $address.IP }}:{{ $address.Port }};
  46. {{ end }}
  47. {{ end }}
  48. {{/* Else default to standard web port 80 */}}
  49. {{ else }}
  50. {{ range $address := $container.Addresses }}
  51. {{ if eq $address.Port "80" }}
  52. # {{$container.Name}}
  53. server {{ $address.IP }}:{{ $address.Port }};
  54. {{ end }}
  55. {{ end }}
  56. {{ end }}
  57. {{ end }}
  58. }
  59. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  60. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  61. {{/* Get the best matching cert by name for the vhost. */}}
  62. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  63. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  64. {{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
  65. {{ $vhostCert := replace $vhostCert ".key" "" -1 }}
  66. {{/* Use the cert specifid on the container or fallback to the best vhost match */}}
  67. {{ $cert := (coalesce $certName $vhostCert) }}
  68. {{ if (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  69. server {
  70. server_name {{ $host }};
  71. rewrite ^(.*) https://{{ $host }}$1 permanent;
  72. }
  73. server {
  74. server_name {{ $host }};
  75. listen 443 ssl;
  76. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  77. 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;
  78. ssl_prefer_server_ciphers on;
  79. ssl_session_timeout 5m;
  80. ssl_session_cache shared:SSL:50m;
  81. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  82. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  83. add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
  84. location / {
  85. proxy_pass http://{{ $host }};
  86. }
  87. }
  88. {{ else }}
  89. server {
  90. server_name {{ $host }};
  91. location / {
  92. proxy_pass http://{{ $host }};
  93. }
  94. }
  95. server {
  96. server_name {{ $host }};
  97. listen 443 ssl;
  98. return 503;
  99. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  100. ssl_certificate /etc/nginx/certs/default.crt;
  101. ssl_certificate_key /etc/nginx/certs/default.key;
  102. {{ end }}
  103. }
  104. {{ end }}
  105. {{ end }}