nginx.tmpl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. '"$upstream_addr"';
  101. access_log off;
  102. {{/* Get the SSL_POLICY defined by this container, falling back to "Mozilla-Intermediate" */}}
  103. {{ $ssl_policy := or ($.Env.SSL_POLICY) "Mozilla-Intermediate" }}
  104. {{ template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  105. {{ if $.Env.RESOLVERS }}
  106. resolver {{ $.Env.RESOLVERS }};
  107. {{ end }}
  108. {{ if (exists "/etc/nginx/proxy.conf") }}
  109. include /etc/nginx/proxy.conf;
  110. {{ else }}
  111. # HTTP 1.1 support
  112. proxy_http_version 1.1;
  113. proxy_buffering off;
  114. proxy_set_header Host $http_host;
  115. proxy_set_header Upgrade $http_upgrade;
  116. proxy_set_header Connection $proxy_connection;
  117. proxy_set_header X-Real-IP $remote_addr;
  118. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  119. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  120. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  121. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  122. # Mitigate httpoxy attack (see README for details)
  123. proxy_set_header Proxy "";
  124. {{ end }}
  125. {{ $access_log := (or (and (not $.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}
  126. {{ $enable_ipv6 := eq (or ($.Env.ENABLE_IPV6) "") "true" }}
  127. server {
  128. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  129. server_tokens off;
  130. listen {{ $external_http_port }};
  131. {{ if $enable_ipv6 }}
  132. listen [::]:{{ $external_http_port }};
  133. {{ end }}
  134. {{ $access_log }}
  135. return 503;
  136. }
  137. {{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  138. server {
  139. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  140. server_tokens off;
  141. listen {{ $external_https_port }} ssl http2;
  142. {{ if $enable_ipv6 }}
  143. listen [::]:{{ $external_https_port }} ssl http2;
  144. {{ end }}
  145. {{ $access_log }}
  146. return 503;
  147. ssl_session_cache shared:SSL:50m;
  148. ssl_session_tickets off;
  149. ssl_certificate /etc/nginx/certs/default.crt;
  150. ssl_certificate_key /etc/nginx/certs/default.key;
  151. }
  152. {{ end }}
  153. {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
  154. {{ $host := trim $host }}
  155. {{ $is_regexp := hasPrefix "~" $host }}
  156. {{ $upstream_name := (print (when $is_regexp (sha1 $host) $host) "-upstream") }}
  157. # {{ $host }}
  158. upstream {{ $upstream_name }} {
  159. {{ range $container := $containers }}
  160. {{ $debug := (eq (coalesce $container.Env.DEBUG $debug_all "false") "true") }}
  161. {{/* If only 1 port exposed, use that as a default, else 80 */}}
  162. {{ $defaultPort := (when (eq (len $container.Addresses) 1) (first $container.Addresses) (dict "Port" "80")).Port }}
  163. {{ range $knownNetwork := $CurrentContainer.Networks }}
  164. {{ range $containerNetwork := $container.Networks }}
  165. {{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }}
  166. ## Can be connected with "{{ $containerNetwork.Name }}" network
  167. {{ $port := (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }}
  168. {{ $address := where $container.Addresses "Port" $port | first }}
  169. {{ if $debug }}
  170. # Exposed ports: {{ $container.Addresses }}
  171. # Default virtual port: {{ $defaultPort }}
  172. # VIRTUAL_PORT: {{ $container.Env.VIRTUAL_PORT }}
  173. {{ if not $address }}
  174. # /!\ Virtual port not exposed
  175. {{ end }}
  176. {{ end }}
  177. {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork "VirtualPort" $port) }}
  178. {{ else }}
  179. # Cannot connect to network '{{ $containerNetwork.Name }}' of this container
  180. {{ end }}
  181. {{ end }}
  182. {{ end }}
  183. {{/* nginx-proxy/nginx-proxy#1105 */}}
  184. # Fallback entry
  185. server 127.0.0.1 down;
  186. {{ end }}
  187. }
  188. {{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
  189. {{ $default_server := index (dict $host "" $default_host "default_server") $host }}
  190. {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
  191. {{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
  192. {{/* Get the SERVER_TOKENS defined by containers w/ the same vhost, falling back to "" */}}
  193. {{ $server_tokens := trim (or (first (groupByKeys $containers "Env.SERVER_TOKENS")) "") }}
  194. {{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
  195. {{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  196. {{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}}
  197. {{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) (or $.Env.HTTPS_METHOD "redirect") }}
  198. {{/* Get the SSL_POLICY defined by containers w/ the same vhost, falling back to empty string (use default) */}}
  199. {{ $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }}
  200. {{/* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000" */}}
  201. {{ $hsts := or (first (groupByKeys $containers "Env.HSTS")) (or $.Env.HSTS "max-age=31536000") }}
  202. {{/* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  203. {{ $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
  204. {{/* Get the first cert name defined by containers w/ the same vhost */}}
  205. {{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  206. {{/* Get the best matching cert by name for the vhost. */}}
  207. {{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  208. {{/* vhostCert is actually a filename so remove any suffixes since they are added later */}}
  209. {{ $vhostCert := trimSuffix ".crt" $vhostCert }}
  210. {{ $vhostCert := trimSuffix ".key" $vhostCert }}
  211. {{/* Use the cert specified on the container or fallback to the best vhost match */}}
  212. {{ $cert := (coalesce $certName $vhostCert) }}
  213. {{ $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))) }}
  214. {{ if $is_https }}
  215. {{ if eq $https_method "redirect" }}
  216. server {
  217. server_name {{ $host }};
  218. {{ if $server_tokens }}
  219. server_tokens {{ $server_tokens }};
  220. {{ end }}
  221. listen {{ $external_http_port }} {{ $default_server }};
  222. {{ if $enable_ipv6 }}
  223. listen [::]:{{ $external_http_port }} {{ $default_server }};
  224. {{ end }}
  225. {{ $access_log }}
  226. # Do not HTTPS redirect Let'sEncrypt ACME challenge
  227. location ^~ /.well-known/acme-challenge/ {
  228. auth_basic off;
  229. auth_request off;
  230. allow all;
  231. root /usr/share/nginx/html;
  232. try_files $uri =404;
  233. break;
  234. }
  235. location / {
  236. return 301 https://$host$request_uri;
  237. }
  238. }
  239. {{ end }}
  240. server {
  241. server_name {{ $host }};
  242. {{ if $server_tokens }}
  243. server_tokens {{ $server_tokens }};
  244. {{ end }}
  245. listen {{ $external_https_port }} ssl http2 {{ $default_server }};
  246. {{ if $enable_ipv6 }}
  247. listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }};
  248. {{ end }}
  249. {{ $access_log }}
  250. {{ if eq $network_tag "internal" }}
  251. # Only allow traffic from internal clients
  252. include /etc/nginx/network_internal.conf;
  253. {{ end }}
  254. {{ template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  255. ssl_session_timeout 5m;
  256. ssl_session_cache shared:SSL:50m;
  257. ssl_session_tickets off;
  258. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  259. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  260. {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  261. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  262. {{ end }}
  263. {{ if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }}
  264. ssl_stapling on;
  265. ssl_stapling_verify on;
  266. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }};
  267. {{ end }}
  268. {{ if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }}
  269. add_header Strict-Transport-Security "{{ trim $hsts }}" always;
  270. {{ end }}
  271. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  272. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  273. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  274. include /etc/nginx/vhost.d/default;
  275. {{ end }}
  276. location / {
  277. {{ if eq $proto "uwsgi" }}
  278. include uwsgi_params;
  279. uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
  280. {{ else if eq $proto "fastcgi" }}
  281. root {{ trim $vhost_root }};
  282. include fastcgi_params;
  283. fastcgi_pass {{ trim $upstream_name }};
  284. {{ else if eq $proto "grpc" }}
  285. grpc_pass {{ trim $proto }}://{{ trim $upstream_name }};
  286. {{ else }}
  287. proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
  288. {{ end }}
  289. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  290. auth_basic "Restricted {{ $host }}";
  291. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  292. {{ end }}
  293. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  294. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  295. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  296. include /etc/nginx/vhost.d/default_location;
  297. {{ end }}
  298. }
  299. }
  300. {{ end }}
  301. {{ if or (not $is_https) (eq $https_method "noredirect") }}
  302. server {
  303. server_name {{ $host }};
  304. {{ if $server_tokens }}
  305. server_tokens {{ $server_tokens }};
  306. {{ end }}
  307. listen {{ $external_http_port }} {{ $default_server }};
  308. {{ if $enable_ipv6 }}
  309. listen [::]:80 {{ $default_server }};
  310. {{ end }}
  311. {{ $access_log }}
  312. {{ if eq $network_tag "internal" }}
  313. # Only allow traffic from internal clients
  314. include /etc/nginx/network_internal.conf;
  315. {{ end }}
  316. {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  317. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  318. {{ else if (exists "/etc/nginx/vhost.d/default") }}
  319. include /etc/nginx/vhost.d/default;
  320. {{ end }}
  321. location / {
  322. {{ if eq $proto "uwsgi" }}
  323. include uwsgi_params;
  324. uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
  325. {{ else if eq $proto "fastcgi" }}
  326. root {{ trim $vhost_root }};
  327. include fastcgi_params;
  328. fastcgi_pass {{ trim $upstream_name }};
  329. {{ else if eq $proto "grpc" }}
  330. grpc_pass {{ trim $proto }}://{{ trim $upstream_name }};
  331. {{ else }}
  332. proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
  333. {{ end }}
  334. {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
  335. auth_basic "Restricted {{ $host }}";
  336. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
  337. {{ end }}
  338. {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
  339. include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
  340. {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
  341. include /etc/nginx/vhost.d/default_location;
  342. {{ end }}
  343. }
  344. }
  345. {{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  346. server {
  347. server_name {{ $host }};
  348. {{ if $server_tokens }}
  349. server_tokens {{ $server_tokens }};
  350. {{ end }}
  351. listen {{ $external_https_port }} ssl http2 {{ $default_server }};
  352. {{ if $enable_ipv6 }}
  353. listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }};
  354. {{ end }}
  355. {{ $access_log }}
  356. return 500;
  357. ssl_certificate /etc/nginx/certs/default.crt;
  358. ssl_certificate_key /etc/nginx/certs/default.key;
  359. }
  360. {{ end }}
  361. {{ end }}
  362. {{ end }}