User Agents

Web browsers and everyday web tech, explained

Speed Up Nginx with Varnish Cache on CentOS

· 4 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Log into your CentOS server via SSH as root or a user with sudo privileges.
  2. Install EPEL repository to access Varnish, as it’s not in the default CentOS repositories:
    yum install epel-release -y
    For CentOS 8, use:dnf install epel-release -y
  3. Install Varnish Cache:
    yum install varnish -y
    On CentOS 8:
    dnf install varnish -y
  4. Stop and disable Nginx temporarily to free port 80:
    systemctl stop nginx
    systemctl disable nginx
    Varnish will listen on port 80 and forward requests to Nginx on another port.
  5. Configure Nginx to listen on port 8080 instead of 80. Edit your Nginx server block (usually in /etc/nginx/conf.d/default.conf or /etc/nginx/nginx.conf):
    server {
        listen 8080;
        server_name your_domain.com;
        root /usr/share/nginx/html;
        index index.html;
        location / {
            try_files $uri $uri/ =404;
        }
    }
    Replace your_domain.com with your actual domain or IP.
  6. Test Nginx configuration and restart:
    nginx -t
    systemctl start nginx
    systemctl enable nginx
  7. Configure Varnish to forward requests to Nginx. Edit the Varnish default configuration:
    nano /etc/varnish/default.vcl
    Replace the backend section with:
    backend default {
        .host = "127.0.0.1";
        .port = "8080";
    }
    This tells Varnish to send all incoming requests to Nginx running on localhost:8080.
  8. Change Varnish listening port from 6081 to 80. Edit the Varnish service configuration:
    nano /etc/varnish/varnish.params
    Find the line VARNISH_LISTEN_PORT=6081 and change it to:
    VARNISH_LISTEN_PORT=80
  9. Start and enable Varnish:
    systemctl start varnish
    systemctl enable varnish
  10. Ensure firewalld allows traffic on port 80:
    firewall-cmd --permanent --add-service=http
    firewall-cmd --reload
  11. Test the setup by visiting your server’s IP or domain in a browser. You should see your site loading through Varnish.
  12. Verify Varnish is caching by checking response headers. Use curl:
    curl -I http://your_domain.com
    Look for X-Cache: HIT or MISS in the response. A HIT means content was served from cache.
  13. Optimize Varnish memory allocation. Edit /etc/varnish/varnish.params and set:
    VARNISH_VARNISHD_OPTS="-a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256M"
    Adjust 256M based on your RAM (e.g., 512M for 2GB systems).
  14. Monitor Varnish cache performance:
    varnishstat
    Watch for cache_hit and cache_miss to evaluate efficiency.

Common Mistakes

  • Forgetting to change Nginx to port 8080, causing port conflicts with Varnish.
  • Not updating varnish.params to 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 varnishlog to 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 varnishstat to adjust memory and TTL settings.
  • Use systemctl restart varnish after 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.