$connection_time 在 nginx 中是什么意思?
What does $connection_time mean in nginx?
我需要一些时间数据来优化nginx。但是我找不到$connection_time
变量的含义。
官方文档说明(http://nginx.org/en/docs/http/ngx_http_core_module.html#var_connection_time):
connection_time
connection time in seconds with a milliseconds resolution (1.19.10)
在访问日志中,我只知道$connection_time
总是大于$request_time
。
$connection_time
是 http 请求使用的 TCP 连接的存活时间。
HTTP keep-alive 和 HTTP/2 允许单个 TCP 连接发送和接收多个 HTTP requests/responses。所以 $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
我需要一些时间数据来优化nginx。但是我找不到$connection_time
变量的含义。
官方文档说明(http://nginx.org/en/docs/http/ngx_http_core_module.html#var_connection_time):
connection_time
connection time in seconds with a milliseconds resolution (1.19.10)
在访问日志中,我只知道$connection_time
总是大于$request_time
。
$connection_time
是 http 请求使用的 TCP 连接的存活时间。
HTTP keep-alive 和 HTTP/2 允许单个 TCP 连接发送和接收多个 HTTP requests/responses。所以 $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