2012-08-13 67 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