Skip to content

Commit c12040d

Browse files
author
Jeff
committed
init varnish Image
0 parents  commit c12040d

4 files changed

Lines changed: 192 additions & 0 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
.vscode
3+
node_modules
4+
npm-debug.log
5+
*.map
6+
**.swp

Dockerfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM studionone/apache-php:7.1
2+
3+
# Varnish is being installed from source because we need to VCL mod to allow for dynamic configurations
4+
RUN apt-get update && apt-get install -y autoconf \
5+
automake \
6+
autotools-dev \
7+
libedit-dev \
8+
libjemalloc-dev \
9+
libncurses-dev \
10+
libpcre3-dev \
11+
libtool \
12+
pkg-config \
13+
python-docutils \
14+
python-sphinx \
15+
&& cd /tmp \
16+
&& wget https://github.com/varnishcache/varnish-cache/archive/varnish-4.1.8.tar.gz \
17+
&& wget https://github.com/Dridi/libvmod-querystring/releases/download/v1.0.1/vmod-querystring-1.0.1.tar.gz \
18+
&& tar xfz varnish-4.1.8.tar.gz \
19+
&& rm -rf /tmp/varnish-4.1.8.tar.gz \
20+
&& tar xfz vmod-querystring-1.0.1.tar.gz \
21+
&& rm -rf /tmp/vmod-querystring-1.0.1.tar.gz \
22+
&& cd /tmp/varnish-cache-varnish-4.1.8 \
23+
&& sh autogen.sh \
24+
&& sh configure \
25+
&& make && make install \
26+
# Varnish querystring module
27+
&& cd /tmp/vmod-querystring-1.0.1 \
28+
&& sh configure \
29+
&& make \
30+
&& make install \
31+
&& rm -rf /tmp/varnish-cache-varnish-4.1.8 \
32+
&& rm -rf /tmp/vmod-querystring-1.0.1 \
33+
&& ldconfig \
34+
&& mkdir -p /etc/varnish
35+
36+
# Varnish defult configuration file
37+
COPY conf/default.vcl.m4 /opt/default.vcl.m4
38+
COPY conf/supervisor_conf.d/varnish.conf /etc/supervisor/conf.d/varnish.conf
39+
40+
# Expose port 80 for nginx-proxy to pick up. This is an expedia requirement
41+
EXPOSE 8080
42+

conf/default.vcl.m4

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
vcl 4.0;
2+
import querystring;
3+
4+
backend default {
5+
.host = "BACKEND_HOST";
6+
.port = "BACKEND_PORT";
7+
}
8+
9+
sub vcl_recv {
10+
11+
#Allow for cache to be by passed for dev
12+
if ( req.url ~ "^/(.*)\?no-cache" ) {
13+
return (pass);
14+
}
15+
16+
#unset all cookies regardless of what they are. We current don't need any cookies for explore
17+
unset req.http.cookie;
18+
}
19+
20+
# The data on which the hashing will take place
21+
sub vcl_hash {
22+
# Called after vcl_recv to create a hash value for the request. This is used as a key
23+
# to look up the object in Varnish.
24+
25+
hash_data(req.url);
26+
27+
if (req.http.host) {
28+
hash_data(req.http.host);
29+
} else {
30+
hash_data(server.ip);
31+
}
32+
33+
# hash cookies for requests that have them
34+
if (req.http.Cookie) {
35+
hash_data(req.http.Cookie);
36+
}
37+
}
38+
39+
sub vcl_hit {
40+
# Called when a cache lookup is successful.
41+
42+
if (obj.ttl >= 0s) {
43+
# A pure unadultered hit, deliver it
44+
return (deliver);
45+
46+
}
47+
48+
}
49+
50+
sub vcl_miss {
51+
# Called after a cache lookup if the requested document was not found in the cache. Its purpose
52+
# is to decide whether or not to attempt to retrieve the document from the backend, and which
53+
# backend to use.
54+
55+
return (fetch);
56+
}
57+
58+
59+
60+
sub vcl_backend_response {
61+
62+
# Pause ESI request and remove Surrogate-Control header
63+
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
64+
unset beresp.http.Surrogate-Control;
65+
set beresp.do_esi = true;
66+
}
67+
68+
# Enable cache for all static files
69+
# The same argument as the static caches from above: monitor your cache size, if you get data nuked out of it, consider giving up the static file cache.
70+
# Before you blindly enable this, have a read here: https://ma.ttias.be/stop-caching-static-files/
71+
if (bereq.url ~ "^[^?]*\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|otf|ogg|ogm|opus|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz|ttf|txt|txz|wav|webm|webp|woff|woff2|xls|xlsx|xml|xz|zip)(\?.*)?$") {
72+
unset beresp.http.set-cookie;
73+
}
74+
75+
# Large static files are delivered directly to the end-user without
76+
# waiting for Varnish to fully read the file first.
77+
# Varnish 4 fully supports Streaming, so use streaming here to avoid locking.
78+
if (bereq.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") {
79+
unset beresp.http.set-cookie;
80+
set beresp.do_stream = true; # Check memory usage it'll grow in fetch_chunksize blocks (128k by default) if the backend doesn't send a Content-Length header, so only enable it for big objects
81+
}
82+
83+
# Sometimes, a 301 or 302 redirect formed via Apache's mod_rewrite can mess with the HTTP port that is being passed along.
84+
# This often happens with simple rewrite rules in a scenario where Varnish runs on :80 and Apache on :8080 on the same box.
85+
# A redirect can then often redirect the end-user to a URL on :8080, where it should be :80.
86+
# This may need finetuning on your setup.
87+
#
88+
# To prevent accidental replace, we only filter the 301/302 redirects for now.
89+
if (beresp.status == 301 || beresp.status == 302) {
90+
set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+", "");
91+
}
92+
93+
# Set 2min cache if unset for static files
94+
if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
95+
set beresp.ttl = 120s; # Important, you shouldn't rely on this, SET YOUR HEADERS in the backend
96+
set beresp.uncacheable = true;
97+
return (deliver);
98+
}
99+
100+
# Don't cache 50x responses
101+
if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
102+
return (abandon);
103+
}
104+
105+
# Allow stale content, in case the backend goes down.
106+
# make Varnish keep all objects for 6 hours beyond their TTL
107+
set beresp.grace = 6h;
108+
109+
return (deliver);
110+
}
111+
112+
# The routine when we deliver the HTTP request to the user
113+
# Last chance to modify headers that are sent to the client
114+
sub vcl_deliver {
115+
# Called before a cached object is delivered to the client.
116+
117+
# Remove some headers: PHP version
118+
unset resp.http.X-Powered-By;
119+
120+
# Remove some headers: Apache version & OS
121+
unset resp.http.Server;
122+
unset resp.http.X-Drupal-Cache;
123+
unset resp.http.X-Varnish;
124+
unset resp.http.Via;
125+
unset resp.http.Link;
126+
unset resp.http.X-Generator;
127+
128+
return (deliver);
129+
}
130+
131+
132+
sub vcl_fini {
133+
# Called when VCL is discarded only after all requests have exited the VCL.
134+
# Typically used to clean up VMODs.
135+
136+
return (ok);
137+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[program:varnish]
2+
command=varnishd -F -f /etc/varnish/default.vcl -s malloc,100m -a 0.0.0.0:8080 -p http_req_hdr_len=16384 -p http_resp_hdr_len=16384 -p workspace_client=128k
3+
priority=998
4+
user=root
5+
autostart=true
6+
autorestart=true
7+
startretries=3

0 commit comments

Comments
 (0)