Dump the code

Bypass the cache

Created 8 months ago
Posted By admin
3min read
The proxy_no_cache directive is used to control whether a response should be cached. When a request matches a location block that includes proxy_no_cache, NGINX will bypass caching for that specific request.
 
The syntax is:
proxy_no_cache condition;
The condition is evaluated. If the condition is true, caching is bypassed for the current request.
The condition can be based on variables, headers, or any other expression that evaluates to a boolean value.

If the condition is true, NGINX does not store the response in the cache, even if caching is enabled for the location block.

For requests where caching is bypassed, NGINX fetches the content directly from the upstream server and serves it to the client.

For requests that do not match the condition in proxy_no_cache, normal caching behavior is applied based on other caching directives like proxy_cache and proxy_cache_valid.

Here's an example to illustrate the use of proxy_no_cache:

location /example {
    proxy_pass http://backend;
    proxy_cache my_cache;
    proxy_cache_key $host$uri$is_args$args;

    # Bypass cache for requests with a specific query parameter
    if ($arg_nocache) {
        proxy_no_cache 1;
    }

    # Other proxy settings...
}
In this example, if a request to /example includes the query parameter nocache, the proxy_no_cache condition is true, and caching is bypassed for that specific request.


The proxy_cache_bypass

The proxy_cache_bypass directive is used to control whether a request should bypass the proxy cache and be fetched directly from the upstream server, ignoring the cached content. This directive is typically used within the context of the location block that defines the proxy pass settings.

The proxy_cache_bypass directive allows you to specify a condition (using an expression) under which the cache should be bypassed. If the condition is true, NGINX will fetch the content directly from the upstream server, skipping the cache.
 
   location / {
       proxy_pass http://backend;
       proxy_cache my_cache;
       proxy_cache_key $host$uri$is_args$args;
       proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
       # Other proxy settings...
   }
In this example, the proxy_cache_bypass directive is configured with a condition specified by the expression $cookie_nocache $arg_nocache$arg_comment. If any of these variables are present in the request, and their values are non-empty, the cache will be bypassed.

   - $cookie_nocache: Bypass cache if the specified cookie is present.
   - $arg_nocache$arg_comment: Bypass cache if the specified query parameters are present.

The expression can include variables, and NGINX evaluates the expression to determine whether the cache should be bypassed. If the expression evaluates to true, the cache is bypassed; otherwise, the cached content is served.


In summary, while both directives are related to caching, proxy_no_cache determines whether to cache a response, and proxy_cache_bypass determines whether to bypass the cache and fetch content directly from the upstream server. They can be used together for more fine-grained control over caching behavior in NGINX.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles