nginx.tmpl 18 KB

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