Share On Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit
Question

What is NGINX web caching?

Tags: nginx
Date:
Status:Resolved
Question Id:42

I work with nginx and I need caching. How can I caching on nginx ?

Answer
Date:
Correct:No

Like Varnish, Nginx is a very capable web cache. Many administrators reach for Varnish, often before it's really needed. However, there are two things to know about Nginx:

  • Nginx can serve static content (directly) very, very efficiently. This is good when the static files are on the same server as Nginx.
  • Nginx can also act as a "true" cache server when placed in front of application servers, just like you might with Varnish.

While Varnish is a pure web cache with more advanced cache-specific features than Nginx, Nginx may still be a perfect match for you.

If your traffic warrants adding a layer of infrastructure for caching, but not the overhead of introducing new technologies that need to be learned and maintained, Nginx might be a better fit.

This is especially true if you happen to use Nginx Plus, which comes with support and extra features.

Answer
Date:
Correct:Yes

It turns out that I needed to add a proxy_cache_valid directive (though it is not clear to me why this is necessary - I assumed simply using proxy_cache_valid in a location would turn on caching on its own).

My nginx.conf that worked (note the new line in bold):

events {}

http {
    proxy_cache_path /tmp/cache keys_zone=one:10m levels=1:2 inactive=2M max_size=100g;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # Static file server
    server {
        listen 127.0.0.1:8080;

        root /opt/nginx-test-data;

        location / {
        }
    }

    # Reverse proxy that talks to server defined above
    server {
        listen 127.0.0.1:8081;
        proxy_cache_min_uses 1;

        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_cache one;
            proxy_cache_valid 200 10m;
        }
    }
}

Your Answer

Review Your Answer