Skip to content

Commit f1f73cc

Browse files
author
Alankrit Srivastava
committed
update
1 parent 3f55955 commit f1f73cc

File tree

42,284 files changed

+989
-5526772
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42,284 files changed

+989
-5526772
lines changed

README.md

+22-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#### Optimising Magento 2 with Varnish Cache, Redis and Nginx SSL termination on the Multi-container Architecture Using Docker-Compose tool.
1+
### Optimising Magento 2 with Varnish Cache, Redis and Nginx SSL termination on the Multi-container Architecture Using Docker-Compose tool.
22

33
This repository corresponds to architecture setup as mentioned in blog https://cloudkul.com/blog/integrate-magento-2-varnish-cache-redis-server-ssl-termination-using-docker-compose/ .
44

@@ -30,46 +30,41 @@ Magento 2 works out of box with Varnish Cache and provides its own VCL file for
3030

3131
Nginx servers as reverse proxy server that receives traffic on port 80 and 443 and then proxy pass it to listening port of Varnish Cache server. It is done to deploy a way to direct both HTTP and HTTPS traffic to Varnish cache server which in turn, if needed, forward it apache2 server.
3232

33-
3433
In this project, we are using:
3534

36-
> Operating system: Ubuntu 14.04
35+
> Operating system: Ubuntu 16.04
3736
3837
> Web Server: Apache2
3938
40-
> Database Server: Mysql-server-5.6
39+
> Database Server: Mysql-server-5.7
40+
41+
> Cache Server: Varnish 4.1
4142
42-
> Cache Server: Varnish 3.0.
43+
> PHP version: PHP-7.1
4344
4445
> Redis server: Redis
4546
4647
> SSL server: Nginx 1.10.1
4748
48-
> PHP version: PHP-7.0
49-
50-
> Magento 2.1.6
51-
52-
To begin with, please install docker and docker-compose on your ubuntu 14.04 server.
49+
To begin with, please install docker and docker-compose on your ubuntu server.
5350

5451
Then follow the following steps:
5552

5653
1). Clone or download this repository as
5754

58-
> git clone https://github.com/webkul/magento2-varnish-redis-ssl-docker-compose.git
55+
> git clone https://github.com/webkul/magento2-varnish-redis-ssl-docker-compose.git.
5956
60-
2) Set/modify mysql root credentials. Go to ~/magento2-varnish-redis-ssl-docker-compose/docker-compose.yml and change mysql root password in database_server in:
57+
2) Set mysql root credentials and name of the database to be created in *database_server* block ~/magento2-varnish-redis-ssl-docker-compose/docker-compose.yml:
6158

62-
> mysql_password=rootpassword123
59+
> mysql_password=
6360
64-
3). Modify change database name, database user and mysql root credentials in ~/magento2-varnish-redis-ssl-docker-compose/database_server/mysql.sh in:
61+
> mysql_database=
6562
66-
> database_name=magento_db ## Mention database name
63+
3). Download Magento 2 version you wish to dockerize and upload it in directory magento2 in parallel docker-compose.yml.
6764

68-
> database_user=magento_user ## Mention database user
65+
> Go to https://magento.com/tech-resources/download? .
6966
70-
> database_root_password=rootpassword123 ## Mention mysql root password.
71-
72-
4). Replace the IP address assigned to 'server_name' in ~/magento2-varnish-redis-ssl-docker-compose/ssl_server/default with your domain name or IP address.
67+
4). Replace localhost in 'server_name' in ~/magento2-varnish-redis-ssl-docker-compose/ssl_server/default with your domain name or IP address.
7368

7469
5). Build the docker image.
7570

@@ -89,18 +84,11 @@ Then follow the following steps:
8984
9085
> docker ps
9186
92-
9). Your database credentials are mentioned in mysql.sh file. Database user password will be randomly generated. Database user password will be stored in /var/log/check.log. To get database user password,
93-
94-
> docker exec -ti mysql bash
95-
96-
> cat /var/log/check.log
97-
98-
or,
99-
100-
> docker exec -i mysql cat /var/log/check.log
10187

10288
Now, your server setup is all ready, now hit your domain name or IP to install Magento 2. Now to configure Varnish for Magento 2 and test its working, please refer to blog https://cloudkul.com/blog/magento-2-and-varnish-cache-integration-with-docker-compose/.
10389

90+
> Use Database host as mysql (name or id of the mysql container).
91+
10492
To configure Magento 2 for redis-server, please refer to blog https://cloudkul.com/blog/integrate-magento-2-varnish-cache-redis-server-ssl-termination-using-docker-compose/ .
10593

10694

@@ -109,3 +97,9 @@ To configure Magento 2 for redis-server, please refer to blog https://cloudkul.c
10997
Although we had secured our application code keeping it on our host but database is as important as server code. So in order to keep their backup we schedule a shell script that will take backups of all the databases present in mysql-server container and keep them in archived from on our host. Shell script is present on ~/magento2-varnish-redis-ssl-docker-compose/backups/db_backup.sh. Please refer to blog https://cloudkul.com/blog/integrate-magento-2-varnish-cache-redis-server-ssl-termination-using-docker-compose/ for backup management.
11098

11199
If you face any issues, kindly report back.
100+
101+
102+
#### GETTING SUPPORT
103+
104+
If you have any issues, contact us at [email protected] or raise ticket at https://webkul.uvdesk.com/
105+

backups/backup.sh

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#!/bin/bash
22

3-
set -x
3+
set -u
44
## Mention your database container name
55
container_name=mysql
66

7-
## Mention your mysql root password
8-
mysql_root_password=rootpassword123
7+
## Mention mysql root password
8+
9+
MYSQL_ROOT_PASSWORD=mention_your_mysql_root_password
910

1011
DATE=`date +%F-%H-%M-%S`
1112

12-
for database in `echo 'show databases;' | docker exec -i mysql mysql --user=root --password=$mysql_root_password | grep -v Database | grep -v information_schema | grep -v mysql | grep -v performance_schema`
13+
for database in `echo 'show databases;' | docker exec -i mysql mysql --user=root --password=$MYSQL_ROOT_PASSWORD | grep -v Database | grep -v information_schema | grep -v mysql | grep -v performance_schema`
1314
do
1415
echo $database
15-
docker exec $container_name mysqldump -u root -p$mysql_root_password $database > $database-$DATE.sql && tar -zcvf $database-$DATE.tar.gz $database-$DATE.sql && rm $database-$DATE.sql && echo "$database-$DATE.tar.gz has been created on `date`" >> database_backup.log
16+
docker exec $container_name mysqldump -u root -p$MYSQL_ROOT_PASSWORD $database > $database-$DATE.sql && tar -zcvf $database-$DATE.tar.gz $database-$DATE.sql && rm $database-$DATE.sql && echo "$database-$DATE.tar.gz has been created on `date`" >> database_backup.log
1617
done
1718

cache_server/Dockerfile

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
From ubuntu:14.04
1+
From ubuntu:16.04
22

33
MAINTAINER Alankrit Srivastava [email protected]
44

55
##update server
66

77
RUN apt-get update \
8-
98
##install supervisor and setup supervisord.conf file
10-
119
&& apt-get install -y supervisor \
12-
1310
&& mkdir -p /var/log/supervisor \
14-
1511
##install varnish
16-
1712
&& apt-get -y install varnish \
18-
1913
&& rm /etc/varnish/default.vcl \
20-
2114
&& rm /etc/default/varnish
22-
23-
EXPOSE 6082 80
24-
15+
EXPOSE 6082 6081
2516
CMD ["/usr/bin/supervisord"]

cache_server/default.vcl

+117-46
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
1+
2+
vcl 4.0;
3+
14
import std;
2-
# The minimal Varnish version is 3.0.5
5+
# The minimal Varnish version is 5.0
36
# For SSL offloading, pass the following header in your proxy server or load balancer: 'X-Forwarded-Proto: https'
47

5-
68
backend default {
79
.host = "apache2";
810
.port = "8080";
11+
.first_byte_timeout = 600s;
912
}
1013

1114
acl purge {
12-
"127.0.0.1";
15+
"localhost";
1316
}
1417

1518
sub vcl_recv {
16-
if (req.restarts == 0) {
17-
if (req.http.x-forwarded-for) {
18-
set req.http.X-Forwarded-For =
19-
req.http.X-Forwarded-For + ", " + client.ip;
20-
} else {
21-
set req.http.X-Forwarded-For = client.ip;
22-
}
23-
}
24-
25-
if (req.request == "PURGE") {
19+
if (req.method == "PURGE") {
2620
if (client.ip !~ purge) {
27-
error 405 "Method not allowed";
21+
return (synth(405, "Method not allowed"));
22+
}
23+
# To use the X-Pool header for purging varnish during automated deployments, make sure the X-Pool header
24+
# has been added to the response in your backend server config. This is used, for example, by the
25+
# capistrano-magento2 gem for purging old content from varnish during it's deploy routine.
26+
if (!req.http.X-Magento-Tags-Pattern && !req.http.X-Pool) {
27+
return (synth(400, "X-Magento-Tags-Pattern or X-Pool header required"));
28+
}
29+
if (req.http.X-Magento-Tags-Pattern) {
30+
ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
2831
}
29-
if (!req.http.X-Magento-Tags-Pattern) {
30-
error 400 "X-Magento-Tags-Pattern header required";
32+
if (req.http.X-Pool) {
33+
ban("obj.http.X-Pool ~ " + req.http.X-Pool);
3134
}
32-
ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
33-
error 200 "Purged";
35+
return (synth(200, "Purged"));
3436
}
3537

36-
if (req.request != "GET" &&
37-
req.request != "HEAD" &&
38-
req.request != "PUT" &&
39-
req.request != "POST" &&
40-
req.request != "TRACE" &&
41-
req.request != "OPTIONS" &&
42-
req.request != "DELETE") {
38+
if (req.method != "GET" &&
39+
req.method != "HEAD" &&
40+
req.method != "PUT" &&
41+
req.method != "POST" &&
42+
req.method != "TRACE" &&
43+
req.method != "OPTIONS" &&
44+
req.method != "DELETE") {
4345
/* Non-RFC2616 or CONNECT which is weird. */
4446
return (pipe);
4547
}
4648

4749
# We only deal with GET and HEAD by default
48-
if (req.request != "GET" && req.request != "HEAD") {
50+
if (req.method != "GET" && req.method != "HEAD") {
4951
return (pass);
5052
}
5153

@@ -54,73 +56,121 @@ sub vcl_recv {
5456
return (pass);
5557
}
5658

59+
# Bypass health check requests
60+
if (req.url ~ "/pub/health_check.php") {
61+
return (pass);
62+
}
63+
64+
# Set initial grace period usage status
65+
set req.http.grace = "none";
66+
5767
# normalize url in case of leading HTTP scheme and domain
5868
set req.url = regsub(req.url, "^http[s]?://", "");
5969

6070
# collect all cookies
6171
std.collect(req.http.Cookie);
6272

63-
# static files are always cacheable. remove SSL flag and cookie
64-
if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
65-
unset req.http.Https;
66-
unset req.http.X-Forwarded-Proto;
67-
unset req.http.Cookie;
73+
# Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
74+
if (req.http.Accept-Encoding) {
75+
if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
76+
# No point in compressing these
77+
unset req.http.Accept-Encoding;
78+
} elsif (req.http.Accept-Encoding ~ "gzip") {
79+
set req.http.Accept-Encoding = "gzip";
80+
} elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
81+
set req.http.Accept-Encoding = "deflate";
82+
} else {
83+
# unkown algorithm
84+
unset req.http.Accept-Encoding;
85+
}
6886
}
6987

70-
set req.grace = 1m;
88+
# Remove Google gclid parameters to minimize the cache objects
89+
set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA"
90+
set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar"
91+
set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"
92+
93+
# Static files caching
94+
if (req.url ~ "^/(pub/)?(media|static)/") {
95+
# Static files should not be cached by default
96+
return (pass);
97+
98+
# But if you use a few locales and don't use CDN you can enable caching static files by commenting previous line (#return (pass);) and uncommenting next 3 lines
99+
#unset req.http.Https;
100+
#unset req.http.X-Forwarded-Proto;
101+
#unset req.http.Cookie;
102+
}
71103

72-
return (lookup);
104+
return (hash);
73105
}
74106

75107
sub vcl_hash {
76108
if (req.http.cookie ~ "X-Magento-Vary=") {
77109
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
78110
}
79111

112+
# For multi site configurations to not cache each other's content
113+
if (req.http.host) {
114+
hash_data(req.http.host);
115+
} else {
116+
hash_data(server.ip);
117+
}
118+
119+
# To make sure http users don't see ssl warning
80120
if (req.http.X-Forwarded-Proto) {
81121
hash_data(req.http.X-Forwarded-Proto);
82122
}
83123

84124
}
85125

86-
sub vcl_fetch {
126+
sub vcl_backend_response {
127+
128+
set beresp.grace = 3d;
129+
87130
if (beresp.http.content-type ~ "text") {
88131
set beresp.do_esi = true;
89132
}
90133

91-
if (req.url ~ "\.js$" || beresp.http.content-type ~ "text") {
134+
if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
92135
set beresp.do_gzip = true;
93136
}
94137

95138
# cache only successfully responses and 404s
96139
if (beresp.status != 200 && beresp.status != 404) {
97140
set beresp.ttl = 0s;
98-
return (hit_for_pass);
141+
set beresp.uncacheable = true;
142+
return (deliver);
99143
} elsif (beresp.http.Cache-Control ~ "private") {
100-
return (hit_for_pass);
144+
set beresp.uncacheable = true;
145+
set beresp.ttl = 86400s;
146+
return (deliver);
101147
}
102148

103149
if (beresp.http.X-Magento-Debug) {
104150
set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
105151
}
106152

107153
# validate if we need to cache it and prevent from setting cookie
108-
# images, css and js are cacheable by default so we have to remove cookie also
109-
if (beresp.ttl > 0s && (req.request == "GET" || req.request == "HEAD")) {
154+
if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
110155
unset beresp.http.set-cookie;
111-
if (req.url !~ "\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)(\?|$)") {
112-
set beresp.http.Pragma = "no-cache";
113-
set beresp.http.Expires = "-1";
114-
set beresp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
115-
set beresp.grace = 1m;
116-
}
117156
}
157+
158+
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
159+
if (beresp.ttl <= 0s ||
160+
beresp.http.Surrogate-control ~ "no-store" ||
161+
(!beresp.http.Surrogate-Control && beresp.http.Vary == "*")) {
162+
# Mark as Hit-For-Pass for the next 2 minutes
163+
set beresp.ttl = 120s;
164+
set beresp.uncacheable = true;
165+
}
166+
return (deliver);
118167
}
119168

120169
sub vcl_deliver {
121170
if (resp.http.X-Magento-Debug) {
122-
if (obj.hits > 0) {
171+
if (resp.http.x-varnish ~ " ") {
123172
set resp.http.X-Magento-Cache-Debug = "HIT";
173+
set resp.http.Grace = req.http.grace;
124174
} else {
125175
set resp.http.X-Magento-Cache-Debug = "MISS";
126176
}
@@ -136,3 +186,24 @@ sub vcl_deliver {
136186
unset resp.http.Via;
137187
unset resp.http.Link;
138188
}
189+
190+
sub vcl_hit {
191+
if (obj.ttl >= 0s) {
192+
# Hit within TTL period
193+
return (deliver);
194+
}
195+
if (std.healthy(req.backend_hint)) {
196+
if (obj.ttl + 300s > 0s) {
197+
# Hit after TTL expiration, but within grace period
198+
set req.http.grace = "normal (healthy server)";
199+
return (deliver);
200+
} else {
201+
# Hit after TTL and grace expiration
202+
return (miss);
203+
}
204+
} else {
205+
# server is not healthy, retrieve from cache
206+
set req.http.grace = "unlimited (unhealthy server)";
207+
return (deliver);
208+
}
209+
}

0 commit comments

Comments
 (0)