forked from nginx-proxy/nginx-proxy
-
Notifications
You must be signed in to change notification settings - Fork 4
/
haproxy.tmpl
256 lines (228 loc) · 11.2 KB
/
haproxy.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
global
log /dev/log local0
ssl-default-bind-options ssl-min-ver TLSv1.0
ssl-default-bind-ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:!DSS'
tune.ssl.default-dh-param 2048
crt-base /etc/haproxy/certs
ca-base /etc/ssl/certs
{{ if (exists "/etc/haproxy/certs/dhparam.pem") }}
# Default dhparam
ssl-dh-param-file /etc/haproxy/certs/dhparam.pem
{{ end }}
{{ if $.Env.ENABLE_ACME }}
lua-load /etc/haproxy/acme-webroot.lua
{{ end }}
defaults
mode http
log global
option httplog
option dontlognull
option redispatch
option forwardfor
retries 3
balance roundrobin
backlog 4096
timeout client 1m
timeout client-fin 10s
timeout connect 3s
timeout server 1m
timeout server-fin 5s
timeout tunnel 40m
timeout http-request 5s
timeout http-keep-alive 15m
timeout queue 15s
timeout tarpit 30s
default-server inter 3s rise 2 fall 3
{{ $CurrentContainer := whereLabelExists $ "ha-proxy.haproxy" | first }}
{{ define "backend" }}
{{/* Get the proto defined by container */}}
{{ $proto := trim (or (index .Container.Labels "ha-proxy.proto") "http") }}
{{ $bk_ssl := when (eq $proto "https") "ssl" "" }}
{{ $bk_opt := when .HA (printf "cookie %s check" .Container.Name) "" }}
{{ if .Address }}
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
{{ if and .Container.Node.ID .Address.HostPort }}
# {{ .Container.Node.Name }}/{{ .Container.Name }}
server {{.Container.Name}} {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }} {{ $bk_opt }} {{ $bk_ssl }}
{{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
{{ else if .Network }}
# {{ .Container.Name }}
server {{ .Container.Name }} {{ .Network.IP }}:{{ .Address.Port }} {{ $bk_opt }} {{ $bk_ssl }}
{{ end }}
{{ else if .Network }}
# {{ .Container.Name }}
{{ if .Network.IP }}
server {{ .Container.Name }} {{ .Network.IP }}:80 disabled
{{ end }}
{{ end }}
{{ end }}
{{ define "frontend" }}
{{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "nohttps" */}}
{{ $https_method := or (first (keys (groupByLabel .Containers "ha-proxy.https_method"))) "nohttps" }}
{{/* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000" */}}
{{ $hsts := or (first (keys (groupByLabel .Containers "ha-proxy.hsts"))) "max-age=31536000" }}
{{ $is_https := ne $https_method "nohttps" }}
{{ $is_http := ne $https_method "nohttp" }}
{{/* Give a name to the host rules */}}
{{ $is_regexp := hasPrefix "~" .Host }}
{{ $is_suffix := hasPrefix "*" .Host }}
{{ $host_name := when $is_regexp (sha1 .Host) (when $is_suffix (replace .Host "*" "." 1) .Host) }}
{{/* Create host acl rules */}}
{{ if $is_regexp }}
{{ if $is_http }}
acl host_{{ $host_name }} hdr_reg(Host) -i {{ trimPrefix "~" .Host }}
{{ end }}
{{ if $is_https }}
acl host_{{ $host_name }}_sni ssl_fc_sni_reg -i {{ trimPrefix "~" .Host }}
{{ end }}
{{ else if $is_suffix }}
{{ if $is_http }}
acl host_{{ $host_name }} hdr_end(Host) -i {{ trimPrefix "*" .Host }}
{{ end }}
{{ if $is_https }}
acl host_{{ $host_name }}_sni ssl_fc_sni_end -i {{ trimPrefix "*" .Host }}
{{ end }}
{{ else }}
{{ if $is_http }}
acl host_{{ $host_name }} hdr(Host) -i {{ .Host }}
{{ end }}
{{ if $is_https }}
acl host_{{ $host_name }}_sni ssl_fc_sni -i {{ .Host }}
{{ end }}
{{ end }}
{{/* End acl rules */}}
{{ if $is_https }}
## https
use_backend bk_{{.Backend}} if https host_{{$host_name}}_sni
{{ end }}
{{ if and $is_https (eq $https_method "redirect") }}
## http redirect
redirect scheme https code 301 if http !acme_challenge host_{{$host_name}}
{{ else if or (not $is_https) (eq $https_method "noredirect") }}
## http
use_backend bk_{{.Backend}} if http host_{{$host_name}}
{{ end }}
{{ end }}
{{ range $hosts, $containers := groupByLabel $ "ha-proxy.host" }}
{{ $hosts := replace $hosts " " "" -1 }}
{{ $is_multi := gt (len (split $hosts ",")) 1 }}
{{ $is_regexp := hasPrefix "~" $hosts }}
{{ $is_suffix := hasPrefix "*" $hosts }}
{{ $host_name := when $is_regexp (sha1 $hosts) $hosts }}
{{ $host_name := when $is_suffix (replace $hosts "*" "." 1) $hosts }}
{{ $host_name := when $is_multi (sha1 $hosts) $host_name }}
# {{ $hosts }}
{{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}}
{{ $network_tag := or (first (keys (groupByLabel $containers "ha-proxy.network_access"))) "external" }}
{{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}}
{{ $https_method := or (first (keys (groupByLabel $containers "ha-proxy.https_method"))) "redirect" }}
{{/* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000" */}}
{{ $hsts := or (first (keys (groupByLabel $containers "ha-proxy.hsts"))) "max-age=31536000" }}
{{/* Get the CONNECTION defined by containers w/ the same vhost, falling back to "server-close" */}}
{{ $connection := or (first (keys (groupByLabel $containers "ha-proxy.connection"))) "server-close" }}
backend bk_{{ $host_name }}
{{ if eq $network_tag "internal" }}
# Only allow traffic from internal clients
acl internal_net_1 src 10.0.0.0/8
acl internal_net_2 src 192.168.0.0/16
acl internal_net_3 src 172.16.0.0/12
http-request deny unless LOCALHOST || internal_net_1 || internal_net_2 || internal_net_3
{{ end }}
http-request set-header X-Real-IP %[src]
http-request set-header X-Forwarded-Ssl off if !{ ssl_fc }
http-request set-header X-Forwarded-Ssl on if { ssl_fc }
http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
http-request set-header X-Forwarded-Proto https if { ssl_fc }
http-request set-header X-Forwarded-Port %[dst_port]
http-request del-header Proxy
{{ if (and (ne $https_method "noredirect") (ne $hsts "off")) }}
http-response set-header Strict-Transport-Security "{{ trim $hsts }}"
{{ end }}
{{ if (eq $connection "keep-alive") }}
# Keep-alive to backend service
option http-keep-alive
{{ else if (eq $connection "tunnel") }}
option http-tunnel
{{ else if (eq $connection "server-close") }}
option http-server-close
{{ else if (eq $connection "close") }}
option httpclose
{{ end }}
{{ $ha := gt (len $containers) 1 }}
{{ range $container := $containers }}
{{ range $knownNetwork := $CurrentContainer.Networks }} {{ range $containerNetwork := $container.Networks }}
{{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }}
## Can be connected with "{{ $containerNetwork.Name }}" network
{{/* If only 1 port exposed, use that */}}
{{ if eq (len $container.Addresses) 1 }}
{{ $address := index $container.Addresses 0 }}
{{ template "backend" (dict "Container" $container "Address" $address "Network" $containerNetwork "HA" $ha) }}
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
{{/* fixed host network */}}
{{ else if (eq $containerNetwork.Name "host") }}
{{ $port := coalesce (index $container.Labels "ha-proxy.port") "80" }}
{{ $address := dict "Port" $port }}
{{ $network := dict "IP" "127.0.0.1" }}
{{ template "backend" (dict "Container" $container "Address" $address "Network" $network "HA" $ha) }}
{{ else }}
{{ $port := coalesce (index $container.Labels "ha-proxy.port") "80" }}
{{ $address := where $container.Addresses "Port" $port | first }}
{{ template "backend" (dict "Container" $container "Address" $address "Network" $containerNetwork "HA" $ha) }}
{{ end }}
{{ end }}
{{ end }} {{ end }}
{{ end }}
{{ end }}
{{/* end range */}}
{{ $enable_ipv6 := eq (or ($.Env.ENABLE_IPV6) "") "true" }}
frontend http
mode http
{{ $has_https := ne 0 (len (whereLabelValueMatches $ "ha-proxy.https_method" "^(redirect|noredirect|nohttp)$")) }}
{{ if $enable_ipv6 }}
bind :::80 v4v6 tfo defer-accept
{{ if $has_https }}
bind :::443 v4v6 tfo defer-accept ssl allow-0rtt alpn h2,http/1.1 npn h2,http/1.1 crt-list /etc/haproxy/certs.list
{{ end }}
{{ else }}
bind :80 tfo defer-accept
{{ if $has_https }}
bind :443 tfo defer-accept ssl allow-0rtt alpn h2,http/1.1 npn h2,http/1.1 crt-list /etc/haproxy/certs.list
{{ end }}
{{ end }}
# Only safe method supported in 0rtt
acl safe_method method GET HEADER CONNECT OPTIONS TRACE
http-request deny deny_status 425 if { ssl_fc_has_early } !safe_method
# ACME service
acl acme_challenge path_beg /.well-known/acme-challenge/
{{ if $.Env.ENABLE_ACME }}
http-request use-service lua.acme-http01 if METH_GET acme_challenge
{{ end }}
acl http ssl_fc,not
acl https ssl_fc
{{/* Get Default Host setting from environment */}}
{{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
{{ $default_host := trim $default_host }}
{{/* Per hosts setting */}}
{{ range $hosts, $containers := groupByLabel $ "ha-proxy.host" }}
{{/* Compute backend name */}}
{{ $hosts := replace $hosts " " "" -1 }}
{{ $hostss := split $hosts ","}}
{{ $is_multi := gt (len $hostss) 1 }}
{{ $is_regexp := hasPrefix "~" $hosts }}
{{ $is_suffix := hasPrefix "*" $hosts }}
{{ $backend_name := when $is_regexp (sha1 $hosts) $hosts }}
{{ $backend_name := when $is_suffix (replace $hosts "*" "." 1) $hosts }}
{{ $backend_name := when $is_multi (sha1 $hosts) $backend_name }}
{{/* Per hosts setting */}}
{{ range $host := $hostss }}
{{ $host := trim $host }}
# {{ $host }}
{{ if eq $host $default_host }}
## Default host
default_backend bk_{{ $backend_name }}
{{ end }}
{{ template "frontend" (dict "Containers" $containers "Host" $host "Backend" $backend_name) }}
{{ end }}
{{/* end host */}}
{{ end }}
{{/* end hosts, containers */}}