nginx.tmpl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. error_log /proc/self/fd/2;
  29. access_log /proc/self/fd/1;
  30. return 503;
  31. }
  32. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  33. upstream {{ $host }} {
  34. {{ range $container := $containers }}
  35. {{ $addrLen := len $container.Addresses }}
  36. {{/* If only 1 port exposed, use that */}}
  37. {{ if eq $addrLen 1 }}
  38. {{ with $address := index $container.Addresses 0 }}
  39. # {{$container.Name}}
  40. server {{ $address.IP }}:{{ $address.Port }};
  41. {{ end }}
  42. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}}
  43. {{ else if $container.Env.VIRTUAL_PORT }}
  44. {{ range $address := .Addresses }}
  45. {{ if eq $address.Port $container.Env.VIRTUAL_PORT }}
  46. # {{$container.Name}}
  47. server {{ $address.IP }}:{{ $address.Port }};
  48. {{ end }}
  49. {{ end }}
  50. {{/* Else default to standard web port 80 */}}
  51. {{ else }}
  52. {{ range $address := $container.Addresses }}
  53. {{ if eq $address.Port "80" }}
  54. # {{$container.Name}}
  55. server {{ $address.IP }}:{{ $address.Port }};
  56. {{ end }}
  57. {{ end }}
  58. {{ end }}
  59. {{ end }}
  60. }
  61. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  62. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  63. {{/* Get the best matching cert by name for the vhost. */}}
  64. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  65. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  66. {{ $vhostCert := replace $vhostCert ".crt" "" -1 }}
  67. {{ $vhostCert := replace $vhostCert ".key" "" -1 }}
  68. {{/* Use the cert specifid on the container or fallback to the best vhost match */}}
  69. {{ $cert := (coalesce $certName $vhostCert) }}
  70. {{ if (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  71. server {
  72. server_name {{ $host }};
  73. rewrite ^(.*) https://{{ $host }}$1 permanent;
  74. }
  75. server {
  76. server_name {{ $host }};
  77. listen 443 ssl;
  78. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  79. 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;
  80. ssl_prefer_server_ciphers on;
  81. ssl_session_timeout 5m;
  82. ssl_session_cache shared:SSL:50m;
  83. ssl_stapling on;
  84. ssl_stapling_verify on;
  85. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  86. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  87. add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
  88. location / {
  89. proxy_pass http://{{ $host }};
  90. }
  91. }
  92. {{ else }}
  93. server {
  94. server_name {{ $host }};
  95. location / {
  96. proxy_pass http://{{ $host }};
  97. }
  98. }
  99. server {
  100. server_name {{ $host }};
  101. listen 443 ssl;
  102. return 503;
  103. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  104. ssl_certificate /etc/nginx/certs/default.crt;
  105. ssl_certificate_key /etc/nginx/certs/default.key;
  106. {{ end }}
  107. }
  108. {{ end }}
  109. {{ end }}