nginx,

How to configure basic caching in Nginx

Feb 04, 2022 · 1 min read · Post a comment
How to configure basic caching in Nginx

As the cache need grows for a big data applications, technology market offers a lot of different caching solutions. Today I want to show you how to set up basic caching and cache your site content on a server level using Nginx.

Prerequisites

  • Nginx

Configure Basic Caching in Nginx

You need to play with two Nginx directives to set up caching. The first one is proxy_cache_path where you need to set a path for the cache and the second is proxy_cache which will enable the cache.

The basic cache configuration should look like this:

proxy_cache_path /path/to/cache levels=1:2 keys_zone=devcoops_cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
    # ... another config
    location / {
        proxy_cache devcoops_cache;
        proxy_pass http://devcoops_upstream;
    }
}

Here I will provide you with an Nginx cache config that is more effective and increases the performance:

proxy_cache_path /tmp/cache keys_zone=devcoops_cache:10m levels=1:2 inactive=600s max_size=100m use_temp_path=off;

server {
    # ... another config
    location / {
        proxy_cache devcoops_cache;
        proxy_cache_lock on;
        proxy_cache_valid 200 1s;
        proxy_cache_use_stale updating;
    }
}

Conclusion

Of course, you can use a specific location /path to cache your data. To find out more about the proxy_cache modules you can refer to the Nginx documentation or put a comment below. On a side note, follow our official channel on Telegram.