Caching in NGINX

Share:

NGINX provides a robust and flexible toolset for managing caching. Through caching, you effectively decrease the load on your servers and increase your application's responsiveness. In this article, we will delve deep into the world of NGINX Caching discussing its concept, how it works, and some practical examples.

Caching is primarily all about storing the output of a request to be reused later for similar requests. NGINX, just like in any other websites, works based on something we'll call the "Matrix Reloaded" principle. If a request is made which has already been completed before(“Matrix Reloaded”), NGINX will not go through the entire request process again. Instead, it will pull out the stored response (or the cached response) and send it back to the client. This makes the whole process extremely efficient and quick.

To start hitching a ride on this high-speed cache autobahn, we first need to understand how to configure our NGINX server to cache responses. By modifying the nginx.conf file, we are able to specify what needs to be cached, for how long, and where the cache should be stored. Here is a simple configuration example:

http {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=trinity_cache:10m;
    server {
        listen 80;
        location / {
            proxy_cache trinity_cache;
            proxy_pass http://localhost:8080;
        }
    }
}

In this configuration, we define a cache path /var/cache/nginx with a key zone named trinity_cache that has a maximum size of 10 MB. Each unique response from the proxied server (http://localhost:8080) is stored in trinity_cache. For any request hitting our server at port 80, NGINX checks to see if an equivalent response is already in the cache (comparing by the cache key, which default is $scheme$proxy_host$request_uri). If a match is found, the cached version is returned instantly.

But perhaps, you want to extend the role of Agent Smith and want to control the caching behavior for individual locations or servers. That’s where the proxy_cache_valid takes the stage. With it, you can set how long different status codes should be cached for:

location / {
    proxy_cache trinity_cache;
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;
    proxy_pass http://localhost:8080;
}

In the above scenario, 200 and 302 response status codes are cached for 10 minutes while the 404 responses are cached for just a minute.

Now, you've set the caching strategy like a pro but you’re also mindful that not everything should be cached. Think of it as knowing that some parts of the Matrix needs real-time data, one that caching could disrupt. NGINX offers proxy_no_cache and proxy_cache_bypass directives which can prevent responses from being cached or bypass the cache for certain requests. Here’s how you can use them:

location / {
    proxy_cache trinity_cache;
    proxy_no_cache $cookie_nocache $arg_nocache;
    proxy_cache_bypass $cookie_nocache $arg_nocache;
    proxy_pass http://localhost:8080;
}

In the above script, the value of the $cookie_nocache and $arg_nocache variables are checked. If either is set, the cache is not used for this request.

By this point, you should have a solid understanding of how NGINX applies caching to better its performance. But before you dash off, there are a few more configuration directives you should be aware of.

The proxy_cache_lock directive ensures that only one request at a time is allowed to populate a new cache element. This is useful when dealing with a "Dog Pile" effect or a sudden Stampedo of requests all at once.

location / {
    proxy_cache trinity_cache;
    proxy_cache_lock on;
    proxy_pass http://localhost:8080;
}

The proxy_cache_use_stale directive instructs NGINX to use stale cache items when an error occurs or when the server is slow to respond. This helps in keeping the user experience smooth even when unanticipated problems arise.

location / {
    proxy_cache trinity_cache;
    proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    proxy_pass http://localhost:8080;
}

Finally, let's look at the proxy_cache_revalidate directive which instructs NGINX to use conditional GET when refreshing items in the cache.

location / {
    proxy_cache trinity_cache;
    proxy_cache_revalidate on;
    proxy_pass http://localhost:8080;
}

To wrap it all up, NGINX can serve as an effective cache engine for your web applications. Whether you are directing the scenes of "The Matrix" or streaming loads of data to end-users, caching done right will result in efficiency and speed. NGINX equips you with several directives to fine-tune your caching needs. Whether you want to cache different status codes, bypass cache for certain requests, or handle a massive simultaneous hit of requests with proxy_cache_lock, NGINX's got you covered.

Learning to harness the power of NGINX cache can drastically improve your application's performance. Take your time to experiment and fine-tune your caching strategies, and you will see the real potential of NGINX caching and how it can lead to a smoother and faster experience for your users.

0 Comment


Sign up or Log in to leave a comment


Recent job openings