Nginx作为反向代理,并在后端发送标头时缓存


Nginx as reverse proxy and cache if backend sends a header

  1. 如果后端发送了头,Nginx是否可以缓存请求?也许包括TTL
  2. 是否可以使用请求使缓存无效?以饼干为例

我想从应用程序的逻辑控制缓存,而不是从nginx配置文件控制缓存,并且不要让请求到达apache.php。

http://wiki.nginx.org/HttpCoreModule#Variables给出了以下内容:

$sent_http_HEADER
The value of the HTTP response header HEADER when converted to lowercase and
with 'dashes' converted to 'underscores', 
e.g. $sent_http_cache_control, $sent_http_content_type...; 

$cookie_COOKIE
The value of the cookie COOKIE; 

如果你把它和if块结合起来,你可以做一些类似的事情:

if ($sent_http_your_added_header = "") { 
  proxy_cache your_cache_zone;
}
if ($cookie_BYPASS = "1") {
  proxy_bypass $cookie_BYPASS;
}

注意:如果您的BYPASS cookie的值为1或0,您实际上可以忘记if,只需使用$cookie_BYPASS,请参阅http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_bypass

就缓存时间而言http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid解释说,nginx响应"X-Accel-Expires"、"Expires"answers"Cache-Control"头(除非您告诉它不要使用proxy_ignore_headers指令)