gpt4 book ai didi

caching - nginx:将条件过期 header 添加到 fastcgi_cache 响应

转载 作者:行者123 更新时间:2023-12-01 07:45:30 25 4
gpt4 key购买 nike

当使用 nginx fastcgi_cache 时,我缓存 HTTP 200 响应的时间比我缓存任何其他 HTTP 代码的时间都长。我希望能够根据此代码有条件地设置过期 header 。

例如:

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 以来还是有问题的没有将 if 列为 expires 指令的允许上下文

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

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

关于caching - nginx:将条件过期 header 添加到 fastcgi_cache 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11942677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com