使用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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。