nginx.tmpl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. server {
  14. listen 80 default_server;
  15. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  16. error_log /proc/self/fd/2;
  17. access_log /proc/self/fd/1;
  18. return 503;
  19. }
  20. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  21. upstream {{ $host }} {
  22. {{ range $index, $value := $containers }}
  23. {{ $addrLen := len $value.Addresses }}
  24. {{/* If only 1 port exposed, use that */}}
  25. {{ if eq $addrLen 1 }}
  26. {{ with $address := index $value.Addresses 0 }}
  27. # {{$value.Name}}
  28. server {{ $address.IP }}:{{ $address.Port }};
  29. {{ end }}
  30. {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}}
  31. {{ else if $value.Env.VIRTUAL_PORT }}
  32. {{ range $i, $address := $value.Addresses }}
  33. {{ if eq $address.Port $value.Env.VIRTUAL_PORT }}
  34. # {{$value.Name}}
  35. server {{ $address.IP }}:{{ $address.Port }};
  36. {{ end }}
  37. {{ end }}
  38. {{/* Else default to standard web port 80 */}}
  39. {{ else }}
  40. {{ range $i, $address := $value.Addresses }}
  41. {{ if eq $address.Port "80" }}
  42. # {{$value.Name}}
  43. server {{ $address.IP }}:{{ $address.Port }};
  44. {{ end }}
  45. {{ end }}
  46. {{ end }}
  47. {{ end }}
  48. }
  49. server {
  50. gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  51. server_name {{ $host }};
  52. proxy_buffering off;
  53. error_log /proc/self/fd/2;
  54. access_log /proc/self/fd/1;
  55. location / {
  56. proxy_pass http://{{ $host }};
  57. proxy_set_header Host $http_host;
  58. proxy_set_header X-Real-IP $remote_addr;
  59. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  60. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  61. # HTTP 1.1 support
  62. proxy_http_version 1.1;
  63. proxy_set_header Upgrade $http_upgrade;
  64. proxy_set_header Connection $proxy_connection;
  65. }
  66. }
  67. {{ end }}