缓存-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;

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...