Install Varnish Cache 6 for Nginx on CentOS/RHEL
In this article
This guide walks you through installing Varnish Cache 6 as a reverse proxy in front of Nginx on CentOS or RHEL. Varnish sits between clients and your web server, caching responses to dramatically reduce server load and improve page load times. By the end, you'll have Varnish listening on port 80, Nginx on port 8080, and a working cache configuration.
What You'll Need
- A CentOS 7/8 or RHEL 7/8 server with root or sudo access.
- Nginx already installed and running on the default port 80.
- Firewalld or iptables configured to allow HTTP traffic (port 80).
- Basic familiarity with the command line and editing files with vi/nano.
Step-by-Step
- Update your system
Runsudo yum update -y(orsudo dnf update -yon RHEL 8) to ensure all packages are current. - Install the EPEL repository
Varnish is not in the default CentOS/RHEL repos. Install EPEL:sudo yum install epel-release -y(for RHEL 8, usesudo dnf install epel-release -y). - Install Varnish 6
Varnish 6 is available in EPEL. Install it withsudo yum install varnish -y. Verify the version withvarnishd -V; you should see 6.x. - Change Nginx to listen on port 8080
Edit/etc/nginx/nginx.conf(or your site config in/etc/nginx/conf.d/). Find thelisten 80;directive and change it tolisten 8080;. Save the file. - Update Nginx to respect real client IPs
Add the following inside theserver { }block (orhttp { }block) to pass the original client IP from Varnish:set_real_ip_from 127.0.0.1;real_ip_header X-Forwarded-For;
Save and test withsudo nginx -t. - Restart Nginx
Runsudo systemctl restart nginx. Verify it's listening on 8080 withsudo netstat -tulpn | grep 8080. - Configure Varnish to listen on port 80
Edit/etc/varnish/varnish.params(or/etc/varnish/varnish.serviceon some systems). Look for theVARNISH_LISTEN_PORTvariable and set it to80. Also setVARNISH_ADMIN_LISTEN_ADDRESSto127.0.0.1andVARNISH_ADMIN_LISTEN_PORTto6082. - Point Varnish to Nginx as the backend
Edit/etc/varnish/default.vcl. Find thebackend defaultsection and change the host and port to:backend default { .host = "127.0.0.1"; .port = "8080"; }
Save the file. - Start and enable Varnish
Runsudo systemctl start varnishandsudo systemctl enable varnish. Check status withsudo systemctl status varnish. - Adjust firewall
If firewalld is active, allow port 80:sudo firewall-cmd --permanent --add-port=80/tcp && sudo firewall-cmd --reload. If using iptables, add a rule for port 80. - Test the setup
Open a browser and visit your server's IP or domain. You should see your site. To confirm Varnish is caching, runcurl -I http://your-serverand look for theVia: 1.1 varnish-v6header in the response.
Common Mistakes
- Forgetting to change Nginx's port – If Nginx still listens on 80, Varnish can't bind to it, causing a conflict.
- Not updating the backend in default.vcl – Varnish will try to connect to the default port (usually 8080) only if you set it; otherwise it may use port 80 and loop.
- Firewall blocking port 80 – Even after installation, if the firewall doesn't allow HTTP, external users can't reach Varnish.
- SELinux blocking connections – On RHEL/CentOS with SELinux enforcing, Varnish may be denied connecting to Nginx on port 8080. Check audit logs with
sudo ausearch -m avc -ts recent.
Tips
- Use a cache-bypass for admin areas – Add VCL rules to skip caching for paths like /admin or /login to avoid serving stale content.
- Set proper cache headers – Ensure your Nginx config sends
Cache-ControlandExpiresheaders so Varnish knows what to cache and for how long. - Monitor cache hit ratio – Use
varnishstatto see the hit rate. A healthy ratio is above 80% for static-heavy sites. - Back up your VCL – Before making changes, copy
default.vcltodefault.vcl.bakso you can roll back easily.
Troubleshooting
Varnish won't start
Check the error with sudo systemctl status varnish or sudo journalctl -u varnish. Common causes: port 80 already in use (Nginx still on 80) or a syntax error in default.vcl. Run varnishd -C -f /etc/varnish/default.vcl to test the VCL syntax.
Site loads but no Varnish headers
If curl -I doesn't show Via or X-Varnish, you're likely hitting Nginx directly. Ensure Varnish is listening on port 80 and Nginx on 8080. Double-check your firewall and that you're accessing the correct IP.
Cache always misses
This often happens when the backend sends Set-Cookie or Cache-Control: private. Varnish by default doesn't cache responses with cookies. You can add VCL to strip cookies for static files or adjust the backend to avoid sending cookies on cacheable responses.
SELinux blocking
If you see permission errors in Varnish logs, try temporarily disabling SELinux with sudo setenforce 0 to test. If that fixes it, run sudo setsebool -P httpd_can_network_connect 1 to allow Varnish to make outbound connections to Nginx.
Port 80 already in use
Run sudo lsof -i :80 to see what's using it. If it's Nginx, change its port as described. If it's something else, stop that service or choose a different port for Varnish (but then you'll need to forward 80 to it).
Frequently Asked Questions
Can I use Varnish with SSL/HTTPS?
Varnish 6 doesn't natively support TLS termination. You can place Varnish behind an SSL terminator like HAProxy or Nginx (with SSL) that forwards decrypted traffic to Varnish on an internal port.
How do I purge the cache completely?
Run sudo varnishadm ban req.url ".*" to invalidate all cached objects. For a specific URL, use sudo varnishadm ban req.url "/path".
Will Varnish cache dynamic pages like PHP?
By default, Varnish caches any response that doesn't have a Set-Cookie or Cache-Control: private header. For PHP pages, you can cache them if they don't set cookies and you set appropriate cache headers.
How do I see what's in the cache?
Use varnishlog to see requests and hits, or varnishstat for summary counters. There's no built-in web UI, but you can install third-party tools like Varnish Dashboard.
Can I run Varnish and Nginx on the same server?
Yes, that's standard. Varnish listens on port 80, Nginx on a higher port like 8080, and Varnish forwards requests to Nginx.
What if I upgrade Nginx later?
Your Varnish config doesn't need changes as long as the backend port stays the same. Just ensure Nginx still listens on 8080.