Nginx:向FastCGI缓存响应添加有条件的过期标头

3
使用nginx fastcgi_cache时,我将HTTP 200响应缓存时间设置得比其他HTTP代码更长。我希望能够根据这个代码有条件地设置过期标头。
例如:
fastcgi_cache_valid   200 302  5m;
fastcgi_cache_valid   any      1m;

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

上述内容(在位置容器内)是否可能实现?
1个回答

3

可以,来自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 没有将 if 列为允许 expires 指令的上下文。

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接