加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 20:52:57 所属栏目:Nginx 来源:网络整理
导读:使用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

使用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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读