-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapache2.tmpl
More file actions
154 lines (132 loc) · 5.43 KB
/
Copy pathapache2.tmpl
File metadata and controls
154 lines (132 loc) · 5.43 KB
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
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule rewrite_module modules/mod_rewrite.so
{{ $topdomain := env "TOPDOMAIN" | default "localhost" }}
{{ $redirectProtocol := eq (env "REDIRECT_TO_SECURE") "true" | ternary "https" "http" }}
{{ with env "CUSTOM_CONFIG" }}
{{ . }}
{{ end }}
AllowEncodedSlashes NoDecode
ProxyPreserveHost On
{{ if eq (env "DEFAULT_REDIRECT_TO_TOPDOMAIN") "true" }}
<VirtualHost *:80>
Redirect / {{ $redirectProtocol }}://{{ $topdomain }}/
</VirtualHost>
{{ end }}
{{/* Only keep containers with exposed port */}}
{{ $exposedContainers := list }}
{{ range $container := $ }}
{{ range $address := $container.Addresses }}
{{ if $address.HostPort }}
{{ $exposedContainers = append $exposedContainers $container }}
{{ break }}
{{ end }}
{{ end }}
{{ end }}
{{/* Only keep containers with webproxy labels */}}
{{ $webproxyfiedContainers := list }}
{{ range $container := $exposedContainers }}
{{ range $label, $value := $container.Labels }}
{{ if hasPrefix "webproxy." $label }}
{{ $webproxyfiedContainers = append $webproxyfiedContainers $container }}
{{ break }}
{{ end }}
{{ end }}
{{ end }}
{{/* Group containers by subdomain */}}
{{ range $subdomain, $containers := groupByLabelWithDefault $webproxyfiedContainers "webproxy.subdomain" "" }}
{{/* Group containers by path (and sort them by path) */}}
{{ $containersByPath := dict }}
{{ range $container := $containers }}
{{ $path := index $container.Labels "webproxy.path" | default "" }}
{{ if not (index $containersByPath $path) }}
{{ $containersByPath = set $containersByPath $path (list $container) }}
{{ else }}
{{ $containersByPath = set $containersByPath $path (append (index $containersByPath $path) $container) }}
{{ end }}
{{ end }}
{{/* Create a host for each subdomain */}}
<VirtualHost *:80>
{{ if eq $subdomain "" }}
ServerName {{ $topdomain }}
{{ else }}
ServerName {{ $subdomain }}.{{ $topdomain }}
{{ end }}
{{ with index ($containers | first).Labels "webproxy.serveradmin" }}
ServerAdmin {{ . }}
{{ end }}
{{ range $path, $containers := $containersByPath }}
{{/* Create a proxy for each path */}}
<Proxy "balancer://http-{{ $path }}">
{{ $websockets := "" }}
{{ $affinityCookie := "" }}
{{ $custom := "" }}
{{ range $container := $containers }}
# Container {{ $container.Name }}
{{ $address := where $container.Addresses "Port" (index $container.Labels "webproxy.port") | default (sortObjectsByKeysAsc $container.Addresses "Port") | first }}
{{ $port := $address.HostPort }}
{{ $ip := ($container.Networks | first).Gateway }}
BalancerMember "http://{{ $ip }}:{{ $port }}"
{{ $websockets = index $container.Labels "webproxy.websockets" | default $websockets }}
{{ $affinityCookie = index $container.Labels "webproxy.affinitycookie" | default $affinityCookie }}
{{ $custom = index $container.Labels "webproxy.custom" | default $custom }}
{{ end }}
{{ with $affinityCookie }}
ProxySet stickysession={{ . }}
{{ end }}
</Proxy>
{{ if $websockets }}
<Proxy "balancer://ws-{{ $path }}">
{{ range $container := $containers }}
{{ if index $container.Labels "webproxy.websockets" }}
# Container {{ $container.Name }}
{{ $address := where $container.Addresses "Port" (index $container.Labels "webproxy.port") | default (sortObjectsByKeysAsc $container.Addresses "Port") | first }}
{{ $port := $address.HostPort }}
{{ $ip := ($container.Networks | first).Gateway }}
BalancerMember "ws://{{ $ip }}:{{ $port }}"
{{ end }}
{{ end }}
</Proxy>
{{ end }}
<Location "/{{ $path }}">
{{ if not (eq $path "") }}
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/{{ $path }}$
RewriteRule ^(.*)$ /{{ $path }}/ [R=301,L]
</Location>
<Location "/{{ $path }}/">
Header edit Location ^(/.*)$ "/{{ $path }}$1"
{{ end }}
{{ if $websockets }}
ProxyPass "balancer://dummy/"
RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule balancer://dummy/(.*) "balancer://ws-{{ $path }}/$1" [P,L]
RewriteRule balancer://dummy/(.*) "balancer://http-{{ $path }}/$1" [P,L]
ProxyPassReverse "balancer://ws-{{ $path }}/"
ProxyPassReverse "balancer://http-{{ $path }}/"
{{ else }}
ProxyPass "balancer://http-{{ $path }}/" nocanon
ProxyPassReverse "balancer://http-{{ $path }}/"
{{ end }}
{{ with $custom }}
{{ . | nindent 8 }}
{{ end }}
</Location>
{{ end }}
{{ if eq $subdomain "" }}
{{ with env "IGNORE_PATHS" }}
# Paths to ignore
{{ range $ignore_path := (split . ",") }}
<Location "/{{ $ignore_path }}">
ProxyPass !
</Location>
{{ end }}
{{ end }}
{{ end }}
</VirtualHost>
{{ end }}