Speed Up Nginx with Varnish Cache on CentOS
In this article
Speed up Nginx with Varnish Cache on CentOS by placing Varnish in front of Nginx as a reverse proxy caching layer. This setup allows Varnish to serve cached static and dynamic content directly from memory, drastically reducing response times and lowering server load. Nginx continues to handle SSL termination, static file serving, and PHP processing, while Varnish caches frequent requests, minimizing repeated backend processing. This guide walks you through installing, configuring, and testing Varnish with Nginx on CentOS 7 or CentOS 8, ensuring optimal performance.
What You'll Need
- A CentOS 7 or CentOS 8 server with root or sudo access
- Nginx already installed and serving a website
- Domain or public IP pointing to your server
- Basic familiarity with command-line and text editors like nano or vim
- At least 1GB of RAM (2GB recommended for better caching)
Step-by-Step
- Log into your CentOS server via SSH as root or a user with sudo privileges.
- Install EPEL repository to access Varnish, as it’s not in the default CentOS repositories:
For CentOS 8, use:yum install epel-release -ydnf install epel-release -y - Install Varnish Cache:
On CentOS 8:yum install varnish -ydnf install varnish -y - Stop and disable Nginx temporarily to free port 80:
Varnish will listen on port 80 and forward requests to Nginx on another port.systemctl stop nginx systemctl disable nginx - Configure Nginx to listen on port 8080 instead of 80. Edit your Nginx server block (usually in
/etc/nginx/conf.d/default.confor/etc/nginx/nginx.conf):
Replaceserver { listen 8080; server_name your_domain.com; root /usr/share/nginx/html; index index.html; location / { try_files $uri $uri/ =404; } }your_domain.comwith your actual domain or IP. - Test Nginx configuration and restart:
nginx -t systemctl start nginx systemctl enable nginx - Configure Varnish to forward requests to Nginx. Edit the Varnish default configuration:
Replace the backend section with:nano /etc/varnish/default.vcl
This tells Varnish to send all incoming requests to Nginx running on localhost:8080.backend default { .host = "127.0.0.1"; .port = "8080"; } - Change Varnish listening port from 6081 to 80. Edit the Varnish service configuration:
Find the linenano /etc/varnish/varnish.paramsVARNISH_LISTEN_PORT=6081and change it to:VARNISH_LISTEN_PORT=80 - Start and enable Varnish:
systemctl start varnish systemctl enable varnish - Ensure firewalld allows traffic on port 80:
firewall-cmd --permanent --add-service=http firewall-cmd --reload - Test the setup by visiting your server’s IP or domain in a browser. You should see your site loading through Varnish.
- Verify Varnish is caching by checking response headers. Use curl:
Look forcurl -I http://your_domain.comX-Cache: HITorMISSin the response. A HIT means content was served from cache. - Optimize Varnish memory allocation. Edit
/etc/varnish/varnish.paramsand set:
AdjustVARNISH_VARNISHD_OPTS="-a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256M"256Mbased on your RAM (e.g., 512M for 2GB systems). - Monitor Varnish cache performance:
Watch forvarnishstatcache_hitandcache_missto evaluate efficiency.
Common Mistakes
- Forgetting to change Nginx to port 8080, causing port conflicts with Varnish.
- Not updating
varnish.paramsto listen on port 80, leaving Varnish on 6081. - Skipping firewall configuration, blocking external access to port 80.
- Using default low memory allocation, leading to poor cache performance.
- Assuming all content is cacheable—dynamic pages may need custom VCL rules to avoid caching.
Tips
- Use
varnishlogto debug request flow and caching decisions. - Set cache TTL in VCL for specific content types (e.g., images, HTML).
- Consider using Nginx for SSL termination and proxying to Varnish on port 80, but that requires more complex routing.
- Regularly monitor
varnishstatto adjust memory and TTL settings. - Use
systemctl restart varnishafter any VCL or params change.
Troubleshooting
If the site doesn’t load, first check if both Nginx and Varnish are running: systemctl status nginx and systemctl status varnish. If Varnish fails to start, verify port 80 isn’t in use with netstat -tuln | grep :80. If Nginx isn’t responding on 8080, test with curl http://127.0.0.1:8080. If Varnish shows no cache hits, ensure the backend is correctly set to 8080 and that content is cacheable. Use varnishlog -g request -q "ReqUrl eq '/'" to trace a specific page. If you get 503 errors, the backend (Nginx) may be unreachable—confirm it’s running and listening on 8080. If changes don’t take effect, restart Varnish after editing config files.
Frequently Asked Questions
Can I use Varnish with HTTPS?
Varnish does not natively support SSL/TLS. Use Nginx or a load balancer to terminate SSL and forward HTTP to Varnish on port 80.
Does Varnish cache dynamic content?
By default, Varnish may not cache dynamic pages. You must adjust VCL rules to cache based on cookies, headers, or URL patterns.
How much RAM should I allocate to Varnish?
Start with 256MB to 1GB, depending on traffic and content size. Monitor varnishstat and increase if cache miss rate is high.
What happens if Varnish crashes?
If Varnish stops, traffic won’t reach Nginx unless you re-enable port 80 on Nginx. Monitor Varnish with tools like systemctl or external checks.
Can I run Varnish and Nginx on different servers?
Yes. Configure Varnish to forward to a remote Nginx server by changing the backend IP in default.vcl.
How do I clear the Varnish cache?
Use varnishadm 'ban.url .' ' to purge all cache, or ban.url /specific-page for targeted purging.