Nginxでリバースプロキシするときに、バックエンドに同時に1つしか同じリクエストが行かないようにする方法

注意

うろ覚えなので間違っているかもしれない

本文

ここ http://www.1x1.jp/blog/2013/12/varnish-cache.html

当初、nginx の proxy_cache を試したのですが、同一 URL へ多数のリクエストが来ている状況でキャッシュのライフタイムが切れると、バックエンドへ同じ URL に対して複数のリクエストが飛んでしまい、PHP アプリケーション側に負荷がかかるという現象があったので、今回は取りやめました。

Varnish では、同一 URL に対して複数のリクエストがあっても、バックエンドへは 1 リクエストしか飛ばないようになっている。

とあったけど確かそれnginxでもできたと思いました。

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock

When enabled, only one request at a time will be allowed to populate a new cache element identified according to the proxy_cache_key directive by passing a request to a proxied server. Other requests of the same cache element will either wait for a response to appear in the cache or the cache lock for this element to be released, up to the time set by the proxy_cache_lock_timeout directive.

これ使えばできると思う。
ただし、

This directive appeared in version 1.1.12.

となっていて、CentOS 5系だと自分でnginxビルドしないと使えないみたいな感じだった気がする。