3

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

上記のようなことは可能ですか (場所コンテナー内)?

4

1 に答える 1

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

そのため、if ステートメントで $sent_http_response に一致させることができます

ただし、http://nginx.org/en/docs/http/ngx_http_headers_module.html#expiresには、expires ディレクティブの許可されたコンテキストとしてリストされていないため、落とし穴があります。

if ブロックで変数を設定して、後で次のように参照することを回避できます。

set $expires_time 1m;
if ($send_http_response ~* "200") {
  set $expires_time 5m; 
}
expires $expires_time;
于 2012-08-16T21:00:08.287 に答える