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