[HowTo] Configure Magento to use Varnish with Nginx VestaCP
Posted: Thu Sep 13, 2018 5:19 pm
Configuring Nginx
We need to edit the Nginx configuration which we created in the first post to handle SSL/TLS termination and as a back-end for Varnish.
We also need to remove the default Nginx server block from the nginx.conf file. Comment or delete the following lines:
Reload the Nginx service for changes to take effect:
Installing and Configuring Varnish
Varnish is a fast reverse-proxy HTTP accelerator that will sit in front of our web server and it will be used as a Full Page Cache solution for our Magento installation.
Install Varnish via yum with the following command:
To configure Magento to use Varnish run:
Next, we need to generate a Varnish configuration file:
The command above needs to be run as a root or user with sudo privileges and it will create a file /etc/varnish/default.vcl using the default values which are localhost as back-end host and port 8080 as back-end port.
The default configuration comes with a wrong URL for the health check file. Open the default.vcl file and remove the /pub part from the line highlighted in yellow:
By default Varnish listens on port 6081 and we need to change it to 80:
Once you are done with the modifications, start and enable the Varnish service:
You can use the varnishlog tool to view real-time web requests and for debugging Varnish.
We need to edit the Nginx configuration which we created in the first post to handle SSL/TLS termination and as a back-end for Varnish.
Code: Select all
nano /etc/nginx/conf.d/yourdomain.com.conf
Code: Select all
upstream fastcgi_backend {
server unix:/run/php-fpm/magento.sock;
}
server {
listen 127.0.0.1:8080;
server_name yourdomain.com www.yourdomain.com;
set $MAGE_ROOT /opt/magento/public_html;
set $MAGE_MODE developer; # or production
include snippets/letsencrypt.conf;
include /opt/magento/public_html/nginx.conf.sample;
}
server {
listen 443 ssl http2;
server_name www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/yourdomain.com/chain.pem;
include snippets/ssl.conf;
return 301 https://yourdomain.com$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/yourdomain.com/chain.pem;
include snippets/ssl.conf;
access_log /var/log/nginx/yourdomain.com-access.log;
error_log /var/log/nginx/yourdomain.com-error.log;
location / {
proxy_pass http://127.0.0.1;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
}
}
Code: Select all
nano /etc/nginx/nginx.conf
Code: Select all
...
# server {
# listen 80 default_server;
# listen [::]:80 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
...
Code: Select all
sudo systemctl reload nginx
Varnish is a fast reverse-proxy HTTP accelerator that will sit in front of our web server and it will be used as a Full Page Cache solution for our Magento installation.
Install Varnish via yum with the following command:
Code: Select all
sudo yum install varnish
Code: Select all
php /opt/magento/public_html/bin/magento config:set --scope=default --scope-code=0 system/full_page_cache/caching_application 2
Code: Select all
sudo php /opt/magento/public_html/bin/magento varnish:vcl:generate > /etc/varnish/default.vcl
The default configuration comes with a wrong URL for the health check file. Open the default.vcl file and remove the /pub part from the line highlighted in yellow:
Code: Select all
nano /etc/varnish/default.vcl
Code: Select all
...
.probe = {
# .url = "/pub/health_check.php";
.url = "/health_check.php";
.timeout = 2s;
.interval = 5s;
.window = 10;
.threshold = 5;
}
...
Code: Select all
nano /etc/varnish/varnish.params
Code: Select all
VARNISH_LISTEN_PORT=80
Code: Select all
sudo systemctl enable varnish
sudo systemctl start varnish