nginx.tmpl 18 KB

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