Skip to content

Commit fb68e08

Browse files
committed
Merge pull request codekitchen#5 from trakos/feature/https_method
Added HTTPS_METHOD (redirect, noredirect, nohttp)
2 parents 84625c0 + ee22c33 commit fb68e08

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

nginx.tmpl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ upstream {{ .Host }} {
4949
{{ end }}
5050
}
5151

52+
{{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}}
53+
{{ $https_method := or (first (groupByKeys .Containers "Env.HTTPS_METHOD")) "redirect" }}
54+
5255
{{/* Get the first cert name defined by containers w/ the same vhost */}}
5356
{{ $certName := (first (groupByKeys .Containers "Env.CERT_NAME")) }}
5457

@@ -62,14 +65,18 @@ upstream {{ .Host }} {
6265
{{/* Use the cert specifid on the container or fallback to the best vhost match */}}
6366
{{ $cert := (coalesce $certName $vhostCert) }}
6467

65-
{{ if (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
68+
{{ $is_https := (and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
69+
70+
{{ if $is_https }}
6671

72+
{{ if eq $https_method "redirect" }}
6773
server {
6874
server_name {{ .Host }};
6975
listen 80;
7076
access_log /var/log/nginx/access.log vhost;
7177
return 301 https://{{ .Host }}$request_uri;
7278
}
79+
{{ end }}
7380

7481
server {
7582
server_name {{ .Host }};
@@ -112,7 +119,9 @@ server {
112119
}
113120
}
114121

115-
{{ else }}
122+
{{ end }}
123+
124+
{{ if or (not $is_https) (eq $https_method "noredirect") }}
116125

117126
server {
118127
server_name {{ .Host }};
@@ -138,6 +147,14 @@ server {
138147
{{ end }}
139148
}
140149
}
150+
{{ else if not (eq $https_method "nohttp") }}
151+
server {
152+
server_name {{ .Host }};
153+
listen 80;
154+
access_log /var/log/nginx/access.log vhost;
155+
return 503 "Unknown HTTPS_METHOD {{ $https_method }}";
156+
add_header Content-Type text/plain;
157+
}
141158
{{ end }}
142159
{{ end }}
143160

0 commit comments

Comments
 (0)