【问题标题】:nginx: add conditional expires header to fastcgi_cache responsenginx:将条件过期标头添加到 fastcgi_cache 响应
【发布时间】:2012-08-10 04:14:21
【问题描述】:

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

例如:

fastcgi_cache_valid   200 302  5m;
fastcgi_cache_valid   any      1m;

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

是否可能出现上述情况(在位置容器内)?

【问题讨论】:

    标签: caching nginx fastcgi


    【解决方案1】:

    当然,来自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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 2021-02-17
      • 2016-05-15
      • 2019-09-05
      相关资源
      最近更新 更多