Nginx缓存锁定总是导致500 ms延迟

wljmcqd8  于 5个月前  发布在  Nginx
关注(0)|答案(2)|浏览(58)

Nginx缓存锁定请求(http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock)总是需要500毫秒才能响应
$ab -n 2 -c 2 http://192.168.12.103/test1234
access.log:
192.168.12.103- [12/Sep/2017:02:34:59 -0700]“GET /test1234 HTTP/1.0”200 12“-”“ApacheBench/2.3”“-”127.0.0.1:9095 0.002 0.002 MISS
192.168.12.103- [12/Sep/2017:02:34:59 -0700]“GET /test1234 HTTP/1.0”200 12“-”“ApacheBench/2.3”“-”- - 0.502 HIT
我知道它缓冲到一个临时文件,并将其复制到该高速缓存。但500毫秒看起来很大。有人知道为什么吗?
如果你能帮忙的话,我将不胜感激。
设置信息:

  1. Test upstream server
    1.缓存位于tmpfs中(mount -t tmpfs -o size= 512 m tmpfs /tmpfs)
  2. nginx版本:1.7.2.1

nginx配置

worker_processes  1;
error_log  logs/error.log;
daemon off;
pid        /var/run/nginx.pid;
events {}
http {
    proxy_cache_path /tmpfs/local_cache keys_zone=local_cache:250m levels=1:2 inactive=8s max_size=1G;
    proxy_temp_path /tmpfs;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '$upstream_addr $upstream_response_time $request_time $upstream_cache_status';

    access_log  logs/access.log  main;
    server {
        listen       80;
        server_name  localhost;

        location / {
                proxy_pass http://127.0.0.1:9095;
                proxy_cache local_cache;
                proxy_cache_valid 200 2s;
                proxy_cache_lock on;
        }

    }
}

字符串

icomxhvb

icomxhvb1#

这似乎是默认的行为。该高速缓存锁定的请求被最坏情况下的500 ms或time_out值锁定。(来源)
如果上游响应时间稳定,对于cache_lock_timeout,任何稍高于响应时间的值,都将避免这种行为。在这种特定情况下,我们可以设置大约5 ms,第一个请求将在0-1 ms内返回,锁定的请求将在5- 6 ms内返回。

yh2wf1be

yh2wf1be2#

现在是2023年,仍然无法使用参数更改默认值。默认值500 ms可以在ngx_http_file_cache.c#L455和ngx_http_file_cache.c#L515中找到。
您可以设置所需的值并自行编译nginx。自定义缓存锁轮询延迟的nginx编译示例-Dockerfile

相关问题