forked from luiselizondo/docker-varnish
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathparse
125 lines (100 loc) · 2.91 KB
/
parse
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
#!/bin/bash
################################################################
#
# Apache License 2.0
#
# This file will parse the Environment variables created by
# node containers linked to the varnish container
# and automatically create a default.vcl configuration file
#
# The configuration file will create a load balancer and add
# each of the node containers linked.
#
# Varnish is not configured to use the reverse proxy features
# that it has, it just a load balancer.
#
################################################################
# Get the Environment variables and save them in the variable envs
envs=`printenv`
# Remove the default.vcl file
echo "vcl 4.0;
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 60s;
.first_byte_timeout = 60s;
.between_bytes_timeout = 60s;
.max_connections = 800;
}
acl purge {
"127.0.0.1";
"localhost";
}
sub vcl_recv {
set req.grace = 2m;
# Set X-Forwarded-For header for logging in nginx
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;
# Remove has_js and CloudFlare/Google Analytics __* cookies and statcounter is_unique
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js|is_unique)=[^;]*", "");
# Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
# Either the admin pages or the login
if (req.url ~ "/wp-(login|admin|cron)") {
# Don't cache, pass to backend
return (pass);
}" > /etc/varnish/default.vcl
# Loop through all of our variables
for env in $envs
do
# separate the name of the variable from the value
IFS== read name value <<< "$env"
# if the variable has PORT_80_TCP_ADDR it means this is a
# variable created by a node container linked to the varnish
# container
if [[ $name == *PORT_80_TCP_ADDR* ]]; then
# create a backend for each node container found in the variables
cat >> /etc/varnish/default.vcl << EOF
backend ${name} {
.host = "${value}";
.port = "80";
.connect_timeout = 60s;
.first_byte_timeout = 60s;
.between_bytes_timeout = 60s;
.max_connections = 800;
}
EOF
fi
done
# once we have all the containers ready, we create the
# load balancer, since we're gonna loop again, we just
# create the first line
cat >> /etc/varnish/default.vcl << EOF
sub vcl_init {
new cluster1 = directors.round_robin();
EOF
# loop again to add each backend created
for env in $envs
do
IFS== read name value <<< "$env"
if [[ $name == *PORT_80_TCP_ADDR* ]]; then
# create each backend in the load balancer
cat >> /etc/varnish/default.vcl << EOF
cluster1.add.backend = (${name}, 1.0);
EOF
fi
done
# close the load balancer line
# and add the balancer as the req.backend
cat >> /etc/varnish/default.vcl << EOF
}
sub vcl_fetch {
if (req.url ~ "\.(css|js|png|gif|jpg)$") {
unset beresp.http.set-cookie;
set beresp.ttl = 66h;
}
}
sub vcl_recv {
set req.backend_hint = cluster1.backend();
}
EOF