在nginx中,$connection_time是什么意思?

3
1个回答

2
$connection_time是http请求使用的TCP连接的存活时间。 HTTP keep-alive和HTTP/2允许单个TCP连接发送和接收多个HTTP请求/响应,因此$connection_time应始终大于$request_time
我们可以使用以下nginx配置文件来证明它。
http {
  ...
  log_format testlog '[$time_local] "$request" connection_time= $connection_time request_time= $request_time';
}


server {
    listen       80;
    server_name  localhost;

    root   /www;

    location / {
        index  index.html index.htm;
    }

    access_log /var/log/nginx/test.access.log testlog;
    error_log /var/log/nginx/test.error.log;

    # disable cache
    expires -1;
    add_header Last-Modified $date_gmt;
    add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
    if_modified_since off;
    etag off;

    # disable keep alive
    location /keep-alive-off/ {
        keepalive_timeout 0;
    }

    # enable keep alive
    location /keep-alive-on/ {
        keepalive_timeout 300s;
    }

}

access.log

[12/Oct/2021:06:38:03 +0000] "GET /keep-alive-off/test.pdf HTTP/1.1" connection_time= 0.212 request_time= 0.210
[12/Oct/2021:06:38:05 +0000] "GET /keep-alive-off/test.pdf HTTP/1.1" connection_time= 0.252 request_time= 0.253
[12/Oct/2021:06:38:06 +0000] "GET /keep-alive-off/test.pdf HTTP/1.1" connection_time= 0.200 request_time= 0.203
[12/Oct/2021:06:38:11 +0000] "GET /keep-alive-on/test.pdf HTTP/1.1" connection_time= 0.240 request_time= 0.239
[12/Oct/2021:06:38:14 +0000] "GET /keep-alive-on/test.pdf HTTP/1.1" connection_time= 3.268 request_time= 0.185
[12/Oct/2021:06:38:17 +0000] "GET /keep-alive-on/test.pdf HTTP/1.1" connection_time= 6.177 request_time= 0.168

我们可以看到,如果关闭HTTP keep-alive以禁止TCP连接复用,则$request_time将始终等于$connection_time。

哇,谢谢!我明白了。我还需要努力学习语法。 - Guan
s/grammer/grammar/g - Guan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接