nginx $upstream_response_time 具体什么时候启动/停止

epr*_*hro 8 performance nginx query-performance

有谁知道具体的时钟何时$upstream_response_time开始和结束?

该文档似乎有点含糊:

记录从上游服务器接收响应所花费的时间;时间以秒为单位,精度为毫秒。多个响应的时间由逗号和冒号分隔,就像 $upstream_addr 变量中的地址一样。

还有一个$upstream_header_time值,它增加了更多的混乱。

  1. 我假设$upstream_connect_time连接建立后但在上游接受之前停止?

  2. 这之后包括什么$upstream_response_time

    • 等待上游接受所花费的时间?
    • 发送请求所花费的时间?
    • 发送响应头花费的时间?

Arm*_*ani 10

更具体的定义在他们的博客中中。

\n
\n

$请求时间 \xe2\x80\x93 完整请求时间,从 NGINX 从客户端读取第一个字节开始,到 NGINX 发送响应正文的最后一个字节为止

\n

$upstream_connect_time \xe2\x80\x93 与上游服务器\n建立连接所花费的时间

\n

$upstream_header_time \xe2\x80\x93 建立与上游服务器的连接与接收\n响应标头的第一个字节之间的时间\n

\n

$upstream_response_time \xe2\x80\x93 建立与上游服务器的连接和接收\n响应正文的最后一个字节之间的时间

\n
\n

所以

\n
    \n
  • $upstream_header_time包含在$upstream_response_time.
  • \n
  • 连接到上游所花费的时间不包括在两者中。
  • \n
  • 向客户端发送响应所花费的时间不包括在两者中。
  • \n
\n


小智 10

我调查并调试了与此相关的行为,结果如下:

开始时间 时间结束
$upstream_connect_time Nginx 与上游服务器建立 TCP 连接之前 在 Nginx 向上游服务器发送 HTTP 请求之前
$upstream_header_time Nginx 与上游服务器建立 TCP 连接之前 Nginx 接收并处理来自上游服务器的 HTTP 响应中的标头后
$upstream_response_time Nginx 与上游服务器建立 TCP 连接之前 Nginx 接收并处理来自上游服务器的 HTTP 响应后

源代码

我将解释 $upstream_connect_time 和 $upstream_response_time 之间的值有何不同,因为这是我主要感兴趣的。

的值u->state->connect_time表示 $upstream_connect_time (以毫秒为单位),在以下部分中提取:https://github.com/nginx/nginx/blob/3334585539168947650a37d74dd32973ab451d70/src/http/ngx_http_upstream.c#L2073

    if (u->state->connect_time == (ngx_msec_t) -1) {
        u->state->connect_time = ngx_current_msec - u->start_time;
    }
Run Code Online (Sandbox Code Playgroud)

而 的值u->state->response_time(表示以毫秒为单位的 $upstream_response_time)在以下部分中设置:https://github.com/nginx/nginx/blob/3334585539168947650a37d74dd32973ab451d70/src/http/ngx_http_upstream.c#L4432

    if (u->state && u->state->response_time == (ngx_msec_t) -1) {
        u->state->response_time = ngx_current_msec - u->start_time;
Run Code Online (Sandbox Code Playgroud)

您可以注意到,这两个值都是基于 计算的u->start_time,这是建立连接之前的时间,定义在https://github.com/nginx/nginx/blob/3334585539168947650a37d74dd32973ab451d70/src/http/ngx_http_upstream.c#L1533 (注意,这ngx_event_connect_peer是一个在 nginx 工作进程和上游服务器之间建立 TCP 连接的函数)。

因此,这两个值都包括建立 TCP 连接所花费的时间。您可以通过使用 gdbserver 等进行实时调试来检查这一点。