2012-08-13 66 views
3

当使用nginx fastcgi_cache时,我缓存HTTP 200响应比我做任何其他HTTP代码更长。我希望能够根据此代码有条件地设置expires标题。nginx:将条件过期头添加到fastcgi_cache响应

例如:

fastcgi_cache_valid 200 302 5m; 
fastcgi_cache_valid any  1m; 

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

是像上述可能的(内部的位置容器)?

回答

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...; 

,所以你可以在匹配$ sent_http_response if语句

有一个疑难杂症虽然因为http://nginx.org/en/docs/http/ngx_http_headers_module.html#expires如果公司所允许的范围内为到期指令

没有列出

你可以在if区块中设置一个变量,然后再引用它,如下所示:

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

你是我的朋友,是男人。谢谢! – rynop 2012-08-17 13:32:52