关于 NGINX 的 proxy_cache_lock 的行为

2014-07-05 14:26:49 +08:00
 Livid
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock

有人了解这个 directive 打开之后的效果么?

proxy_cache_lock on;

根据文档,貌似如果一个大文件需要回源,那么打开之后,在同一时间就只会发起一个到源站的请求,然后其他对同样文件的请求就会等这个请求先完成。

但是这样的话,如果这个文件非常非常大,需要几分钟才能下载完的话,难道其他请求也就干等几分钟吗?
10466 次点击
所在节点    NGINX
9 条回复
aveline
2014-07-05 14:41:19 +08:00
是阻塞的 ... 所以这个功能很鸡肋。

是想实现 Squid 里的 Collapsed Forwarding 功能么?
Livid
2014-07-05 14:44:41 +08:00
@aveline 在测试到底是怎样的。

这里有篇博客说和 Squid 的类似:

https://blog.feuvan.net/2013/07/12/10135-nginx-proxy-cache-lock.html
lsylsy2
2014-07-05 14:46:59 +08:00
一直觉着Nginx的缓存做的不好,只用来做负载均衡,缓存还是交给Varnish和Squid
alex321
2014-07-05 15:00:20 +08:00
我在网上照抄了一堆做了基于 sslstart 的 google 反向代理给身边的小伙伴们使用,设置了 cache 2g,可能是访问量很小,目前还没遇到什么症状,也不知道这个缓存的价值如何。
aveline
2014-07-05 15:07:01 +08:00
在本地重新测试了一下,proxy_cache_lock 打开的时候,另一个同样 cache key 的请求在到达 proxy_cache_lock_timeout 之前是不会发起的。
Livid
2014-07-05 15:13:03 +08:00
aveline
2014-07-05 15:22:10 +08:00
@Livid 赞,看了下里面是用 redis 的 subscribe 实现的,太机智了 ...
Livid
2014-10-17 20:49:15 +08:00
@aveline 关于 proxy_cache_lock 的问题,今天又研究了一下。总算是找到 Nginx.org 官方的 Maxim Dounin 的答复了:

https://www.ruby-forum.com/topic/5010940

Hello!

On Mon, Jun 30, 2014 at 11:10:52PM -0400, Paul Schlie wrote:

> being seemingly why proxy_cache_lock was introduced, as you initially suggested.
Again: responses are not guaranteed to be the same, and unless
you are using cache (and hence proxy_cache_key and various header
checks to ensure responses are at least interchangeable), the only
thing you can do is to proxy requests one by one.

If you are using cache, then there is proxy_cache_key to identify
a resource requested, and proxy_cache_lock to prevent multiple
parallel requests to populate the same cache node (and
"proxy_cache_use_stale updating" to prevent multiple requests when
updating a cache node).

In theory, cache code can be improved (compared to what we
currently have) to introduce sending of a response being loaded
into a cache to multiple clients. I.e., stop waiting for a cache
lock once we've got the response headers, and stream the response
body being load to all clients waited for it. This should/can
help when loading large files into a cache, when waiting with
proxy_cache_lock for a complete response isn't cheap. In
practice, introducing such a code isn't cheap either, and it's not
about using other names for temporary files.

--
Maxim Dounin
http://nginx.org/

好想悬赏找人做这个功能。
Livid
2014-10-17 20:51:05 +08:00
嗯,必须找公司申请资源把这个功能做了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/121073

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX