Table of Contents
How to Set Up Varnish Cache on Your VPS
If you are running a website on a virtual private server and struggling with slow load times or high server load, implementing a Varnish cache VPS setup could be the solution you have been looking for. Varnish Cache is a powerful, open-source HTTP accelerator designed to dramatically speed up your web server by storing copies of pages in memory and serving them to visitors without repeatedly hitting your backend. In this guide, we will walk you through everything you need to know to get Varnish Cache up and running on your VPS.
What Is Varnish Cache and Why Does It Matter?
Varnish Cache is a web application accelerator, also known as a caching HTTP reverse proxy. When a visitor requests a page from your website, Varnish intercepts that request and checks whether it already has a cached version available. If it does, it serves that cached version almost instantly, bypassing your web server entirely. If it does not, it fetches the content from your backend, stores it, and then delivers it to the user.
The performance benefits of a Varnish cache VPS configuration are significant. Websites can see response times drop from hundreds of milliseconds to just a few milliseconds. This is particularly valuable for content-heavy websites, news portals, e-commerce platforms, and any site that experiences high traffic volumes. Faster load times also contribute positively to your search engine rankings, making Varnish an important tool not just for performance but for SEO as well.
Prerequisites Before You Begin
Before diving into the installation process, make sure you have the following in place:
- A VPS running Ubuntu 20.04 or 22.04 (this guide uses Ubuntu, though the steps are similar for Debian-based systems)
- Root or sudo access to your server
- A web server such as Apache or Nginx already installed and running
- Basic familiarity with the Linux command line
For more general guidance on managing your VPS effectively, you can visit da-manager.com/blog for a range of helpful tutorials and server management tips.
Step 1: Installing Varnish Cache on Your VPS
The first step in your Varnish cache VPS setup is to install the software itself. Ubuntu’s default repositories include Varnish, but it is generally recommended to install from the official Varnish repository to get the latest stable version.
Adding the Varnish Repository
Open your terminal and run the following commands to add the official Varnish repository:
curl -s https://packagecloud.io/varnishcache/varnish74/gpgkey | sudo apt-key add - sudo apt-get install -y apt-transport-https echo "deb https://packagecloud.io/varnishcache/varnish74/ubuntu/ focal main" | sudo tee /etc/apt/sources.list.d/varnishcache_varnish74.list sudo apt-get update sudo apt-get install varnish
Once the installation is complete, you can verify it by running varnishd -V to confirm the version installed.
Step 2: Configuring Your Web Server to Work With Varnish
By default, your web server (Apache or Nginx) listens on port 80. Since Varnish will now act as the front-facing layer, you need to move your web server to a different port, typically port 8080, and let Varnish listen on port 80 instead.
Changing the Apache Listening Port
If you are using Apache, open the ports configuration file:
sudo nano /etc/apache2/ports.conf
Change Listen 80 to Listen 8080. Then update your virtual host file to reflect the same change, replacing port 80 with 8080. Restart Apache once done:
sudo systemctl restart apache2
Changing the Nginx Listening Port
If you are using Nginx, open your server block configuration file and change the listen 80 directive to listen 8080. Save the file and restart Nginx:
sudo systemctl restart nginx
Step 3: Configuring Varnish to Listen on Port 80
Now you need to configure your Varnish cache VPS setup so that Varnish listens on port 80 and forwards requests to your web server on port 8080.
Editing the Varnish Service File
On Ubuntu systems using systemd, you will need to edit the Varnish service file rather than the default configuration file:
sudo systemctl edit --full varnish
Find the line that begins with ExecStart and modify the -a flag to specify port 80:
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
Save and close the file, then reload the systemd daemon and restart Varnish:
sudo systemctl daemon-reload sudo systemctl restart varnish
Step 4: Editing the Varnish VCL Configuration File
Varnish uses a domain-specific language called VCL (Varnish Configuration Language) to define how requests and responses should be handled. The main configuration file is located at /etc/varnish/default.vcl.
Setting the Backend
Open the VCL file and ensure the backend definition points to your web server on port 8080:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
This tells Varnish to forward all uncached requests to your local web server. Save the file and restart Varnish to apply the changes.
Step 5: Testing Your Varnish Cache VPS Setup
Once everything is configured, it is essential to verify that Varnish is working correctly. You can use the curl command to inspect response headers:
curl -I http://yourdomain.com
Look for a header that reads X-Varnish or Via: 1.1 varnish in the response. If you see these headers, Varnish is successfully intercepting and serving requests. You should also see an Age header on subsequent requests, indicating that content is being served from cache.
Step 6: Fine-Tuning Your Varnish Configuration
A basic Varnish installation will already provide noticeable performance improvements, but there are several additional tweaks worth considering for a more robust Varnish cache VPS environment.
Increasing Cache Memory
By default, Varnish allocates 256MB of memory for caching. For high-traffic websites, you may want to increase this. In the ExecStart line of your service file, adjust the -s malloc value accordingly, for example to 512m or 1g depending on your VPS resources.
Excluding Certain Pages From Caching
Not all content should be cached. Login pages, checkout pages, and admin areas should typically be excluded. You can add logic to your VCL file to handle this:
sub vcl_recv {
if (req.url ~ "^/wp-admin" || req.url ~ "^/checkout") {
return (pass);
}
}
This ensures that sensitive or dynamic pages are always fetched fresh from your backend.
Conclusion
Setting up a Varnish cache VPS configuration is one of the most effective ways to boost your website’s performance and reduce the load on your server. By acting as a high-performance intermediary between your visitors and your web server, Varnish can handle thousands of requests per second with minimal resource usage. Whether you are running a small blog or a large e-commerce platform, the steps outlined in this guide will help you get Varnish up and running quickly and efficiently. With proper configuration and fine-tuning, you will notice a dramatic improvement in page load speeds, user experience, and ultimately, your search engine visibility.














