缓存-Nginx:向fastcgi_cache响应添加条件到期标头

使用nginx fastcgi_cache时,与其他任何HTTP代码相比,我缓存HTTP 200响应的时间更长.我希望能够根据此代码有条件地设置expires标头.

例如:

fastcgi_cache_valid   200 302  5m;
fastcgi_cache_valid   any      1m;

if( $HTTP_CODE = 200 ) {
  expires 5m;
}
else {
  expires 1m;
}

是否可能发生上述情况(在位置容器内)?
最佳答案
当然,从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...; 

因此您可以在if语句中匹配$sent_http_response

但是有一个陷阱,因为http://nginx.org/en/docs/http/ngx_http_headers_module.html#expires没有列出expires指令的允许上下文

您可以解决在if块中设置变量的问题,然后稍后像这样引用它:

set $expires_time 1m;
if ($send_http_response ~* "200") {
  set $expires_time 5m; 
}
expires $expires_time;

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。