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

标签 caching nginx fastcgi

当使用 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/

相关文章:

java - Spring 4如何定时刷新ConcurrentMapCache?

PHP-FPM fastcgi_finish_request靠谱吗?

php - IIS php Mysql - 数据库插入值在 XAMPP 和 WAMP 上工作,但在 IIS 上不起作用

python - 迁移到 mysql-django-uwsgi-nginx 期间出现内部服务器错误

ruby-on-rails - 让 Nginx 转发到 EC2 上的 Thin

php - 如何在lighttpd服务器中同时运行多个php版本?

php - CakePHP 中的文件或 APC 缓存引擎?

Android 2.1 View 的 getDrawingCache() 方法总是返回 null

performance - 是否可以使用 CompUnit 模块收集数据?

php - nginx - 某些 ajax 请求的 502 Bad Gateway