Ref:https://blog.pusher.com/speeding-up-your-website-using-varnish/
https://www.cyberciti.biz/faq/how-to-install-and-configure-varnish-cache-on-ubuntu-linux-16-04-lts/
https://www.howtoforge.com/tutorial/ubuntu-nginx-varnish/
https://amasty.com/blog/use-varnish-with-magento-2-for-better-end-user-experience/
Install Varnish:
|
1 |
teddy@teddy:~$ sudo apt-get update |
|
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 |
teddy@teddy:~$ sudo apt-get install varnish Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libtidy5 libzip4 linux-headers-4.15.0-29 linux-headers-4.15.0-29-generic linux-headers-4.15.0-30 linux-headers-4.15.0-30-generic linux-headers-4.15.0-32 linux-headers-4.15.0-32-generic linux-headers-4.15.0-33 linux-headers-4.15.0-33-generic linux-headers-4.15.0-34 linux-headers-4.15.0-34-generic linux-image-4.15.0-29-generic linux-image-4.15.0-30-generic linux-image-4.15.0-32-generic linux-image-4.15.0-33-generic linux-image-4.15.0-34-generic linux-modules-4.15.0-29-generic linux-modules-4.15.0-30-generic linux-modules-4.15.0-32-generic linux-modules-4.15.0-33-generic linux-modules-4.15.0-34-generic linux-modules-extra-4.15.0-29-generic linux-modules-extra-4.15.0-30-generic linux-modules-extra-4.15.0-32-generic linux-modules-extra-4.15.0-33-generic linux-modules-extra-4.15.0-34-generic qtdeclarative5-controls-plugin qtdeclarative5-dialogs-plugin Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: libjemalloc1 libvarnishapi1 Suggested packages: varnish-doc The following NEW packages will be installed: libjemalloc1 libvarnishapi1 varnish 0 upgraded, 3 newly installed, 0 to remove and 6 not upgraded. Need to get 660 kB of archives. After this operation, 2.101 kB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://kambing.ui.ac.id/ubuntu xenial-updates/universe amd64 libvarnishapi1 amd64 4.1.1-1ubuntu0.2 [57,0 kB] Get:2 http://kambing.ui.ac.id/ubuntu xenial/universe amd64 libjemalloc1 amd64 3.6.0-9ubuntu1 [78,9 kB] Get:3 http://kambing.ui.ac.id/ubuntu xenial-updates/universe amd64 varnish amd64 4.1.1-1ubuntu0.2 [524 kB] Fetched 660 kB in 1s (634 kB/s) Selecting previously unselected package libvarnishapi1. (Reading database ... 546972 files and directories currently installed.) Preparing to unpack .../libvarnishapi1_4.1.1-1ubuntu0.2_amd64.deb ... Unpacking libvarnishapi1 (4.1.1-1ubuntu0.2) ... Selecting previously unselected package libjemalloc1. Preparing to unpack .../libjemalloc1_3.6.0-9ubuntu1_amd64.deb ... Unpacking libjemalloc1 (3.6.0-9ubuntu1) ... Selecting previously unselected package varnish. Preparing to unpack .../varnish_4.1.1-1ubuntu0.2_amd64.deb ... Unpacking varnish (4.1.1-1ubuntu0.2) ... Processing triggers for libc-bin (2.23-0ubuntu10) ... Processing triggers for systemd (229-4ubuntu21.5) ... Processing triggers for ureadahead (0.100.0-19) ... ureadahead will be reprofiled on next reboot Processing triggers for man-db (2.7.5-1) ... Setting up libvarnishapi1 (4.1.1-1ubuntu0.2) ... Setting up libjemalloc1 (3.6.0-9ubuntu1) ... Setting up varnish (4.1.1-1ubuntu0.2) ... Processing triggers for libc-bin (2.23-0ubuntu10) ... Processing triggers for systemd (229-4ubuntu21.5) ... Processing triggers for ureadahead (0.100.0-19) ... |
Start/stop/restart the varnish service:
|
1 2 3 |
teddy@teddy:~$ sudo service varnish start teddy@teddy:~$ sudo service varnish stop teddy@teddy:~$ sudo service varnish restart |
Check the varnish status (when active):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
teddy@teddy:~$ sudo service varnish status ● varnish.service - Varnish HTTP accelerator Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: Active: active (running) since Jum 2019-02-08 09:39:10 WIB; 2min 27s ago Docs: https://www.varnish-cache.org/docs/4.1/ man:varnishd Main PID: 15892 (varnishd) CGroup: /system.slice/varnish.service ├─15892 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localho └─15959 /usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localho Peb 08 09:39:10 teddy systemd[1]: Started Varnish HTTP accelerator. Peb 08 09:39:10 teddy varnishd[15892]: Debug: Platform: Linux,4.15.0-38-generic, Peb 08 09:39:10 teddy varnishd[15892]: Platform: Linux,4.15.0-38-generic,x86_64, Peb 08 09:39:10 teddy varnishd[15892]: Debug: Child (15959) Started Peb 08 09:39:10 teddy varnishd[15892]: Child (15959) Started Peb 08 09:39:10 teddy varnishd[15892]: Info: Child (15959) said Child starts Peb 08 09:39:10 teddy varnishd[15892]: Child (15959) said Child starts Peb 08 09:40:55 teddy systemd[1]: Started Varnish HTTP accelerator. |
Varnish installed and it is a default configured to listen on port 6081 and 6082 for varnish admin:
|
1 2 3 4 5 6 7 8 9 |
teddy@teddy:~$ netstat -tulpn (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name ... tcp 0 0 0.0.0.0:6081 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN ... |
When we open on the browser: http://localhost:6081/, we’d get a varnish error page
However http://localhost:6082/ will show page error.
We will be changing this so it listens on port 80 (fontaineind use the default port 6081 for varnish) and Nginx listens on port 8080. To do this, edit your site’s Nginx configuration file. In our case, this file is /etc/nginx/sites-available/fontaineind.test.
|
1 |
teddy@teddy:~$ sudo gedit /etc/nginx/sites-available/fontaineind.test |
modify
|
1 2 3 |
listen 80; to listen 8080; |
Save the file. Then restart nginx server:
|
1 |
sudo service nginx restart |
When you open your website with the port 8080, http://fontaineind.test:8080, It’d show the website but the css seems missing! I think I need to clear the cache and rebuild the cache. but it show an expected error.
|
1 2 3 4 5 6 7 |
teddy@teddy:~/Documents/works/fontaineind$ sudo su -s /bin/bash www-data www-data@teddy:/home/teddy/Documents/works/fontaineind$ php bin/magento c:f [InvalidArgumentException] There are no commands defined in the "c" namespace. |
Another error when trying to show the magento 2 command:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
www-data@teddy:/home/teddy/Documents/works/fontaineind$ php bin/magento Magento CLI version 2.1.9 Usage: command [options] [arguments] Options: --help (-h) Display this help message ... Available commands: help Displays help for a command list Lists commands We're sorry, an error occurred. Try clearing the cache and code generation directories. By default, they are: var/cache, var/di, var/generation, and var/page_cache. [Exception] Deprecated Functionality: The each() function is deprecated. This message w ill be suppressed on further calls in /home/teddy/Documents/works/fontainei nd/vendor/colinmollenhour/cache-backend-file/File.php on line 81 |
Solution: THIS IS BECAUSE I USE PHP 7.2. CHANGE IT TO PHP 7.0
|
1 2 3 4 5 6 7 |
teddy@teddy:~/Documents/works/fontaineind$ sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php teddy@teddy:~/Documents/works/fontaineind$ php -v PHP 7.0.33-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Dec 7 2018 08:27:52) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.33-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans |
Then the magento 2 command is running well back.
also probably I need to change the base url to include the port 8080? –> NO NEED. we will take it later. Let’s continue.
Now that port 80 is free (NO. I NEED TO CHECK IT VIA sudo netstat -plntu | grep ‘:80’), let’s configure Varnish to be the one that responds. Open the file /etc/default/varnish for editing. Here is the default content.
|
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 |
# Configuration file for varnish # # /etc/init.d/varnish expects the variables $DAEMON_OPTS, $NFILES and $MEMLOCK # to be set from this shell script fragment. # # Note: If systemd is installed, this file is obsolete and ignored. Please see # /usr/share/doc/varnish/examples/varnish.systemd-drop-in.conf # Should we start varnishd at boot? Set to "no" to disable. START=yes # Maximum number of open files (for ulimit -n) NFILES=131072 # Maximum locked memory size (for ulimit -l) # Used for locking the shared memory log in memory. If you increase log size, # you need to increase this number as well MEMLOCK=82000 # Default varnish instance name is the local nodename. Can be overridden with # the -n switch, to have more instances on a single server. # You may need to uncomment this variable for alternatives 1 and 3 below. # INSTANCE=$(uname -n) # This file contains 4 alternatives, please use only one. ## Alternative 1, Minimal configuration, no VCL # # Listen on port 6081, administration on localhost:6082, and forward to # content server on localhost:8080. Use a 1GB fixed-size cache file. # # This example uses the INSTANCE variable above, which you need to uncomment. # # DAEMON_OPTS="-a :6081 \ # -T localhost:6082 \ # -b localhost:8080 \ # -u varnish -g varnish \ # -S /etc/varnish/secret \ # -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G" ## Alternative 2, Configuration with VCL # # Listen on port 6081, administration on localhost:6082, and forward to # one content server selected by the vcl file, based on the request. # DAEMON_OPTS="-a :6081 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m" ## Alternative 3, Advanced configuration # # This example uses the INSTANCE variable above, which you need to uncomment. # # See varnishd(1) for more information. # # # Main configuration file. You probably want to change it :) # VARNISH_VCL_CONF=/etc/varnish/default.vcl # # # Default address and port to bind to # # Blank address means all IPv4 and IPv6 interfaces, otherwise specify # # a host name, an IPv4 dotted quad, or an IPv6 address in brackets. # VARNISH_LISTEN_ADDRESS= # VARNISH_LISTEN_PORT=6081 # # # Telnet admin interface listen address and port # VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 # VARNISH_ADMIN_LISTEN_PORT=6082 # # # Cache file location # VARNISH_STORAGE_FILE=/var/lib/varnish/$INSTANCE/varnish_storage.bin # # # Cache file size: in bytes, optionally using k / M / G / T suffix, # # or in percentage of available disk space using the % suffix. # VARNISH_STORAGE_SIZE=1G # # # File containing administration secret # VARNISH_SECRET_FILE=/etc/varnish/secret # # # Backend storage specification # VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}" # # # Default TTL used when the backend does not specify one # VARNISH_TTL=120 # # # DAEMON_OPTS is used by the init script. If you add or remove options, make # # sure you update this section, too. # DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \ # -f ${VARNISH_VCL_CONF} \ # -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \ # -t ${VARNISH_TTL} \ # -S ${VARNISH_SECRET_FILE} \ # -s ${VARNISH_STORAGE}" # ## Alternative 4, Do It Yourself # # DAEMON_OPTS="" |
Look for the line below:
|
1 2 3 4 5 |
DAEMON_OPTS="-a :6081 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m" |
change ‘-a :6081’ to ‘-a :80’:
|
1 2 3 4 5 |
DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m" |
Save the file.
NOTE: I also can change the malloc. malloc is RAM usage for Varnish. Default is 256m. I can change it to (for example) 1024m (1G)
Next, open the file /etc/varnish/default.vcl and check that the default backend is set to port 8080, because this is where Nginx serves from now. Look for the line below and make sure the .port is 8080. Here is the default content:
|
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 |
# # This is an example VCL file for Varnish. # # It does not do anything by default, delegating control to the # builtin VCL. The builtin VCL is called when there is no explicit # return statement. # # See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ # and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples. # Marker to tell the VCL compiler that this VCL has been adapted to the # new 4.0 format. vcl 4.0; # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_recv { # Happens before we check if we have this in cache already. # # Typically you clean up the request here, removing cookies you don't need, # rewriting the request, etc. } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. } |
NOTE: For the explanation of vcl_recv, vcl_backend_response and vcl_deliver, pls read https://amasty.com/blog/use-varnish-with-magento-2-for-better-end-user-experience/
make sure the backend port is 8080
|
1 2 3 4 5 |
... backend default { .host = "127.0.0.1"; .port = "8080"; ... |
Once you have confirmed this, the next thing will be to copy the varnish.service file to our systemd directory. This will enable systemd to start varnish on port 80. Run the command below:
|
1 2 |
teddy@teddy:~/Documents/works/fontaineind$ sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/ teddy@teddy:~/Documents/works/fontaineind$ sudo gedit /etc/systemd/system/varnish.service |
This will copy the varnish service file, and then open an editor with the contents.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[Unit] Description=Varnish HTTP accelerator Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd [Service] Type=simple LimitNOFILE=131072 LimitMEMLOCK=82000 ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m ExecReload=/usr/share/varnish/reload-vcl ProtectSystem=full ProtectHome=true PrivateTmp=true PrivateDevices=true [Install] WantedBy=multi-user.target |
Look for the ExecStart line:
|
1 |
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m |
Now change the -F -a :6081 to -F -a :80. Leave the localhost:6082 on the same line as it is. Now save and exit the file. Run the commands below to reload everything you have just configured:
|
1 2 3 |
teddy@teddy:~/Documents/works/fontaineind$ sudo systemctl daemon-reload teddy@teddy:~/Documents/works/fontaineind$ sudo systemctl reload varnish.serviceteddy@teddy:~/Documents/works/fontaineind$ sudo service varnish restart teddy@teddy:~/Documents/works/fontaineind$ sudo service nginx restart |
The commands above will reload the system daemon, reload varnish, restart the varnish service, and restart Nginx.
NOTE: I need to check the varnish status as well. Varnish can’t start because port 80 already is use:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
teddy@teddy:~/Documents/works/fontaineind$ sudo service varnish start teddy@teddy:~/Documents/works/fontaineind$ sudo service varnish status varnish.service - Varnish HTTP accelerator Loaded: loaded (/etc/systemd/system/varnish.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Jum 2019-02-08 10:47:44 WIB; 1s ago Docs: https://www.varnish-cache.org/docs/4.1/ man:varnishd Process: 19051 ExecReload=/usr/share/varnish/reload-vcl (code=exited, status=0/SUCCESS) Process: 20664 ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f / Main PID: 20664 (code=exited, status=2) Peb 08 10:47:44 teddy systemd[1]: Started Varnish HTTP accelerator. Peb 08 10:47:44 teddy varnishd[20664]: Error: Cannot open socket: :80: Address already in use <--------------------- Peb 08 10:47:44 teddy systemd[1]: varnish.service: Main process exited, code=exited, status=2/INVA Peb 08 10:47:44 teddy systemd[1]: varnish.service: Unit entered failed state. Peb 08 10:47:44 teddy systemd[1]: varnish.service: Failed with result 'exit-code'. |
Check who is using port 80:
|
1 2 3 4 |
teddy@teddy:/var/log$ sudo netstat -plntu | grep ':80' tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 19110/nginx -g daem tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19110/nginx -g daem tcp6 0 0 :::80 :::* LISTEN 19110/nginx -g daem |
it seems nginx with pid 19110 is using both port 80 and 8080. or here is the complete list:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
teddy@teddy:/var/log$ sudo lsof -i ':80' COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME firefox 5044 teddy 159u IPv4 163157 0t0 TCP 192.168.1.3:47334->us3.clientdomainmanager.org:http (ESTABLISHED) nginx 19110 root 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19110 root 9u IPv6 146485 0t0 TCP *:http (LISTEN) nginx 19111 www-data 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19111 www-data 9u IPv6 146485 0t0 TCP *:http (LISTEN) nginx 19112 www-data 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19112 www-data 9u IPv6 146485 0t0 TCP *:http (LISTEN) nginx 19113 www-data 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19113 www-data 9u IPv6 146485 0t0 TCP *:http (LISTEN) nginx 19114 www-data 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19114 www-data 9u IPv6 146485 0t0 TCP *:http (LISTEN) nginx 19115 www-data 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19115 www-data 9u IPv6 146485 0t0 TCP *:http (LISTEN) nginx 19116 www-data 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19116 www-data 9u IPv6 146485 0t0 TCP *:http (LISTEN) nginx 19117 www-data 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19117 www-data 9u IPv6 146485 0t0 TCP *:http (LISTEN) nginx 19118 www-data 8u IPv4 146484 0t0 TCP *:http (LISTEN) nginx 19118 www-data 9u IPv6 146485 0t0 TCP *:http (LISTEN) |
Solution: nginx use both ports 80 and 8080 because in sites-enabled directory there are many nginx setting for different websites that it still use port 80.
|
1 2 3 4 5 6 7 8 9 |
teddy@teddy:/etc/nginx/sites-enabled$ ls aliceisback.test ko32example.test notarisfemira.test silverstripe.test craftcms.test kohana.test orangescrum.test stickercollective.test default kokopage.test osclass374.test tff-magento2.test engelcoolers.test koseven.test persuratan.test wordpress498.test fontaineind.test laravel-voyager.test pethome.test yclass.test hitchnpitch2.test magento19.test phpmyadmin.test hitchnpitch.test magento222.test resales.test kirby.test magentoce215.test sc2.test |
SO. REPLACE THE PORT 80 TO 8080 FOR ALL WEBSITE IN SITES-ENABLES DIRECTORY!!!
AFTER I CHANGED THE PORT IN ALL THE WEBSITE, NO MORE APPLICATION USE PORT 80 ANYMORE
|
1 2 3 |
teddy@teddy:/etc/nginx/sites-enabled$ sudo netstat -plntu | grep ':80' tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 30008/nginx -g daem tcp6 0 0 :::8080 :::* LISTEN 30008/nginx -g daem |
OR
|
1 2 3 |
teddy@teddy:/etc/nginx/sites-enabled$ sudo netstat -plntu | grep nginx tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 30008/nginx -g daem tcp6 0 0 :::8080 :::* LISTEN 30008/nginx -g daem |
Nginx is using port 8080 now. I can start varnish and check the status:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
teddy@teddy:/etc/nginx/sites-enabled$ sudo service varnish start teddy@teddy:/etc/nginx/sites-enabled$ sudo service varnish status ● varnish.service - Varnish HTTP accelerator Loaded: loaded (/etc/systemd/system/varnish.service; enabled; vendor preset: enabled) Active: active (running) since Jum 2019-02-08 14:02:22 WIB; 4s ago Docs: https://www.varnish-cache.org/docs/4.1/ man:varnishd Process: 19051 ExecReload=/usr/share/varnish/reload-vcl (code=exited, status=0/SUCCESS) Main PID: 30257 (varnishd) CGroup: /system.slice/varnish.service ├─30257 /usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varn └─30267 /usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varn Peb 08 14:02:22 teddy systemd[1]: Started Varnish HTTP accelerator. Peb 08 14:02:23 teddy varnishd[30257]: Debug: Platform: Linux,4.15.0-38-generic,x86_64,-junix,-sma Peb 08 14:02:23 teddy varnishd[30257]: Platform: Linux,4.15.0-38-generic,x86_64,-junix,-smalloc,-s Peb 08 14:02:23 teddy varnishd[30257]: Debug: Child (30267) Started Peb 08 14:02:23 teddy varnishd[30257]: Child (30267) Started Peb 08 14:02:23 teddy varnishd[30257]: Info: Child (30267) said Child starts Peb 08 14:02:23 teddy varnishd[30257]: Child (30267) said Child starts |
NOTE: TO RUN THE OTHERS RUNNING WEBSITE (WITHOUT VARNISH RUINNING) THAT IT HAD BEEN WORKING PREVIOUSLY, I NEED TO ADD THE PORT 8080 AT THE END OF THE URL. FOR EXAMPLE:
http://localhost:8080/phpinfo.php
http://notarisfemira.test:8080/
http://phpmyadmin.test:8080
Now visit the IP of your web page and you should see nothing has changed. However, when you run it through the HTTP checker we used before, you should see that the headers have changed.
http://fontaineind.test/ –> First time run about 20 seconds
|
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 |
Request URL: http://fontaineind.test/ Request Method: GET Status Code: 200 OK Remote Address: 127.0.0.1:80 Referrer Policy: no-referrer-when-downgrade Accept-Ranges: bytes Age: 0 Cache-Control: max-age=0, must-revalidate, no-cache, no-store Connection: keep-alive Content-Encoding: gzip Content-Type: text/html; charset=UTF-8 Date: Fri, 08 Feb 2019 07:06:16 GMT Expires: Thu, 08 Feb 2018 07:06:16 GMT Pragma: no-cache Server: nginx/1.10.3 (Ubuntu) Set-Cookie: X-Magento-Vary=1824d10cc0bdb1434527d6e3f2eb93ec93279e70; path=/; HttpOnly Transfer-Encoding: chunked Vary: Accept-Encoding Via: 1.1 varnish-v4 X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Varnish: 11 X-XSS-Protection: 1; mode=block Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 Cache-Control: no-cache Connection: keep-alive Cookie: _ga=GA1.2.153436117.1539751926; __zlcmid=ovhiooeabcbDF8; fastest-popup=true; __atuvc=1%7C48%2C1%7C49; persistent_shopping_cart=1KnMotS8bUecwuFfLz4ZWnjaHxkFBegbdLoTMPtSV1vnmhuz8m; PHPSESSID=a81ae6cda46l21cvknvu7prsb3; form_key=tcwxhKGXlLxlM34K; _gid=GA1.2.1663350196.1549590795; mage-translation-storage=%7B%7D; mage-translation-file-version=%7B%7D; mage-cache-storage=%7B%7D; mage-cache-storage-section-invalidation=%7B%7D; _hjIncludedInSample=1; mage-cache-sessid=true; X-Magento-Vary=1824d10cc0bdb1434527d6e3f2eb93ec93279e70; mage-messages=; private_content_version=f2172c778c79ea181e3586015b2ef162; section_data_ids=%7B%22cart%22%3A1549592685%2C%22wishlist%22%3A1549592684%2C%22last-ordered-items%22%3A1549592684%2C%22customer%22%3A1549590799%2C%22compare-products%22%3A1549590799%2C%22directory-data%22%3A1549590799%2C%22review%22%3A1549590799%2C%22crosssell_products%22%3A1549590799%2C%22paypal-billing-agreement%22%3A1549590799%7D Host: fontaineind.test Pragma: no-cache Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 |
The second run (refresh) about 9 seconds
|
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 |
Request URL: http://fontaineind.test/ Request Method: GET Status Code: 200 OK Remote Address: 127.0.0.1:80 Referrer Policy: no-referrer-when-downgrade Accept-Ranges: bytes Age: 0 Cache-Control: max-age=0, must-revalidate, no-cache, no-store Connection: keep-alive Content-Encoding: gzip Content-Type: text/html; charset=UTF-8 Date: Fri, 08 Feb 2019 07:09:41 GMT Expires: Thu, 08 Feb 2018 07:06:16 GMT Pragma: no-cache Server: nginx/1.10.3 (Ubuntu) Set-Cookie: X-Magento-Vary=1824d10cc0bdb1434527d6e3f2eb93ec93279e70; path=/; HttpOnly Transfer-Encoding: chunked Vary: Accept-Encoding Via: 1.1 varnish-v4 X-Cache-Warmer: HIT X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Varnish: 32841 X-XSS-Protection: 1; mode=block Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 Cache-Control: no-cache Connection: keep-alive Cookie: _ga=GA1.2.153436117.1539751926; __zlcmid=ovhiooeabcbDF8; fastest-popup=true; __atuvc=1%7C48%2C1%7C49; persistent_shopping_cart=1KnMotS8bUecwuFfLz4ZWnjaHxkFBegbdLoTMPtSV1vnmhuz8m; PHPSESSID=a81ae6cda46l21cvknvu7prsb3; form_key=tcwxhKGXlLxlM34K; _gid=GA1.2.1663350196.1549590795; mage-translation-storage=%7B%7D; mage-translation-file-version=%7B%7D; mage-cache-storage=%7B%7D; mage-cache-storage-section-invalidation=%7B%7D; _hjIncludedInSample=1; mage-cache-sessid=true; X-Magento-Vary=1824d10cc0bdb1434527d6e3f2eb93ec93279e70; mage-messages=; private_content_version=ec5f01fcb88dd0469d065a446f519922; section_data_ids=%7B%22cart%22%3A1549609584%2C%22wishlist%22%3A1549609582%2C%22last-ordered-items%22%3A1549609582%2C%22customer%22%3A1549609582%2C%22compare-products%22%3A1549609582%2C%22directory-data%22%3A1549609584%2C%22review%22%3A1549609582%2C%22crosssell_products%22%3A1549609582%2C%22paypal-billing-agreement%22%3A1549609582%7D Host: fontaineind.test Pragma: no-cache Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 |
NOTE: I can see some variables like X-Varnish and Age. But why Age still 0 on the refresh page? It means it miss the cache.
In magento, I already enabled Page Cache and Block HTML cache.
Configure Magento to use Varnish
To configure Magento to use Varnish:
Log in to the Magento Admin as an administrator.
Click STORES > Configuration > ADVANCED > System > Full Page Cache.
From the Caching Application list, click Varnish Cache (recommended).
Enter a value in the TTL for public content field.
Expand Varnish Configuration and enter the following information:
Access list: localhost –> 172.31.31.224 for fontaineind.com.au
Backend host: localhost –> 172.31.31.224 for fontaineind.com.au
Backend port: 8080
Then click a button ‘Export VCL for Varnish 4’ -> It’d create a file ‘varnish.vcl’ and download it.
Here is the file content:
|
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
vcl 4.0; import std; # The minimal Varnish version is 4.0 # For SSL offloading, pass the following header in your proxy server or load balancer: 'X-Forwarded-Proto: https' backend default { .host = "localhost"; .port = "8080"; } acl purge { "localhost"; } sub vcl_recv { if (req.method == "PURGE") { if (client.ip !~ purge) { return (synth(405, "Method not allowed")); } if (!req.http.X-Magento-Tags-Pattern) { return (synth(400, "X-Magento-Tags-Pattern header required")); } ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern); return (synth(200, "Purged")); } if (req.method != "GET" && req.method != "HEAD" && req.method != "PUT" && req.method != "POST" && req.method != "TRACE" && req.method != "OPTIONS" && req.method != "DELETE") { /* Non-RFC2616 or CONNECT which is weird. */ return (pipe); } # We only deal with GET and HEAD by default if (req.method != "GET" && req.method != "HEAD") { return (pass); } # Bypass shopping cart, checkout and search requests if (req.url ~ "/checkout" || req.url ~ "/catalogsearch") { return (pass); } # normalize url in case of leading HTTP scheme and domain set req.url = regsub(req.url, "^http[s]?://", ""); # collect all cookies std.collect(req.http.Cookie); # Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression if (req.http.Accept-Encoding) { if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") { # No point in compressing these unset req.http.Accept-Encoding; } elsif (req.http.Accept-Encoding ~ "gzip") { set req.http.Accept-Encoding = "gzip"; } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") { set req.http.Accept-Encoding = "deflate"; } else { # unkown algorithm unset req.http.Accept-Encoding; } } # Remove Google gclid parameters to minimize the cache objects set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA" set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar" set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz" # static files are always cacheable. remove SSL flag and cookie if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") { unset req.http.Https; unset req.http.X-Forwarded-Proto; unset req.http.Cookie; } return (hash); } sub vcl_hash { if (req.http.cookie ~ "X-Magento-Vary=") { hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1")); } # For multi site configurations to not cache each other's content if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } # To make sure http users don't see ssl warning if (req.http.X-Forwarded-Proto) { hash_data(req.http.X-Forwarded-Proto); } } sub vcl_backend_response { if (beresp.http.content-type ~ "text") { set beresp.do_esi = true; } if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") { set beresp.do_gzip = true; } # cache only successfully responses and 404s if (beresp.status != 200 && beresp.status != 404) { set beresp.ttl = 0s; set beresp.uncacheable = true; return (deliver); } elsif (beresp.http.Cache-Control ~ "private") { set beresp.uncacheable = true; set beresp.ttl = 86400s; return (deliver); } if (beresp.http.X-Magento-Debug) { set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control; } # validate if we need to cache it and prevent from setting cookie # images, css and js are cacheable by default so we have to remove cookie also if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) { unset beresp.http.set-cookie; 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)(\?|$)") { set beresp.http.Pragma = "no-cache"; set beresp.http.Expires = "-1"; set beresp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0"; set beresp.grace = 1m; } } # If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass if (beresp.ttl <= 0s || beresp.http.Surrogate-control ~ "no-store" || (!beresp.http.Surrogate-Control && beresp.http.Vary == "*")) { # Mark as Hit-For-Pass for the next 2 minutes set beresp.ttl = 120s; set beresp.uncacheable = true; } return (deliver); } sub vcl_deliver { if (resp.http.X-Magento-Debug) { if (resp.http.x-varnish ~ " ") { set resp.http.X-Magento-Cache-Debug = "HIT"; } else { set resp.http.X-Magento-Cache-Debug = "MISS"; } } else { unset resp.http.Age; } unset resp.http.X-Magento-Debug; unset resp.http.X-Magento-Tags; unset resp.http.X-Powered-By; unset resp.http.Server; unset resp.http.X-Varnish; unset resp.http.Via; unset resp.http.Link; } |
SO I need to modify the content of /etc/varnish/default.vcl according to this varnish.vcl.
|
1 |
teddy@teddy:/etc/nginx/sites-enabled$ sudo gedit /etc/varnish/default.vcl |
Just copy and paste all content of varnish.vcl to all content in /etc/varnish/default.vcl. Then restart varnish: sudo service varnish restart
NOTE: IN THE DEV SERVER, I PREFER TO DISPLAY THE X-VARNISH AND AGE VARIABLES. SO I MODIFIED /etc/varnish/default.vcl LIKE THIS:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
... sub vcl_deliver { if (resp.http.X-Magento-Debug) { if (resp.http.x-varnish ~ " ") { set resp.http.X-Magento-Cache-Debug = "HIT"; } else { set resp.http.X-Magento-Cache-Debug = "MISS"; } } else { #unset resp.http.Age; } unset resp.http.X-Magento-Debug; unset resp.http.X-Magento-Tags; unset resp.http.X-Powered-By; unset resp.http.Server; #unset resp.http.X-Varnish; unset resp.http.Via; unset resp.http.Link; } |
I also modified /home/teddy/Documents/works/fontaineind/app/etc/env.php to include the varnish setting:
|
1 2 3 4 5 6 7 8 |
'http_cache_hosts' => array ( 0 => array ( 'host' => 'localhost', 'port' => '80', ), ), |
clear and flush the cache. BUT Age still 0 !!!! EVEN WEIRDS, WHEN I CHANGED IN THE MAGENTO ADMIN THAT CACHE APPLICATION IS VARNISH THEN REFRESH THE WEBSITE (ALSO RESTART VARNISH), I GOT THE PAGE LOAD SLOWER!!! WHY?? –> SOLUTION: I NEED TO EXPORT VCL FILE LIKE ABOVE AND COPY THE CONTENT INTO /etc/varnish/default.vcl
NOW THE VARNISH IS WORKING!!!
THE SECOND AND MORE REFRESH WOULD MAKE THE PAGE LOAD FASTER. HERE IS THE RESPONSE:
|
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 |
Request URL: http://fontaineind.test/ Request Method: GET Status Code: 200 OK Remote Address: 127.0.0.1:80 Referrer Policy: no-referrer-when-downgrade Accept-Ranges: bytes Age: 42 Cache-Control: no-store, no-cache, must-revalidate, max-age=0 Connection: keep-alive Content-Encoding: gzip Content-Length: 117914 Content-Type: text/html; charset=UTF-8 Date: Fri, 08 Feb 2019 08:03:16 GMT Expires: -1 Pragma: no-cache Vary: Accept-Encoding X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Varnish: 131131 3 X-XSS-Protection: 1; mode=block Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 Cache-Control: no-cache Connection: keep-alive Cookie: _ga=GA1.2.153436117.1539751926; __zlcmid=ovhiooeabcbDF8; fastest-popup=true; __atuvc=1%7C48%2C1%7C49; persistent_shopping_cart=1KnMotS8bUecwuFfLz4ZWnjaHxkFBegbdLoTMPtSV1vnmhuz8m; PHPSESSID=a81ae6cda46l21cvknvu7prsb3; form_key=tcwxhKGXlLxlM34K; _gid=GA1.2.1663350196.1549590795; mage-translation-storage=%7B%7D; mage-translation-file-version=%7B%7D; mage-cache-storage=%7B%7D; mage-cache-storage-section-invalidation=%7B%7D; _hjIncludedInSample=1; mage-cache-sessid=true; X-Magento-Vary=1824d10cc0bdb1434527d6e3f2eb93ec93279e70; mage-messages=; _gat=1; private_content_version=54b5be8fb7f0610fc48d2534a06f1e50; section_data_ids=%7B%22cart%22%3A1549613003%2C%22wishlist%22%3A1549613001%2C%22last-ordered-items%22%3A1549613001%2C%22customer%22%3A1549609582%2C%22compare-products%22%3A1549609582%2C%22directory-data%22%3A1549609584%2C%22review%22%3A1549609582%2C%22crosssell_products%22%3A1549609582%2C%22paypal-billing-agreement%22%3A1549609582%7D Host: fontaineind.test Pragma: no-cache Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 |
SEE THE AGE = 42 AND X-Varnish: 131131 3