nginx.tmpl 18 KB

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