5.3 Cache Bypass 绕过缓存

Problem

You need the ability to bypass the caching.

问题

将一些内容不进行缓存


Solution

Use the proxy_cache_bypass directive with a nonempty or nonzero

value. One way to do this is by setting a variable within location

blocks that you do not want cached to equal 1:

proxy_cache_bypass $http_cache_bypass;

The configuration tells NGINX to bypass the cache if the HTTP

request header named cache_bypass is set to any value that is not 0.

解决方案

将 proxy_cache_passby 指令,设置称非空值或非 0。一种途径是,在 location

块级指令中设置一个值等于 1 的 proxy_cache_passby 指令:

proxy_cache_bypass $http_cache_bypass;

配置告知 NGINX 服务器,如果一个 HTTP cache_passby 请求头的值设置为非

0(或非空),则不对该请求进行缓存处理。


Discussion

There are many scenarios that demand that the request is not

cached. For this, NGINX exposes a proxy_cache_bypass directive

that when the value is nonempty or nonzero, the request will be sent

to an upstream server rather than be pulled from cache. Interesting

techniques and solutions for cache bypass are derived from the need

of the client and application. These can be as simple as a request

variable or as intricate as a number of map blocks.

For many reasons, you may want to bypass the cache. One impor‐

tant reason is troubleshooting and debugging. Reproducing issues

can be hard if you’re consistently pulling cached pages or if your

cache key is specific to a user identifier. Having the ability to bypass

the cache is vital. Options include but are not limited to bypassing

cache when a particular cookie, header, or request argument is set.

You can also turn off cache completely for a given context such as a

location block by setting proxy_cache off;

结论

挺多应用场景下都不应对请求进行缓存处理,对此,NGINX 提供 proxy_cache_passby

指令来应对这些场景。通过将指令值设置为非空或非零,匹配的请求 URI 会直接发送给

被代理服务器,而不是从缓存中获取。如何使用该指令,需要结合客户端和应用的实际

使用。它既可以配制成如同一个请求变量一样简单,也可以配置成复杂的映射指令块。

但最终目的都是绕过缓存。其中,一个重要的应用场景就是排除故障和调试应用。如果

在研发过程中一直使用缓存,或对特定用户进行缓存,缓存会影响问题的复现。提供对指定

cookie、请求头(headers)或请求参数等的缓存绕过能力,则是一个必要的功能。此外,

NGINX 服务器还能够在 location 块指令中将 proxy_cache 指令设置为 off,完全禁用

缓存。

results matching ""

    No results matching ""