Call Us! 1-800-383-5193
Call Us! 1-800-383-5193

Need Help?

Our experts will login to your server within 30 minutes to fix urgent issues.

We will keep your servers stable, secure and fast at all times for one fixed price.

Configure Varnish with Nginx – The right way

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:

configure_varnish_with_nginx

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.


PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

varnish

Submit a Comment

Your email address will not be published. Required fields are marked *