nginx.tmpl 18 KB

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