nginx.tmpl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. server {
  62. server_name {{ $host }};
  63. location / {
  64. proxy_pass http://{{ $host }};
  65. }
  66. }
  67. {{ end }}