• *Wellcome Guest This Forum is created to help each other. Join us in our journey.

    * Guest Earn Piont Every Post, like, comment,and etc.

How to Enable and Disable Nginx Cache

K

Karim Buzdar

Guest
When you have enabled caching in Nginx Plus, it stores responses in a cache disk, and these are further used to respond to clients without holding a proxy request for every time with the same content. Nginx Plus’s caching has more capabilities in which the most useful features, such as cache purging, delayed caching, and dynamic content caching, are included.

In this article, we will learn more about caching, such as how to enable and disable the caching in an Nginx server on a Linux system.

How to Enable Caching?


In the top level of the http {} context, include a directive the proxy_cache_path to enable caching. The first parameter, which is the local filesystem path for cached content, and the parameter keys_zone that defines the size and name of the shared memory zone, are mandatory. The last parameter, keys_zone, stores the metadata of cached items:

http {

...

proxy_cache_path /data/nginx/cache keys_zone=one:10m;

}

You have to include the proxy_cache directive to define the items such as (protocol type and location or virtual server address) in the http context. Through which you want to cache server responses, mention the zone name, which is defined by a parameter the keys_zone to the directive proxy_cache_path (which is one in this case):

http {

...

proxy_cache_path /data/nginx/cache keys_zone=one:10m;

server {

proxy_cache mycache;

location / {

proxy_pass http://localhost:8000;

}

}

}

It is noted that the total amount of cached response is not affected by the size, which is defined in the keys_zone parameter. All cached responses separately themselves are saved in specific files with a copy of the metadata on your filesystem. However, if you want to limit the total cached response data amount, then you may include the parameter max_size to the directive in proxy_cache_path.

How to do limit or disable Caching?


All responses remain stored in the cache indefinitely. These responses are only removed when it exceeds the defined maximum size and the time of length since they last were requested. But, you can set according to your convenience means for how much time these cached responses are considered valid or even if they are used by different directives in the server {}, http {} or in the context of location {}. However, to limit the cached responses considered as valid, you need to include a directive with the name proxy_cache_valid.

Let’s define the cache limiting concept with an example. In the below given example, 200 or 302 code responses are considered to be valid for 10 minutes, and 404 responses are valid till 1 minute.

proxy_cache_valid 200 302 10m;

proxy_cache_valid 404 1m;

So, you can also define the validity of the cached responses of time for with all status codes to define a parameter ‘any’ that you can also see in the below line of code :

proxy_cache_valid any 5m;

There are some set of conditions under which Nginx does not send cached responses to the clients, so include a directive proxy_cache_bypass. Each parameter in the below example defines conditions and has a number of variables. If at least one parameter is not equal to zero ‘0’ or not empty then, Nginx does not find the response in the cache and requests immediately forward to the backend server.

proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;

Under the condition where you want that Nginx does not cache a response. Then, you will include the proxy_no_cache directive and define all the following parameters:

proxy_no_cache $http_pragma $http_authorization;
Conclusion


Caching provides more features in an Nginx server. We have also explored in this article how we can enable or disable caching, including all directives and parameters. To explore more, you can get more help from online resources. I hope the above caching article will be informative for you.

Credit to linux
 

Latest posts

Latest threads

Top