accplan commited on
Commit
6d425f2
1 Parent(s): df48ffd
Dockerfile CHANGED
@@ -67,7 +67,7 @@ COPY --from=dockergen /usr/local/bin/docker-gen /usr/local/bin/docker-gen
67
 
68
  COPY network_internal.conf /etc/nginx/
69
 
70
- COPY app nginx.tmpl LICENSE /app/
71
  WORKDIR /app/
72
 
73
  EXPOSE 7860
 
67
 
68
  COPY network_internal.conf /etc/nginx/
69
 
70
+ COPY app nginx.tmpl /app/
71
  WORKDIR /app/
72
 
73
  EXPOSE 7860
app/Procfile ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ dockergen: docker-gen -watch -notify "nginx -s reload" /app/nginx.tmpl /etc/nginx/conf.d/default.conf
2
+ nginx: nginx
app/dhparam/ffdhe2048.pem ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN DH PARAMETERS-----
2
+ MIIBCAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz
3
+ +8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a
4
+ 87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7
5
+ YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi
6
+ 7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD
7
+ ssbzSibBsu/6iGtCOGEoXJf//////////wIBAg==
8
+ -----END DH PARAMETERS-----
app/dhparam/ffdhe3072.pem ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN DH PARAMETERS-----
2
+ MIIBiAKCAYEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz
3
+ +8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a
4
+ 87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7
5
+ YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi
6
+ 7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD
7
+ ssbzSibBsu/6iGtCOGEfz9zeNVs7ZRkDW7w09N75nAI4YbRvydbmyQd62R0mkff3
8
+ 7lmMsPrBhtkcrv4TCYUTknC0EwyTvEN5RPT9RFLi103TZPLiHnH1S/9croKrnJ32
9
+ nuhtK8UiNjoNq8Uhl5sN6todv5pC1cRITgq80Gv6U93vPBsg7j/VnXwl5B0rZsYu
10
+ N///////////AgEC
11
+ -----END DH PARAMETERS-----
app/dhparam/ffdhe4096.pem ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN DH PARAMETERS-----
2
+ MIICCAKCAgEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz
3
+ +8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a
4
+ 87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7
5
+ YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi
6
+ 7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD
7
+ ssbzSibBsu/6iGtCOGEfz9zeNVs7ZRkDW7w09N75nAI4YbRvydbmyQd62R0mkff3
8
+ 7lmMsPrBhtkcrv4TCYUTknC0EwyTvEN5RPT9RFLi103TZPLiHnH1S/9croKrnJ32
9
+ nuhtK8UiNjoNq8Uhl5sN6todv5pC1cRITgq80Gv6U93vPBsg7j/VnXwl5B0rZp4e
10
+ 8W5vUsMWTfT7eTDp5OWIV7asfV9C1p9tGHdjzx1VA0AEh/VbpX4xzHpxNciG77Qx
11
+ iu1qHgEtnmgyqQdgCpGBMMRtx3j5ca0AOAkpmaMzy4t6Gh25PXFAADwqTs6p+Y0K
12
+ zAqCkc3OyX3Pjsm1Wn+IpGtNtahR9EGC4caKAH5eZV9q//////////8CAQI=
13
+ -----END DH PARAMETERS-----
app/docker-entrypoint.sh ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ function _parse_true() {
5
+ case "$1" in
6
+
7
+ true | True | TRUE | 1)
8
+ return 0
9
+ ;;
10
+
11
+ *)
12
+ return 1
13
+ ;;
14
+
15
+ esac
16
+ }
17
+
18
+ function _parse_false() {
19
+ case "$1" in
20
+
21
+ false | False | FALSE | 0)
22
+ return 0
23
+ ;;
24
+
25
+ *)
26
+ return 1
27
+ ;;
28
+
29
+ esac
30
+ }
31
+
32
+ function _print_version {
33
+ if [[ -n "${NGINX_PROXY_VERSION:-}" ]]; then
34
+ echo "Info: running nginx-proxy version ${NGINX_PROXY_VERSION}"
35
+ fi
36
+ }
37
+
38
+ function _check_unix_socket() {
39
+ # Warn if the DOCKER_HOST socket does not exist
40
+ if [[ ${DOCKER_HOST} == unix://* ]]; then
41
+ local SOCKET_FILE="${DOCKER_HOST#unix://}"
42
+
43
+ if [[ ! -S ${SOCKET_FILE} ]]; then
44
+ cat >&2 <<-EOT
45
+ ERROR: you need to share your Docker host socket with a volume at ${SOCKET_FILE}
46
+ Typically you should run your nginxproxy/nginx-proxy with: \`-v /var/run/docker.sock:${SOCKET_FILE}:ro\`
47
+ See the documentation at: https://github.com/nginx-proxy/nginx-proxy/#usage
48
+ EOT
49
+
50
+ exit 1
51
+ fi
52
+ fi
53
+ }
54
+
55
+ function _resolvers() {
56
+ # Compute the DNS resolvers for use in the templates - if the IP contains ":", it's IPv6 and must be enclosed in []
57
+ RESOLVERS=$(awk '$1 == "nameserver" {print ($2 ~ ":")? "["$2"]": $2}' ORS=' ' /etc/resolv.conf | sed 's/ *$//g'); export RESOLVERS
58
+
59
+ SCOPED_IPV6_REGEX='\[fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}\]'
60
+
61
+ if [[ -z ${RESOLVERS} ]]; then
62
+ echo 'Warning: unable to determine DNS resolvers for nginx' >&2
63
+ unset RESOLVERS
64
+ elif [[ ${RESOLVERS} =~ ${SCOPED_IPV6_REGEX} ]]; then
65
+ echo -n 'Warning: Scoped IPv6 addresses removed from resolvers: ' >&2
66
+ echo "${RESOLVERS}" | grep -Eo "$SCOPED_IPV6_REGEX" | paste -s -d ' ' >&2
67
+ RESOLVERS=$(echo "${RESOLVERS}" | sed -r "s/${SCOPED_IPV6_REGEX}//g" | xargs echo -n); export RESOLVERS
68
+ fi
69
+ }
70
+
71
+ function _setup_dhparam() {
72
+ # DH params will be supplied for nginx here:
73
+ local DHPARAM_FILE='/etc/nginx/dhparam/dhparam.pem'
74
+
75
+ # Should be 2048, 3072, or 4096 (default):
76
+ local FFDHE_GROUP="${DHPARAM_BITS:=4096}"
77
+
78
+ # DH params may be provided by the user (rarely necessary)
79
+ if [[ -f ${DHPARAM_FILE} ]]; then
80
+ echo 'Warning: A custom dhparam.pem file was provided. Best practice is to use standardized RFC7919 DHE groups instead.' >&2
81
+ return 0
82
+ elif _parse_true "${DHPARAM_SKIP:=false}"; then
83
+ echo 'Skipping Diffie-Hellman parameters setup.'
84
+ return 0
85
+ elif _parse_false "${DHPARAM_GENERATION:=true}"; then
86
+ echo 'Warning: The DHPARAM_GENERATION environment variable is deprecated, please consider using DHPARAM_SKIP set to true instead.' >&2
87
+ echo 'Skipping Diffie-Hellman parameters setup.'
88
+ return 0
89
+ elif [[ ! ${DHPARAM_BITS} =~ ^(2048|3072|4096)$ ]]; then
90
+ echo "ERROR: Unsupported DHPARAM_BITS size: ${DHPARAM_BITS}. Use: 2048, 3072, or 4096 (default)." >&2
91
+ exit 1
92
+ fi
93
+
94
+ echo 'Setting up DH Parameters..'
95
+
96
+ # Use an existing pre-generated DH group from RFC7919 (https://datatracker.ietf.org/doc/html/rfc7919#appendix-A):
97
+ local RFC7919_DHPARAM_FILE="/app/dhparam/ffdhe${FFDHE_GROUP}.pem"
98
+
99
+ # Provide the DH params file to nginx:
100
+ cp "${RFC7919_DHPARAM_FILE}" "${DHPARAM_FILE}"
101
+ }
102
+
103
+ # Run the init logic if the default CMD was provided
104
+ if [[ $* == 'forego start -r' ]]; then
105
+ _print_version
106
+
107
+ _check_unix_socket
108
+
109
+ _resolvers
110
+
111
+ _setup_dhparam
112
+
113
+ if [ -z "${TRUST_DOWNSTREAM_PROXY}" ]; then
114
+ cat >&2 <<-EOT
115
+ Warning: TRUST_DOWNSTREAM_PROXY is not set; defaulting to "true". For security, you should explicitly set TRUST_DOWNSTREAM_PROXY to "false" if there is not a trusted reverse proxy in front of this proxy.
116
+ Warning: The default value of TRUST_DOWNSTREAM_PROXY might change to "false" in a future version of nginx-proxy. If you require TRUST_DOWNSTREAM_PROXY to be enabled, explicitly set it to "true".
117
+ EOT
118
+ fi
119
+ fi
120
+
121
+ exec "$@"
network_internal.conf ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ # Only allow traffic from internal clients
2
+ allow 127.0.0.0/8;
3
+ allow 10.0.0.0/8;
4
+ allow 192.168.0.0/16;
5
+ allow 172.16.0.0/12;
6
+ allow fc00::/7; # IPv6 local address range
7
+ deny all;
nginx.tmpl ADDED
@@ -0,0 +1,627 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # nginx-proxy{{ if $.Env.NGINX_PROXY_VERSION }} version : {{ $.Env.NGINX_PROXY_VERSION }}{{ end }}
2
+
3
+ {{- /*
4
+ * Global values. Values are stored in this map rather than in individual
5
+ * global variables so that the values can be easily passed to embedded
6
+ * templates. (Go templates cannot access variables outside of their own
7
+ * scope.)
8
+ */}}
9
+ {{- $globals := dict }}
10
+ {{- $_ := set $globals "containers" $ }}
11
+ {{- $_ := set $globals "Env" $.Env }}
12
+ {{- $_ := set $globals "Docker" $.Docker }}
13
+ {{- $_ := set $globals "CurrentContainer" (where $globals.containers "ID" $globals.Docker.CurrentContainerID | first) }}
14
+ {{- $_ := set $globals "default_cert_ok" (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
15
+ {{- $_ := set $globals "external_http_port" (coalesce $globals.Env.HTTP_PORT "80") }}
16
+ {{- $_ := set $globals "external_https_port" (coalesce $globals.Env.HTTPS_PORT "443") }}
17
+ {{- $_ := set $globals "sha1_upstream_name" (parseBool (coalesce $globals.Env.SHA1_UPSTREAM_NAME "false")) }}
18
+ {{- $_ := set $globals "default_root_response" (coalesce $globals.Env.DEFAULT_ROOT "404") }}
19
+ {{- $_ := set $globals "trust_downstream_proxy" (parseBool (coalesce $globals.Env.TRUST_DOWNSTREAM_PROXY "true")) }}
20
+ {{- $_ := set $globals "access_log" (or (and (not $globals.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}
21
+ {{- $_ := set $globals "enable_ipv6" (parseBool (coalesce $globals.Env.ENABLE_IPV6 "false")) }}
22
+ {{- $_ := set $globals "ssl_policy" (or ($globals.Env.SSL_POLICY) "Mozilla-Intermediate") }}
23
+ {{- $_ := set $globals "vhosts" (dict) }}
24
+ {{- $_ := set $globals "networks" (dict) }}
25
+ # Networks available to the container running docker-gen (which are assumed to
26
+ # match the networks available to the container running nginx):
27
+ {{- /*
28
+ * Note: $globals.CurrentContainer may be nil in some circumstances due to
29
+ * <https://github.com/nginx-proxy/docker-gen/issues/458>. For more context
30
+ * see <https://github.com/nginx-proxy/nginx-proxy/issues/2189>.
31
+ */}}
32
+ {{- if $globals.CurrentContainer }}
33
+ {{- range sortObjectsByKeysAsc $globals.CurrentContainer.Networks "Name" }}
34
+ {{- $_ := set $globals.networks .Name . }}
35
+ # {{ .Name }}
36
+ {{- else }}
37
+ # (none)
38
+ {{- end }}
39
+ {{- else }}
40
+ # /!\ WARNING: Failed to find the Docker container running docker-gen. All
41
+ # upstream (backend) application containers will appear to be
42
+ # unreachable. Try removing the -only-exposed and -only-published
43
+ # arguments to docker-gen if you pass either of those. See
44
+ # <https://github.com/nginx-proxy/docker-gen/issues/458>.
45
+ {{- end }}
46
+
47
+ {{- /*
48
+ * Template used as a function to get a container's IP address. This
49
+ * template only outputs debug comments; the IP address is "returned" by
50
+ * storing the value in the provided dot dict.
51
+ *
52
+ * The provided dot dict is expected to have the following entries:
53
+ * - "globals": Global values.
54
+ * - "container": The container's RuntimeContainer struct.
55
+ *
56
+ * The return value will be added to the dot dict with key "ip".
57
+ */}}
58
+ {{- define "container_ip" }}
59
+ {{- $ip := "" }}
60
+ # networks:
61
+ {{- range sortObjectsByKeysAsc $.container.Networks "Name" }}
62
+ {{- /*
63
+ * TODO: Only ignore the "ingress" network for Swarm tasks (in case
64
+ * the user is not using Swarm mode and names a network "ingress").
65
+ */}}
66
+ {{- if eq .Name "ingress" }}
67
+ # {{ .Name }} (ignored)
68
+ {{- continue }}
69
+ {{- end }}
70
+ {{- if and (not (index $.globals.networks .Name)) (not $.globals.networks.host) }}
71
+ # {{ .Name }} (unreachable)
72
+ {{- continue }}
73
+ {{- end }}
74
+ {{- /*
75
+ * Do not emit multiple `server` directives for this container if it
76
+ * is reachable over multiple networks. This avoids accidentally
77
+ * inflating the effective round-robin weight of a server due to the
78
+ * redundant upstream addresses that nginx sees as belonging to
79
+ * distinct servers.
80
+ */}}
81
+ {{- if $ip }}
82
+ # {{ .Name }} (ignored; reachable but redundant)
83
+ {{- continue }}
84
+ {{- end }}
85
+ # {{ .Name }} (reachable)
86
+ {{- if and . .IP }}
87
+ {{- $ip = .IP }}
88
+ {{- else }}
89
+ # /!\ No IP for this network!
90
+ {{- end }}
91
+ {{- else }}
92
+ # (none)
93
+ {{- end }}
94
+ # IP address: {{ if $ip }}{{ $ip }}{{ else }}(none usable){{ end }}
95
+ {{- $_ := set $ "ip" $ip }}
96
+ {{- end }}
97
+
98
+ {{- /*
99
+ * Template used as a function to get the port of the server in the given
100
+ * container. This template only outputs debug comments; the port is
101
+ * "returned" by storing the value in the provided dot dict.
102
+ *
103
+ * The provided dot dict is expected to have the following entries:
104
+ * - "container": The container's RuntimeContainer struct.
105
+ *
106
+ * The return value will be added to the dot dict with key "port".
107
+ */}}
108
+ {{- define "container_port" }}
109
+ {{- /* If only 1 port exposed, use that as a default, else 80. */}}
110
+ # exposed ports:{{ range sortObjectsByKeysAsc $.container.Addresses "Port" }} {{ .Port }}/{{ .Proto }}{{ else }} (none){{ end }}
111
+ {{- $default_port := when (eq (len $.container.Addresses) 1) (first $.container.Addresses).Port "80" }}
112
+ # default port: {{ $default_port }}
113
+ {{- $port := or $.container.Env.VIRTUAL_PORT $default_port }}
114
+ # using port: {{ $port }}
115
+ {{- $addr_obj := where $.container.Addresses "Port" $port | first }}
116
+ {{- if and $addr_obj $addr_obj.HostPort }}
117
+ # /!\ WARNING: Virtual port published on host. Clients
118
+ # might be able to bypass nginx-proxy and
119
+ # access the container's server directly.
120
+ {{- end }}
121
+ {{- $_ := set $ "port" $port }}
122
+ {{- end }}
123
+
124
+ {{- define "ssl_policy" }}
125
+ {{- if eq .ssl_policy "Mozilla-Modern" }}
126
+ ssl_protocols TLSv1.3;
127
+ {{- /*
128
+ * nginx currently lacks ability to choose ciphers in TLS 1.3 in
129
+ * configuration; see https://trac.nginx.org/nginx/ticket/1529. A
130
+ * possible workaround can be modify /etc/ssl/openssl.cnf to change
131
+ * it globally (see
132
+ * https://trac.nginx.org/nginx/ticket/1529#comment:12). Explicitly
133
+ * set ngnix default value in order to allow single servers to
134
+ * override the global http value.
135
+ */}}
136
+ ssl_ciphers HIGH:!aNULL:!MD5;
137
+ ssl_prefer_server_ciphers off;
138
+ {{- else if eq .ssl_policy "Mozilla-Intermediate" }}
139
+ ssl_protocols TLSv1.2 TLSv1.3;
140
+ 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';
141
+ ssl_prefer_server_ciphers off;
142
+ {{- else if eq .ssl_policy "Mozilla-Old" }}
143
+ ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
144
+ 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';
145
+ ssl_prefer_server_ciphers on;
146
+ {{- else if eq .ssl_policy "AWS-TLS-1-2-2017-01" }}
147
+ ssl_protocols TLSv1.2 TLSv1.3;
148
+ 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';
149
+ ssl_prefer_server_ciphers on;
150
+ {{- else if eq .ssl_policy "AWS-TLS-1-1-2017-01" }}
151
+ ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
152
+ 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';
153
+ ssl_prefer_server_ciphers on;
154
+ {{- else if eq .ssl_policy "AWS-2016-08" }}
155
+ ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
156
+ 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';
157
+ ssl_prefer_server_ciphers on;
158
+ {{- else if eq .ssl_policy "AWS-2015-05" }}
159
+ ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
160
+ 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';
161
+ ssl_prefer_server_ciphers on;
162
+ {{- else if eq .ssl_policy "AWS-2015-03" }}
163
+ ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
164
+ 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';
165
+ ssl_prefer_server_ciphers on;
166
+ {{- else if eq .ssl_policy "AWS-2015-02" }}
167
+ ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
168
+ 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';
169
+ ssl_prefer_server_ciphers on;
170
+ {{- end }}
171
+ {{- end }}
172
+
173
+ {{- define "location" }}
174
+ {{- $override := printf "/etc/nginx/vhost.d/%s_%s_location_override" .Host (sha1 .Path) }}
175
+ {{- if and (eq .Path "/") (not (exists $override)) }}
176
+ {{- $override = printf "/etc/nginx/vhost.d/%s_location_override" .Host }}
177
+ {{- end }}
178
+ {{- if exists $override }}
179
+ include {{ $override }};
180
+ {{- else }}
181
+ {{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
182
+ location {{ .Path }} {
183
+ {{- if eq .NetworkTag "internal" }}
184
+ # Only allow traffic from internal clients
185
+ include /etc/nginx/network_internal.conf;
186
+ {{- end }}
187
+
188
+ {{- if eq .Proto "uwsgi" }}
189
+ include uwsgi_params;
190
+ uwsgi_pass {{ trim .Proto }}://{{ trim .Upstream }};
191
+ {{- else if eq .Proto "fastcgi" }}
192
+ root {{ trim .VhostRoot }};
193
+ include fastcgi_params;
194
+ fastcgi_pass {{ trim .Upstream }};
195
+ {{- if $keepalive }}
196
+ fastcgi_keep_conn on;
197
+ {{- end }}
198
+ {{- else if eq .Proto "grpc" }}
199
+ grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
200
+ {{- else }}
201
+ proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
202
+ set $upstream_keepalive {{ if $keepalive }}true{{ else }}false{{ end }};
203
+ {{- end }}
204
+
205
+ {{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
206
+ auth_basic "Restricted {{ .Host }}";
207
+ auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
208
+ {{- end }}
209
+
210
+ {{- if (exists (printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) )) }}
211
+ include {{ printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) }};
212
+ {{- else if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
213
+ include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
214
+ {{- else if (exists "/etc/nginx/vhost.d/default_location") }}
215
+ include /etc/nginx/vhost.d/default_location;
216
+ {{- end }}
217
+ }
218
+ {{- end }}
219
+ {{- end }}
220
+
221
+ {{- define "upstream" }}
222
+ upstream {{ .Upstream }} {
223
+ {{- $server_found := false }}
224
+ {{- $loadbalance := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.loadbalance")) }}
225
+ {{- if $loadbalance }}
226
+ # From the container's loadbalance label:
227
+ {{ $loadbalance }}
228
+ {{- end }}
229
+ {{- range $container := .Containers }}
230
+ # Container: {{ $container.Name }}
231
+ {{- $args := dict "globals" $.globals "container" $container }}
232
+ {{- template "container_ip" $args }}
233
+ {{- $ip := $args.ip }}
234
+ {{- $args := dict "container" $container }}
235
+ {{- template "container_port" $args }}
236
+ {{- $port := $args.port }}
237
+ {{- if $ip }}
238
+ {{- $server_found = true }}
239
+ server {{ $ip }}:{{ $port }};
240
+ {{- end }}
241
+ {{- end }}
242
+ {{- /* nginx-proxy/nginx-proxy#1105 */}}
243
+ {{- if not $server_found }}
244
+ # Fallback entry
245
+ server 127.0.0.1 down;
246
+ {{- end }}
247
+ {{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
248
+ {{- if $keepalive }}
249
+ keepalive {{ $keepalive }};
250
+ {{- end }}
251
+ }
252
+ {{- end }}
253
+
254
+ # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
255
+ # scheme used to connect to this server
256
+ map $http_x_forwarded_proto $proxy_x_forwarded_proto {
257
+ default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
258
+ '' $scheme;
259
+ }
260
+
261
+ map $http_x_forwarded_host $proxy_x_forwarded_host {
262
+ default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$http_host{{ end }};
263
+ '' $http_host;
264
+ }
265
+
266
+ # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
267
+ # server port the client connected to
268
+ map $http_x_forwarded_port $proxy_x_forwarded_port {
269
+ default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_port{{ else }}$server_port{{ end }};
270
+ '' $server_port;
271
+ }
272
+
273
+ # If the request from the downstream client has an "Upgrade:" header (set to any
274
+ # non-empty value), pass "Connection: upgrade" to the upstream (backend) server.
275
+ # Otherwise, the value for the "Connection" header depends on whether the user
276
+ # has enabled keepalive to the upstream server.
277
+ map $http_upgrade $proxy_connection {
278
+ default upgrade;
279
+ '' $proxy_connection_noupgrade;
280
+ }
281
+ map $upstream_keepalive $proxy_connection_noupgrade {
282
+ # Preserve nginx's default behavior (send "Connection: close").
283
+ default close;
284
+ # Use an empty string to cancel nginx's default behavior.
285
+ true '';
286
+ }
287
+ # Abuse the map directive (see <https://stackoverflow.com/q/14433309>) to ensure
288
+ # that $upstream_keepalive is always defined. This is necessary because:
289
+ # - The $proxy_connection variable is indirectly derived from
290
+ # $upstream_keepalive, so $upstream_keepalive must be defined whenever
291
+ # $proxy_connection is resolved.
292
+ # - The $proxy_connection variable is used in a proxy_set_header directive in
293
+ # the http block, so it is always fully resolved for every request -- even
294
+ # those where proxy_pass is not used (e.g., unknown virtual host).
295
+ map "" $upstream_keepalive {
296
+ # The value here should not matter because it should always be overridden in
297
+ # a location block (see the "location" template) for all requests where the
298
+ # value actually matters.
299
+ default false;
300
+ }
301
+
302
+ # Apply fix for very long server names
303
+ server_names_hash_bucket_size 128;
304
+
305
+ # Default dhparam
306
+ {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
307
+ ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
308
+ {{- end }}
309
+
310
+ # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
311
+ map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
312
+ default off;
313
+ https on;
314
+ }
315
+
316
+ gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
317
+
318
+ log_format vhost '{{ or $globals.Env.LOG_FORMAT "$host $remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$upstream_addr\"" }}';
319
+
320
+ access_log off;
321
+
322
+ {{- template "ssl_policy" (dict "ssl_policy" $globals.ssl_policy) }}
323
+ error_log /dev/stderr;
324
+
325
+ {{- if $globals.Env.RESOLVERS }}
326
+ resolver {{ $globals.Env.RESOLVERS }};
327
+ {{- end }}
328
+
329
+ {{- if (exists "/etc/nginx/proxy.conf") }}
330
+ include /etc/nginx/proxy.conf;
331
+ {{- else }}
332
+ # HTTP 1.1 support
333
+ proxy_http_version 1.1;
334
+ proxy_buffering off;
335
+ proxy_set_header Host $http_host;
336
+ proxy_set_header Upgrade $http_upgrade;
337
+ proxy_set_header Connection $proxy_connection;
338
+ proxy_set_header X-Real-IP $remote_addr;
339
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
340
+ proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
341
+ proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
342
+ proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
343
+ proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
344
+ proxy_set_header X-Original-URI $request_uri;
345
+
346
+ # Mitigate httpoxy attack (see README for details)
347
+ proxy_set_header Proxy "";
348
+ {{- end }}
349
+
350
+ {{- /*
351
+ * Precompute some information about each vhost. This is done early because
352
+ * the creation of fallback servers depends on DEFAULT_HOST, HTTPS_METHOD,
353
+ * and whether there are any missing certs.
354
+ */}}
355
+ {{- range $vhost, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
356
+ {{- $vhost := trim $vhost }}
357
+ {{- if not $vhost }}
358
+ {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
359
+ {{- continue }}
360
+ {{- end }}
361
+ {{- $certName := first (groupByKeys $containers "Env.CERT_NAME") }}
362
+ {{- $vhostCert := closest (dir "/etc/nginx/certs") (printf "%s.crt" $vhost) }}
363
+ {{- $vhostCert = trimSuffix ".crt" $vhostCert }}
364
+ {{- $vhostCert = trimSuffix ".key" $vhostCert }}
365
+ {{- $cert := or $certName $vhostCert }}
366
+ {{- $cert_ok := and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert)) }}
367
+ {{- $default := eq $globals.Env.DEFAULT_HOST $vhost }}
368
+ {{- $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) $globals.Env.HTTPS_METHOD "redirect" }}
369
+ {{- $_ := set $globals.vhosts $vhost (dict "cert" $cert "cert_ok" $cert_ok "containers" $containers "default" $default "https_method" $https_method) }}
370
+ {{- end }}
371
+
372
+ {{- /*
373
+ * If needed, create a catch-all fallback server to send an error code to
374
+ * clients that request something from an unknown vhost.
375
+ */}}
376
+ {{- block "fallback_server" $globals }}
377
+ {{- $globals := . }}
378
+ {{- $http_exists := false }}
379
+ {{- $https_exists := false }}
380
+ {{- $default_http_exists := false }}
381
+ {{- $default_https_exists := false }}
382
+ {{- range $vhost := $globals.vhosts }}
383
+ {{- $http := or (ne $vhost.https_method "nohttp") (not $vhost.cert_ok) }}
384
+ {{- $https := ne $vhost.https_method "nohttps" }}
385
+ {{- $http_exists = or $http_exists $http }}
386
+ {{- $https_exists = or $https_exists $https }}
387
+ {{- $default_http_exists = or $default_http_exists (and $http $vhost.default) }}
388
+ {{- $default_https_exists = or $default_https_exists (and $https $vhost.default) }}
389
+ {{- end }}
390
+ {{- $fallback_http := and $http_exists (not $default_http_exists) }}
391
+ {{- $fallback_https := and $https_exists (not $default_https_exists) }}
392
+ {{- /*
393
+ * If there are no vhosts at all, create fallbacks for both plain http
394
+ * and https so that clients get something more useful than a connection
395
+ * refused error.
396
+ */}}
397
+ {{- if and (not $http_exists) (not $https_exists) }}
398
+ {{- $fallback_http = true }}
399
+ {{- $fallback_https = true }}
400
+ {{- end }}
401
+ {{- if or $fallback_http $fallback_https }}
402
+ server {
403
+ server_name _; # This is just an invalid value which will never trigger on a real hostname.
404
+ server_tokens off;
405
+ {{- if $fallback_http }}
406
+ listen {{ $globals.external_http_port }} default_server;
407
+ {{- if $globals.enable_ipv6 }}
408
+ listen [::]:{{ $globals.external_http_port }} default_server;
409
+ {{- end }}
410
+ {{- end }}
411
+ {{- if $fallback_https }}
412
+ listen {{ $globals.external_https_port }} ssl http2 default_server;
413
+ {{- if $globals.enable_ipv6 }}
414
+ listen [::]:{{ $globals.external_https_port }} ssl http2 default_server;
415
+ {{- end }}
416
+ {{- end }}
417
+ {{ $globals.access_log }}
418
+ {{- if $globals.default_cert_ok }}
419
+ ssl_session_cache shared:SSL:50m;
420
+ ssl_session_tickets off;
421
+ ssl_certificate /etc/nginx/certs/default.crt;
422
+ ssl_certificate_key /etc/nginx/certs/default.key;
423
+ {{- else }}
424
+ # No default.crt certificate found for this vhost, so force nginx to emit a
425
+ # TLS error if the client connects via https.
426
+ {{- /* See the comment in the main `server` directive for rationale. */}}
427
+ ssl_ciphers aNULL;
428
+ set $empty "";
429
+ ssl_certificate data:$empty;
430
+ ssl_certificate_key data:$empty;
431
+ if ($https) {
432
+ return 444;
433
+ }
434
+ {{- end }}
435
+ return 503;
436
+ }
437
+ {{- end }}
438
+ {{- end }}
439
+
440
+ {{- range $host, $vhost := $globals.vhosts }}
441
+ {{- $cert := $vhost.cert }}
442
+ {{- $cert_ok := $vhost.cert_ok }}
443
+ {{- $containers := $vhost.containers }}
444
+ {{- $default_server := when $vhost.default "default_server" "" }}
445
+ {{- $https_method := $vhost.https_method }}
446
+
447
+ {{- $is_regexp := hasPrefix "~" $host }}
448
+ {{- $upstream_name := when (or $is_regexp $globals.sha1_upstream_name) (sha1 $host) $host }}
449
+
450
+ {{- $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
451
+ {{- $nPaths := len $paths }}
452
+ {{- if eq $nPaths 0 }}
453
+ {{- $paths = dict "/" $containers }}
454
+ {{- end }}
455
+
456
+ {{- range $path, $containers := $paths }}
457
+ {{- $upstream := $upstream_name }}
458
+ {{- if gt $nPaths 0 }}
459
+ {{- $sum := sha1 $path }}
460
+ {{- $upstream = printf "%s-%s" $upstream $sum }}
461
+ {{- end }}
462
+ # {{ $host }}{{ $path }}
463
+ {{ template "upstream" (dict "globals" $globals "Upstream" $upstream "Containers" $containers) }}
464
+ {{- end }}
465
+
466
+ {{- /*
467
+ * Get the SERVER_TOKENS defined by containers w/ the same vhost,
468
+ * falling back to "".
469
+ */}}
470
+ {{- $server_tokens := trim (or (first (groupByKeys $containers "Env.SERVER_TOKENS")) "") }}
471
+
472
+ {{- /*
473
+ * Get the SSL_POLICY defined by containers w/ the same vhost, falling
474
+ * back to empty string (use default).
475
+ */}}
476
+ {{- $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }}
477
+
478
+ {{- /*
479
+ * Get the HSTS defined by containers w/ the same vhost, falling back to
480
+ * "max-age=31536000".
481
+ */}}
482
+ {{- $hsts := or (first (groupByKeys $containers "Env.HSTS")) (or $globals.Env.HSTS "max-age=31536000") }}
483
+
484
+ {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
485
+ {{- $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
486
+
487
+ {{- if and $cert_ok (eq $https_method "redirect") }}
488
+ server {
489
+ server_name {{ $host }};
490
+ {{- if $server_tokens }}
491
+ server_tokens {{ $server_tokens }};
492
+ {{- end }}
493
+ listen {{ $globals.external_http_port }} {{ $default_server }};
494
+ {{- if $globals.enable_ipv6 }}
495
+ listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
496
+ {{- end }}
497
+ {{ $globals.access_log }}
498
+
499
+ # Do not HTTPS redirect Let's Encrypt ACME challenge
500
+ location ^~ /.well-known/acme-challenge/ {
501
+ auth_basic off;
502
+ auth_request off;
503
+ allow all;
504
+ root /usr/share/nginx/html;
505
+ try_files $uri =404;
506
+ break;
507
+ }
508
+
509
+ location / {
510
+ {{- if eq $globals.external_https_port "443" }}
511
+ return 301 https://$host$request_uri;
512
+ {{- else }}
513
+ return 301 https://$host:{{ $globals.external_https_port }}$request_uri;
514
+ {{- end }}
515
+ }
516
+ }
517
+ {{- end }}
518
+
519
+ server {
520
+ server_name {{ $host }};
521
+ {{- if $server_tokens }}
522
+ server_tokens {{ $server_tokens }};
523
+ {{- end }}
524
+ {{ $globals.access_log }}
525
+ {{- if or (eq $https_method "nohttps") (not $cert_ok) (eq $https_method "noredirect") }}
526
+ listen {{ $globals.external_http_port }} {{ $default_server }};
527
+ {{- if $globals.enable_ipv6 }}
528
+ listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
529
+ {{- end }}
530
+ {{- end }}
531
+ {{- if ne $https_method "nohttps" }}
532
+ listen {{ $globals.external_https_port }} ssl http2 {{ $default_server }};
533
+ {{- if $globals.enable_ipv6 }}
534
+ listen [::]:{{ $globals.external_https_port }} ssl http2 {{ $default_server }};
535
+ {{- end }}
536
+
537
+ {{- if $cert_ok }}
538
+ {{- template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
539
+
540
+ ssl_session_timeout 5m;
541
+ ssl_session_cache shared:SSL:50m;
542
+ ssl_session_tickets off;
543
+
544
+ ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
545
+ ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
546
+
547
+ {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
548
+ ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
549
+ {{- end }}
550
+
551
+ {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }}
552
+ ssl_stapling on;
553
+ ssl_stapling_verify on;
554
+ ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }};
555
+ {{- end }}
556
+
557
+ {{- if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }}
558
+ set $sts_header "";
559
+ if ($https) {
560
+ set $sts_header "{{ trim $hsts }}";
561
+ }
562
+ add_header Strict-Transport-Security $sts_header always;
563
+ {{- end }}
564
+ {{- else if $globals.default_cert_ok }}
565
+ # No certificate found for this vhost, so use the default certificate and
566
+ # return an error code if the user connects via https.
567
+ ssl_certificate /etc/nginx/certs/default.crt;
568
+ ssl_certificate_key /etc/nginx/certs/default.key;
569
+ if ($https) {
570
+ return 500;
571
+ }
572
+ {{- else }}
573
+ # No certificate found for this vhost, so force nginx to emit a TLS error if
574
+ # the client connects via https.
575
+ {{- /*
576
+ * The alternative is to not provide an https server for this
577
+ * vhost, which would either cause the user to see the wrong
578
+ * vhost (if there is another vhost with a certificate) or a
579
+ * connection refused error (if there is no other vhost with a
580
+ * certificate). A TLS error is easier to troubleshoot, and is
581
+ * safer than serving the wrong vhost. Also see
582
+ * <https://serverfault.com/a/1044022>.
583
+ */}}
584
+ ssl_ciphers aNULL;
585
+ set $empty "";
586
+ ssl_certificate data:$empty;
587
+ ssl_certificate_key data:$empty;
588
+ if ($https) {
589
+ return 444;
590
+ }
591
+ {{- end }}
592
+ {{- end }}
593
+
594
+ {{- if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
595
+ include {{ printf "/etc/nginx/vhost.d/%s" $host }};
596
+ {{- else if (exists "/etc/nginx/vhost.d/default") }}
597
+ include /etc/nginx/vhost.d/default;
598
+ {{- end }}
599
+
600
+ {{- range $path, $containers := $paths }}
601
+ {{- /*
602
+ * Get the VIRTUAL_PROTO defined by containers w/ the same
603
+ * vhost-vpath, falling back to "http".
604
+ */}}
605
+ {{- $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
606
+
607
+ {{- /*
608
+ * Get the NETWORK_ACCESS defined by containers w/ the same vhost,
609
+ * falling back to "external".
610
+ */}}
611
+ {{- $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
612
+ {{- $upstream := $upstream_name }}
613
+ {{- $dest := "" }}
614
+ {{- if gt $nPaths 0 }}
615
+ {{- $sum := sha1 $path }}
616
+ {{- $upstream = printf "%s-%s" $upstream $sum }}
617
+ {{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
618
+ {{- end }}
619
+ {{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag "Containers" $containers) }}
620
+ {{- end }}
621
+ {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}}
622
+ location / {
623
+ return {{ $globals.default_root_response }};
624
+ }
625
+ {{- end }}
626
+ }
627
+ {{- end }}