Are you confused on how to configure Varnish with Nginx server? We can help you with it.
Varnish cache helps to increase the website speed easily. But it requires additional steps to make it work with the Nginx web server.
At Bobcares, we often receive requests to install and configure Varnish as part of our Server Management Services.
Today lets see how our Support Engineers configure Varnish with the Nginx server.
More about Varnish cache
Before getting much into the error, let’s first discuss the Varnish cache in brief.
So, Varnish cache or caching HTTP reverse proxy is basically a web application accelerator. It works on the concept of HTTP caching. Varnish serves the contents from its cache. This again depends on the actual Varnish server settings.
Here, the caching allows the browser to store local copies of web resources for faster retrieval. Also, the webserver can attach cache headers to each item, say image, text file, etc. Thus the Nginx webserver can specify the desired cache behavior.
Moreover, it is well known for its flexible configuration language, VCL.
In short, proper Varnish setup increases the speed of a website and reduces the load on the webserver.
How we install and configure Varnish with Nginx server?
Let’s now discuss how we can install Varnish in an Nginx server.
Having a decade of experience in managing servers, our Dedicated Engineers deal with the Varnish installation frequently.
Firstly, we make sure that all our system packages are up-to-date. On an Ubuntu server, we use the command,
apt update
Then, we install Varnish using the below command,
apt install varnish
After the installation, we need to start the service using the below command,
service varnish start
Configuring Varnish with Nginx
Then, we need to reconfigure the Nginx default port 80 to some other listening port like 8080. So, we edit the default site using the command,
nano /etc/nginx/sites-available/default
And, then change the default port from 80 to 8080 as given below.
server {
listen 8080 default_server;
listen [::]:8080 default_server;
Then, we save the settings and reload the Nginx server using the below command,
service nginx reload
Now, we want to route traffic from Nginx through the Varnish cache server. So, we need to reconfigure the Varnish cache to listen on port 80.
To do so, we edit the file /lib/systemd/system/varnish.service and change the default port 6081 to port 80.
Next, we instruct Varnish to listen to Nginx port 8080. For that, we edit /etc/varnish/default.vcl. And, we give the Nginx IP address in the .host field and 8080 in the .port field.
Finally, we need to reload the systemd daemon and restart the Varnish cache server. We use the command,
systemctl daemon-reload
service varnish restart
We can check whether the Varnish cache works fine or not using the command,
varnishstat
When Nginx servers the request via Varnish caching, the curl output appears as:
It clearly shows the “ETag: W/”5e3e5afa-2aa6” as well as the Varnish/5.2 version.
[Need more assistance to install Varnish with Nginx?- We’re available 24/7 to help you.]
Conclusion
In today’s write up, we discussed how our Support Engineers install and configure Varnish with Nginx on an Ubuntu server.
0 Comments