nginx.tmpl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. {{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }}
  2. {{ $external_http_port := coalesce $.Env.HTTP_PORT "80" }}
  3. {{ $external_https_port := coalesce $.Env.HTTPS_PORT "443" }}
  4. {{ $debug_all := $.Env.DEBUG }}
  5. {{ define "upstream" }}
  6. {{ if .Address }}
  7. {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
  8. {{ if and .Container.Node.ID .Address.HostPort }}
  9. # {{ .Container.Node.Name }}/{{ .Container.Name }}
  10. server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }};
  11. {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
  12. {{ else if .Network }}
  13. # {{ .Container.Name }}
  14. server {{ .Network.IP }}:{{ .Address.Port }};
  15. {{ end }}
  16. {{ else if .Network }}
  17. # {{ .Container.Name }}
  18. {{ if .Network.IP }}
  19. server {{ .Network.IP }}:{{ .VirtualPort }};
  20. {{ else }}
  21. # /!\ No IP for this network!
  22. {{ end }}
  23. {{ end }}
  24. {{ end }}
  25. {{ define "ssl_policy" }}
  26. {{ if eq .ssl_policy "Mozilla-Modern" }}
  27. ssl_protocols TLSv1.3;
  28. {{/* nginx currently lacks ability to choose ciphers in TLS 1.3 in configuration, see https://trac.nginx.org/nginx/ticket/1529 /*}}
  29. {{/* a possible workaround can be modify /etc/ssl/openssl.cnf to change it globally (see https://trac.nginx.org/nginx/ticket/1529#comment:12 ) /*}}
  30. {{/* explicitly set ngnix default value in order to allow single servers to override the global http value */}}
  31. ssl_ciphers HIGH:!aNULL:!MD5;
  32. ssl_prefer_server_ciphers off;
  33. {{ else if eq .ssl_policy "Mozilla-Intermediate" }}
  34. ssl_protocols TLSv1.2 TLSv1.3;
  35. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
  36. ssl_prefer_server_ciphers off;
  37. {{ else if eq .ssl_policy "Mozilla-Old" }}
  38. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  39. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA';
  40. ssl_prefer_server_ciphers on;
  41. {{ else if eq .ssl_policy "AWS-TLS-1-2-2017-01" }}
  42. ssl_protocols TLSv1.2 TLSv1.3;
  43. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:AES128-GCM-SHA256:AES128-SHA256:AES256-GCM-SHA384:AES256-SHA256';
  44. ssl_prefer_server_ciphers on;
  45. {{ else if eq .ssl_policy "AWS-TLS-1-1-2017-01" }}
  46. ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
  47. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA';
  48. ssl_prefer_server_ciphers on;
  49. {{ else if eq .ssl_policy "AWS-2016-08" }}
  50. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  51. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA';
  52. ssl_prefer_server_ciphers on;
  53. {{ else if eq .ssl_policy "AWS-2015-05" }}
  54. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  55. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DES-CBC3-SHA';
  56. ssl_prefer_server_ciphers on;
  57. {{ else if eq .ssl_policy "AWS-2015-03" }}
  58. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  59. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA:DES-CBC3-SHA';
  60. ssl_prefer_server_ciphers on;
  61. {{ else if eq .ssl_policy "AWS-2015-02" }}
  62. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  63. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA';
  64. ssl_prefer_server_ciphers on;
  65. {{ end }}
  66. {{ end }}
  67. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  68. # scheme used to connect to this server
  69. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  70. default $http_x_forwarded_proto;
  71. '' $scheme;
  72. }
  73. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  74. # server port the client connected to
  75. map $http_x_forwarded_port $proxy_x_forwarded_port {
  76. default $http_x_forwarded_port;
  77. '' $server_port;
  78. }
  79. # If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
  80. # Connection header that may have been passed to this server
  81. map $http_upgrade $proxy_connection {
  82. default upgrade;
  83. '' close;
  84. }
  85. # Apply fix for very long server names
  86. server_names_hash_bucket_size 128;
  87. # Default dhparam
  88. {{ if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  89. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  90. {{ end }}
  91. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  92. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  93. default off;
  94. https on;
  95. }
  96. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  97. log_format vhost '$host $remote_addr - $remote_user [$time_local] '
  98. '"$request" $status $body_bytes_sent '
  99. '"$http_referer" "$http_user_agent"';
  100. access_log off;
  101. {{/* Get the SSL_POLICY defined by this container, falling back to "Mozilla-Intermediate" */}}
  102. {{ $ssl_policy := or ($.Env.SSL_POLICY) "Mozilla-Intermediate" }}
  103. {{ template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  104. {{ if $.Env.RESOLVERS }}
  105. resolver {{ $.Env.RESOLVERS }};
  106. {{ end }}
  107. {{ if (exists "/etc/nginx/proxy.conf") }}
  108. include /etc/nginx/proxy.conf;
  109. {{ else }}
  110. # HTTP 1.1 support
  111. proxy_http_version 1.1;
  112. proxy_buffering off;
  113. proxy_set_header Host $http_host;
  114. proxy_set_header Upgrade $http_upgrade;
  115. proxy_set_header Connection $proxy_connection;
  116. proxy_set_header X-Real-IP $remote_addr;
  117. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  118. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  119. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  120. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  121. # Mitigate httpoxy attack (see README for details)
  122. proxy_set_header Proxy "";
  123. {{ end }}
  124. {{ $access_log := (or (and (not $.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}
  125. {{ $enable_ipv6 := eq (or ($.Env.ENABLE_IPV6) "") "true" }}
  126. server {
  127. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  128. server_tokens off;
  129. listen {{ $external_http_port }};
  130. {{ if $enable_ipv6 }}
  131. listen [::]:{{ $external_http_port }};
  132. {{ end }}
  133. {{ $access_log }}
  134. return 503;
  135. }
  136. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  137. server {
  138. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  139. server_tokens off;
  140. listen {{ $external_https_port }} ssl http2;
  141. {{ if $enable_ipv6 }}
  142. listen [::]:{{ $external_https_port }} ssl http2;
  143. {{ end }}
  144. {{ $access_log }}
  145. return 503;
  146. ssl_session_cache shared:SSL:50m;
  147. ssl_session_tickets off;
  148. ssl_certificate /etc/nginx/certs/default.crt;
  149. ssl_certificate_key /etc/nginx/certs/default.key;
  150. }
  151. {{ end }}
  152. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  153. {{ $host := trim $host }}
  154. {{ $is_regexp := hasPrefix "~" $host }}
  155. {{ $upstream_name := (print (when $is_regexp (sha1 $host) $host) "-upstream") }}
  156. # {{ $host }}
  157. upstream {{ $upstream_name }} {
  158. {{ range $container := $containers }}
  159. {{ $debug := (eq (coalesce $container.Env.DEBUG $debug_all "false") "true") }}
  160. {{/* If only 1 port exposed, use that as a default, else 80 */}}
  161. {{ $defaultPort := (when (eq (len $container.Addresses) 1) (first $container.Addresses) (dict "Port" "80")).Port }}
  162. {{ range $knownNetwork := $CurrentContainer.Networks }}
  163. {{ range $containerNetwork := $container.Networks }}
  164. {{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }}
  165. ## Can be connected with "{{ $containerNetwork.Name }}" network
  166. {{ $port := (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }}
  167. {{ $address := where $container.Addresses "Port" $port | first }}
  168. {{ if $debug }}
  169. # Exposed ports: {{ $container.Addresses }}
  170. # Default virtual port: {{ $defaultPort }}
  171. # VIRTUAL_PORT: {{ $container.Env.VIRTUAL_PORT }}
  172. {{ if not $address }}
  173. # /!\ Virtual port not exposed
  174. {{ end }}
  175. {{ end }}
  176. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork "VirtualPort" $port) }}
  177. {{ else }}
  178. # Cannot connect to network '{{ $containerNetwork.Name }}' of this container
  179. {{ end }}
  180. {{ end }}
  181. {{ end }}
  182. {{/* nginx-proxy/nginx-proxy#1105 */}}
  183. # Fallback entry
  184. server 127.0.0.1 down;
  185. {{ end }}
  186. }
  187. {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
  188. {{ $default_server := index (dict $host "" $default_host "default_server") $host }}
  189. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  190. {{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
  191. {{/* Get the SERVER_TOKENS defined by containers w/ the same vhost, falling back to "" */}}
  192. {{ $server_tokens := trim (or (first (groupByKeys $containers "Env.SERVER_TOKENS")) "") }}
  193. {{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
  194. {{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  195. {{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}}
  196. {{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) (or $.Env.HTTPS_METHOD "redirect") }}
  197. {{/* Get the SSL_POLICY defined by containers w/ the same vhost, falling back to empty string (use default) */}}
  198. {{ $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }}
  199. {{/* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000" */}}
  200. {{ $hsts := or (first (groupByKeys $containers "Env.HSTS")) (or $.Env.HSTS "max-age=31536000") }}
  201. {{/* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  202. {{ $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
  203. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  204. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  205. {{/* Get the best matching cert by name for the vhost. */}}
  206. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  207. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  208. {{ $vhostCert := trimSuffix ".crt" $vhostCert }}
  209. {{ $vhostCert := trimSuffix ".key" $vhostCert }}
  210. {{/* Use the cert specified on the container or fallback to the best vhost match */}}
  211. {{ $cert := (coalesce $certName $vhostCert) }}
  212. {{ $is_https := (and (ne $https_method "nohttps") (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  213. {{ if $is_https }}
  214. {{ if eq $https_method "redirect" }}
  215. server {
  216. server_name {{ $host }};
  217. {{ if $server_tokens }}
  218. server_tokens {{ $server_tokens }};
  219. {{ end }}
  220. listen {{ $external_http_port }} {{ $default_server }};
  221. {{ if $enable_ipv6 }}
  222. listen [::]:{{ $external_http_port }} {{ $default_server }};
  223. {{ end }}
  224. {{ $access_log }}
  225. # Do not HTTPS redirect Let'sEncrypt ACME challenge
  226. location ^~ /.well-known/acme-challenge/ {
  227. auth_basic off;
  228. auth_request off;
  229. allow all;
  230. root /usr/share/nginx/html;
  231. try_files $uri =404;
  232. break;
  233. }
  234. location / {
  235. return 301 https://$host$request_uri;
  236. }
  237. }
  238. {{ end }}
  239. server {
  240. server_name {{ $host }};
  241. {{ if $server_tokens }}
  242. server_tokens {{ $server_tokens }};
  243. {{ end }}
  244. listen {{ $external_https_port }} ssl http2 {{ $default_server }};
  245. {{ if $enable_ipv6 }}
  246. listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }};
  247. {{ end }}
  248. {{ $access_log }}
  249. {{ if eq $network_tag "internal" }}
  250. # Only allow traffic from internal clients
  251. include /etc/nginx/network_internal.conf;
  252. {{ end }}
  253. {{ template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  254. ssl_session_timeout 5m;
  255. ssl_session_cache shared:SSL:50m;
  256. ssl_session_tickets off;
  257. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  258. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  259. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  260. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  261. {{ end }}
  262. {{ if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }}
  263. ssl_stapling on;
  264. ssl_stapling_verify on;
  265. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }};
  266. {{ end }}
  267. {{ if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }}
  268. add_header Strict-Transport-Security "{{ trim $hsts }}" always;
  269. {{ end }}
  270. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  271. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  272. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  273. include /etc/nginx/vhost.d/default;
  274. {{ end }}
  275. location / {
  276. {{ if eq $proto "uwsgi" }}
  277. include uwsgi_params;
  278. uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
  279. {{ else if eq $proto "fastcgi" }}
  280. root {{ trim $vhost_root }};
  281. include fastcgi_params;
  282. fastcgi_pass {{ trim $upstream_name }};
  283. {{ else if eq $proto "grpc" }}
  284. grpc_pass {{ trim $proto }}://{{ trim $upstream_name }};
  285. {{ else }}
  286. proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
  287. {{ end }}
  288. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  289. auth_basic "Restricted {{ $host }}";
  290. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  291. {{ end }}
  292. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  293. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  294. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  295. include /etc/nginx/vhost.d/default_location;
  296. {{ end }}
  297. }
  298. }
  299. {{ end }}
  300. {{ if or (not $is_https) (eq $https_method "noredirect") }}
  301. server {
  302. server_name {{ $host }};
  303. {{ if $server_tokens }}
  304. server_tokens {{ $server_tokens }};
  305. {{ end }}
  306. listen {{ $external_http_port }} {{ $default_server }};
  307. {{ if $enable_ipv6 }}
  308. listen [::]:80 {{ $default_server }};
  309. {{ end }}
  310. {{ $access_log }}
  311. {{ if eq $network_tag "internal" }}
  312. # Only allow traffic from internal clients
  313. include /etc/nginx/network_internal.conf;
  314. {{ end }}
  315. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  316. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  317. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  318. include /etc/nginx/vhost.d/default;
  319. {{ end }}
  320. location / {
  321. {{ if eq $proto "uwsgi" }}
  322. include uwsgi_params;
  323. uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
  324. {{ else if eq $proto "fastcgi" }}
  325. root {{ trim $vhost_root }};
  326. include fastcgi_params;
  327. fastcgi_pass {{ trim $upstream_name }};
  328. {{ else if eq $proto "grpc" }}
  329. grpc_pass {{ trim $proto }}://{{ trim $upstream_name }};
  330. {{ else }}
  331. proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
  332. {{ end }}
  333. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  334. auth_basic "Restricted {{ $host }}";
  335. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  336. {{ end }}
  337. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  338. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  339. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  340. include /etc/nginx/vhost.d/default_location;
  341. {{ end }}
  342. }
  343. }
  344. {{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  345. server {
  346. server_name {{ $host }};
  347. {{ if $server_tokens }}
  348. server_tokens {{ $server_tokens }};
  349. {{ end }}
  350. listen {{ $external_https_port }} ssl http2 {{ $default_server }};
  351. {{ if $enable_ipv6 }}
  352. listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }};
  353. {{ end }}
  354. {{ $access_log }}
  355. return 500;
  356. ssl_certificate /etc/nginx/certs/default.crt;
  357. ssl_certificate_key /etc/nginx/certs/default.key;
  358. }
  359. {{ end }}
  360. {{ end }}
  361. {{ end }}