Home > OS >  What does $connection_time mean in nginx?
What does $connection_time mean in nginx?

Time:10-16

I need some time data for optimizing nginx. But I can not find the meaning of $connection_time variable.

Offical document explains(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)

In the access log, I only know $connection_time is always greater than $request_time.

CodePudding user response:

$connection_time is alive time of TCP connection used by a http request.

HTTP keep-alive and HTTP/2 allows a single TCP connection to send and receive multiple HTTP requests/responses. so the $connection_time should be always greater than $request_time;

We can prove it using the following nginx configure file

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

We can see that if turn off HTTP keep-alive to disallow TCP connection reuse, $request_time would always be equal with $connection_time

  • Related