How to install and set up Varnish Cache 6 with Nginx on CentOS 8
How to install and set up Varnish Cache 6 with Nginx on CentOS 8
Varnish Cache is a free and open source reverse proxy HTTP accelerator for websites and APIs. It can be installed in front of any web server running on HTTP as a reverse proxy. It is located between the web server and the browser, and saves web pages, cookies and other data in the memory. This cache will be used to satisfy all future requests for completely similar content. This will increase the web application load and increase the performance of the web server by more than 300 times.
In this tutorial, we set up the Nginx server as a backend server and configured it to listen on port 8080, and then configure the Varnish cache to listen on the default HTTP port 80.
prerequisites
- Server running CentOS 8.
- The root password is configured for the server.
Install and configure Nginx web server
First, you need to install Nginx web server in your system. You can use the following command to install:
dnf install nginx -y
After the installation is complete, start the Nginx service, and use the following command to start it when the system restarts:
systemctl start nginxsystemctl enable nginx
By default, Nginx listens on port 80. Therefore, you need to configure Nginx to listen on port 80. You can configure it by editing the Nginx default configuration file:
nano /etc/nginx/nginx.conf
Find the following lines:
listen 80 default_server; listen [::]:80 default_server;
And, replace them with the following lines:
listen 8080 default_server; listen [::]:8080 default_server;
Save and close the file when you are done. Then, restart the Nginx service to apply the changes:
systemctl restart nginx
At this point, the Nginx web server has been installed and is listening on port 8080. You can check it with the following command:
netstat -tpln | grep 8080
You should get the following output:
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 30367/nginx: master tcp6 0 0 :::8080 :::* LISTEN 30367/nginx: master
Install varnish cache
By default, the Varnish package is available in the CentOS 8 default repository. You can install it by running the following command:
dnf module install varnish
After the installation is complete, use the following command to start the Varnish service:
systemctl start varnish
You can also use the following command to verify the installed version of Varnish cache:
varnishd -V
You should see the following output:
varnishd (varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91) Copyright (c) 2006 Verdens Gang AS Copyright (c) 2006-2018 Varnish Software AS
Configure Varnish for Nginx
Next, you need to configure Varnish to run on port 80 to receive HTTP requests from clients. You can configure it by editing the Varnish default configuration file:
systemctl edit --full varnish
Find the following line:
ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
And, replace it with the following line:
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
Save and close the file when you are done.
Next, you will need to define the backend server for Varnish. You can do this by editing the main Varnish configuration file /etc/varnish/default.vcl:
nano /etc/varnish/default.vcl
Change the following lines: ads
backend myserver { .host = "127.0.0.1"; .port = "8080"; }
Save and close the file when you are done. Then, reload the systemd daemon using the following command:
systemctl daemon-reload
Next, restart the Varnish service and use the following command to start it when the system restarts:
systemctl restart varnishsystemcl enable varnish
You can also use the following command to verify the state of the varnish:
systemctl status varnish
You should get the following output:
? varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/etc/systemd/system/varnish.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2020-08-29 09:36:58 EDT; 12s ago Process: 30421 ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS) Main PID: 30424 (varnishd) Tasks: 217 Memory: 91.8M CGroup: /system.slice/varnish.service ??30424 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m ??30434 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m Aug 29 09:36:57 centos8 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator... Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91 Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Aug 29 09:36:58 centos8 varnishd[30424]: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91 Aug 29 09:36:58 centos8 varnishd[30424]: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Child (30434) Started Aug 29 09:36:58 centos8 varnishd[30424]: Child (30434) Started Aug 29 09:36:58 centos8 varnishd[30424]: Child (30434) said Child starts Aug 29 09:36:58 centos8 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
You can now use the following command to verify the listening ports of Nginx and Varnish:
netstat -tpln | grep 80
You should see the following output:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 30424/varnishd tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 30367/nginx: master tcp6 0 0 :::80 :::* LISTEN 30424/varnishd tcp6 0 0 :::8080 :::* LISTEN 30367/nginx: master
Configure SELinux and firewall
By default, SELinux is enabled in CentOS 8. Therefore, you need to configure SELinux for Varnish. You can configure it with the following command:
setsebool -P httpd_can_network_connect 1
Next, you will need to allow port 80 to pass through Firewalld. You can use the following commands to do so:
firewall-cmd --permanent --zone public --add-port 80/tcpfirewall-cmd --reload
Once completed, you can proceed to the next step.
Test varnish cache
At this point, the Nginx web server will be used to install and configure Varnish cache. Now, you can test whether the Varnish cache is working properly.
You can test it with curl command as follows:
curl -I http://localhost
You should see the following output:
HTTP/1.1 200 OK Server: nginx/1.14.1 Date: Sat, 29 Aug 2020 13:53:44 GMT Content-Type: text/html; charset=UTF-8 X-Powered-By: PHP/7.2.24 X-Varnish: 32800 Age: 0 Via: 1.1 varnish (Varnish/6.0) Accept-Ranges: bytes Connection: keep-alive
in conclusion
Congratulations! You have successfully installed Varnish cache with Nginx on CentOS 8. Hope you can also easily configure Varnish cache using Apache web server. If you have any questions, please feel free to ask me.