If your website is running on Apache or Nginx and traffic spikes cause slowdowns or outages, Varnish can solve that. Varnish is an open-source HTTP cache that sits in front of your web server, stores pages in memory, and serves them to visitors without hitting your backend. Your origin server only handles requests for content that isn’t cached.
How Varnish works
Imagine your VPS runs WordPress with Apache. Without caching, every visitor triggers PHP processing and database queries. With 100 concurrent visitors, Apache struggles. Add Varnish in front, and it serves the same cached page to all 100 visitors from memory. Apache only gets hit when the cache expires or a visitor does something unique (like logging in).
The result: your site handles thousands of requests per second with minimal CPU and RAM on the origin server.
Setting up Varnish on a VPS
A common setup uses two virtual servers:
- Origin server: your Linux VPS running WordPress, Apache/Nginx, and MySQL
- Varnish server: a second VPS running Varnish, pointed at the origin server
Point your domain’s DNS at the Varnish server. It fetches pages from the origin, caches them, and serves them to visitors. This also hides your origin server’s IP address, since only the Varnish server is exposed to the internet.
You can also run Varnish on the same server as your web application, with Varnish listening on port 80 and Apache/Nginx on a different port.
When to use Varnish
Varnish works best for sites that serve mostly the same content to most visitors (blogs, news sites, product pages, documentation). It’s less useful for highly dynamic, personalized content where every page is different for every user.
For WordPress sites, Varnish combined with a cache-clearing plugin means your content updates are reflected almost immediately while still benefiting from caching for the vast majority of requests.
We’ve used Varnish at ServerPoint for years. If you want help setting it up on your VPS hosting, contact us.