Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit c767cc3

Browse files
committed
Varnish support
1 parent bac37c2 commit c767cc3

File tree

13 files changed

+231
-69
lines changed

13 files changed

+231
-69
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ install:
3333
- sudo apt-get update && sudo apt-get install -y socat
3434
# TODO Install sudo apt install nfs-kernel-server
3535
# - sudo apt install -y nfs-kernel-server
36+
- cat ./etc/helm/values.yaml
3637

3738
before_script:
3839
- sudo bash ./scripts/host/configure_ubuntu_travis.sh
3940
- cat ./tests/include/configuration.sh
4041
- cat ./scripts/host/k_rebuild_environment.sh
4142
- cat ./init_project.sh
4243
- cat ./etc/config.yaml.dist
43-
- cat ./etc/helm/values.yaml
4444
# - cat ./tests/_files/* # TODO: Cannot cat because of directory, move configs to a separate directory
4545
- cat ./scripts/host/get_path_to_php.sh
4646
- while sleep 540; do echo "=====[ $SECONDS seconds still running ]====="; done &

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The environment for Magento EE development is also configured.
5050
The [project initialization script](init_project.sh) configures a complete development environment:
5151

5252
<!-- 1. Adds some missing software on the host -->
53-
1. Configures all software necessary for Magento 2: Nginx, PHP 7.x, MySQL 5.6, Git, Composer, XDebug<!--, Rabbit MQ, Varnish-->
53+
1. Configures all software necessary for Magento 2: Nginx, PHP 7.x, MySQL 5.6, Git, Composer, XDebug, Rabbit MQ, Varnish
5454
1. Installs Magento 2 from Git repositories or Composer packages (can be configured via `checkout_source_from` option in [etc/config.yaml](etc/config.yaml.dist))
5555
<!-- 1. Configures PHP Storm project (partially at the moment)-->
5656
<!-- 1. Installs NodeJS, NPM, Grunt and Gulp for front end development
@@ -344,13 +344,11 @@ PHP version will be applied after "devbox reload".
344344

345345
### Activating Varnish
346346

347-
Not available yet.
348-
<!--Set `use_varnish: 1` to use varnish along with apache in [config.yaml](etc/config.yaml.dist). Changes will be applied on `m-reinstall`.
349-
It will use default file etc/magento2_default_varnish.vcl.dist generated from a Magento 2.1 instance. Magento 2.2+ supports additional Varnish features and you may need to provide custom version of VCL to enable them.
350-
Varnish Version: 4.1
347+
Use the following commands to enable/disable varnish <!--without reinstalling Magento-->: `m-varnish disable` or `m-varnish enable`.
351348

352-
Use the following commands to enable/disable varnish without reinstalling Magento: `m-varnish disable` or `m-varnish enable`.
353-
-->
349+
You can also set `use_varnish: 1` in [config.yaml](etc/config.yaml.dist) to use varnish. Changes will be applied on `init_project.sh -f`.
350+
351+
The VCL content can be found in [configmap.yaml](etc/helm/templates/configmap.yaml).
354352

355353
### Activating ElasticSearch
356354

etc/config.yaml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ repository_url:
1919
# git@github.com:magento/magento2ee.git OR custom one
2020
ee: ""
2121
additional_repositories:
22+
# [To apply changes: m-switch-to-ce -f OR m-switch-to-ee -f]
2223
# - "git@github.com:magento/magento2-sample-data.git"
2324
# - "git@github.com:magento/magento2-sample-data-ee.git"
2425
# - "git@github.com:magento/magento2b2b.git"

etc/helm/templates/configmap.yaml

Lines changed: 181 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ data:
1818
fastcgi_read_timeout 30000s;
1919
2020
upstream fastcgi_backend {
21-
server 127.0.0.1:9001;
21+
server 127.0.0.1:{{ .Values.global.monolith.service.phpFpmPort }};
2222
}
2323
2424
upstream fastcgi_backend_xdebug {
25-
server 127.0.0.1:9002;
25+
server 127.0.0.1:{{ .Values.global.monolith.service.phpFpmXdebugPort }};
2626
}
2727
2828
map $cookie_XDEBUG_SESSION $fastcgi_backend_name {
@@ -31,7 +31,7 @@ data:
3131
}
3232
3333
server {
34-
listen 8001;
34+
listen "{{ .Values.global.monolith.service.nginxPort }}";
3535
3636
server_name _;
3737
set $MAGE_ROOT {{.Values.global.monolith.volumeHostPath}}/magento;
@@ -213,3 +213,181 @@ data:
213213
}
214214
}
215215
}
216+
---
217+
kind: ConfigMap
218+
apiVersion: v1
219+
metadata:
220+
name: {{ template "magento.fullname" . }}-varnish-config
221+
data:
222+
varnish.vcl: |
223+
vcl 4.0;
224+
225+
import std;
226+
# The minimal Varnish version is 4.0
227+
# For SSL offloading, pass the following header in your proxy server or load balancer: 'X-Forwarded-Proto: https'
228+
229+
backend default {
230+
.host = "127.0.0.1";
231+
.port = "{{ .Values.global.monolith.service.nginxPort }}";
232+
}
233+
234+
acl purge {
235+
"localhost";
236+
"192.0.0.0"/8;
237+
"172.0.0.0"/8;
238+
}
239+
240+
sub vcl_recv {
241+
if (req.method == "PURGE") {
242+
if (client.ip !~ purge) {
243+
return (synth(405, "Method not allowed"));
244+
}
245+
if (!req.http.X-Magento-Tags-Pattern) {
246+
return (synth(400, "X-Magento-Tags-Pattern header required"));
247+
}
248+
ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
249+
return (synth(200, "Purged"));
250+
}
251+
252+
if (req.method != "GET" &&
253+
req.method != "HEAD" &&
254+
req.method != "PUT" &&
255+
req.method != "POST" &&
256+
req.method != "TRACE" &&
257+
req.method != "OPTIONS" &&
258+
req.method != "DELETE") {
259+
/* Non-RFC2616 or CONNECT which is weird. */
260+
return (pipe);
261+
}
262+
263+
# We only deal with GET and HEAD by default
264+
if (req.method != "GET" && req.method != "HEAD") {
265+
return (pass);
266+
}
267+
268+
# Bypass shopping cart, checkout and search requests
269+
if (req.url ~ "/checkout" || req.url ~ "/catalogsearch") {
270+
return (pass);
271+
}
272+
273+
# normalize url in case of leading HTTP scheme and domain
274+
set req.url = regsub(req.url, "^http[s]?://", "");
275+
276+
# collect all cookies
277+
std.collect(req.http.Cookie);
278+
279+
# Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
280+
if (req.http.Accept-Encoding) {
281+
if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
282+
# No point in compressing these
283+
unset req.http.Accept-Encoding;
284+
} elsif (req.http.Accept-Encoding ~ "gzip") {
285+
set req.http.Accept-Encoding = "gzip";
286+
} elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
287+
set req.http.Accept-Encoding = "deflate";
288+
} else {
289+
# unkown algorithm
290+
unset req.http.Accept-Encoding;
291+
}
292+
}
293+
294+
# Remove Google gclid parameters to minimize the cache objects
295+
set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA"
296+
set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar"
297+
set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"
298+
299+
# static files are always cacheable. remove SSL flag and cookie
300+
if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
301+
unset req.http.Https;
302+
unset req.http.X-Forwarded-Proto;
303+
unset req.http.Cookie;
304+
}
305+
306+
return (hash);
307+
}
308+
309+
sub vcl_hash {
310+
if (req.http.cookie ~ "X-Magento-Vary=") {
311+
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
312+
}
313+
314+
# For multi site configurations to not cache each other's content
315+
if (req.http.host) {
316+
hash_data(req.http.host);
317+
} else {
318+
hash_data(server.ip);
319+
}
320+
321+
# To make sure http users don't see ssl warning
322+
if (req.http.X-Forwarded-Proto) {
323+
hash_data(req.http.X-Forwarded-Proto);
324+
}
325+
326+
}
327+
328+
sub vcl_backend_response {
329+
if (beresp.http.content-type ~ "text") {
330+
set beresp.do_esi = true;
331+
}
332+
333+
if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
334+
set beresp.do_gzip = true;
335+
}
336+
337+
# cache only successfully responses and 404s
338+
if (beresp.status != 200 && beresp.status != 404) {
339+
set beresp.ttl = 0s;
340+
set beresp.uncacheable = true;
341+
return (deliver);
342+
} elsif (beresp.http.Cache-Control ~ "private") {
343+
set beresp.uncacheable = true;
344+
set beresp.ttl = 86400s;
345+
return (deliver);
346+
}
347+
348+
if (beresp.http.X-Magento-Debug) {
349+
set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
350+
}
351+
352+
# validate if we need to cache it and prevent from setting cookie
353+
# images, css and js are cacheable by default so we have to remove cookie also
354+
if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
355+
unset beresp.http.set-cookie;
356+
if (bereq.url !~ "\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)(\?|$)") {
357+
set beresp.http.Pragma = "no-cache";
358+
set beresp.http.Expires = "-1";
359+
set beresp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
360+
set beresp.grace = 1m;
361+
}
362+
}
363+
364+
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
365+
if (beresp.ttl <= 0s ||
366+
beresp.http.Surrogate-control ~ "no-store" ||
367+
(!beresp.http.Surrogate-Control && beresp.http.Vary == "*")) {
368+
# Mark as Hit-For-Pass for the next 2 minutes
369+
set beresp.ttl = 120s;
370+
set beresp.uncacheable = true;
371+
}
372+
return (deliver);
373+
}
374+
375+
sub vcl_deliver {
376+
if (resp.http.X-Magento-Debug) {
377+
if (resp.http.x-varnish ~ " ") {
378+
set resp.http.X-Magento-Cache-Debug = "HIT";
379+
} else {
380+
set resp.http.X-Magento-Cache-Debug = "MISS";
381+
}
382+
} else {
383+
unset resp.http.Age;
384+
}
385+
386+
unset resp.http.X-Magento-Debug;
387+
unset resp.http.X-Magento-Tags;
388+
unset resp.http.X-Powered-By;
389+
unset resp.http.Server;
390+
unset resp.http.X-Varnish;
391+
unset resp.http.Via;
392+
unset resp.http.Link;
393+
}

etc/helm/templates/magento2-deployment.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,24 @@ spec:
7777
- name: nginx-config-volume
7878
mountPath: /etc/nginx/nginx.conf
7979
subPath: nginx.conf
80+
- name: varnish
81+
image: million12/varnish
82+
env:
83+
- name: VCL_CONFIG
84+
value: /etc/varnish/magento.vcl
85+
- name: VARNISHD_PARAMS
86+
value: "-a 0.0.0.0:{{ .Values.global.monolith.service.varnishPort }}"
87+
volumeMounts:
88+
- name: varnish-config-volume
89+
mountPath: /etc/varnish/magento.vcl
90+
subPath: varnish.vcl
8091
volumes:
8192
- name: nginx-config-volume
8293
configMap:
8394
name: {{ template "magento.fullname" . }}-nginx-config
95+
- name: varnish-config-volume
96+
configMap:
97+
name: {{ template "magento.fullname" . }}-varnish-config
8498
- name: code
8599
{{- if .Values.global.persistence.nfs.enabled }}
86100
persistentVolumeClaim:

etc/helm/templates/service.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ spec:
1212
type: {{ .Values.global.monolith.service.type }}
1313
ports:
1414
- port: {{ .Values.global.monolith.service.port }}
15+
targetPort: {{ if .Values.global.caching.varnish.enabled }}{{ .Values.global.monolith.service.varnishPort }}{{ else }}{{ .Values.global.monolith.service.nginxPort }}{{ end }}
1516
nodePort: {{ .Values.global.monolith.service.nodePort }}
1617
selector:
1718
app.kubernetes.io/name: {{ include "magento.name" . }}

etc/helm/values.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ monolith:
2626
imageTag: 7.1-fpm
2727
imagePullPolicy: Always
2828
global:
29+
caching:
30+
varnish:
31+
enabled: true # This value is overridden via <devbox_project>/etc/config.yaml
2932
checkout:
3033
enabled: false
3134
db:
@@ -38,7 +41,11 @@ global:
3841
service:
3942
type: NodePort
4043
nodePort: 30301
41-
port: 8001
44+
port: 8050
45+
nginxPort: 8001
46+
phpFpmPort: 9001
47+
phpFpmXdebugPort: 9002
48+
varnishPort: 6081
4249
mysqlHost: magento-mysql
4350
volumeHostPath: /Users/user/Projects/magento2-kubernetes-for-developers
4451
persistence:

k-ssh-varnish

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
cd "$(dirname "${BASH_SOURCE[0]}")" && devbox_dir=$PWD
6+
7+
source "${devbox_dir}/scripts/functions.sh"
8+
9+
loginToPodContainer "$(getMagento2PodId)" "varnish"

m-varnish

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ if [[ ${debug_devbox_project} -eq 1 ]]; then
1414
set -x
1515
fi
1616

17+
arguments=$@
1718
cd "${devbox_dir}"
18-
devbox ssh -c "bash /devbox/scripts/guest/m-varnish ${@}" 2> >(logError)
19+
executeInMagento2Container "${devbox_dir}/scripts/guest/m-varnish" -- ${arguments} 2> >(logError)
20+
21+
bash "${devbox_dir}/k-rebuild-environment" "$@" 2> >(logError)
1922

2023
info "$(regular)See details in $(bold)${devbox_dir}/log/${current_script_name}.log$(regular). For debug output set $(bold)debug:devbox_project$(regular) to $(bold)1$(regular) in $(bold)etc/config.yaml$(regular)"

0 commit comments

Comments
 (0)