大网数据广告活动
服务器运维问答

Why is $upstream_response_time in add_header a hyphen?

· 52CE 我爱测

Given /etc/nginx/nginx.conf : events {} http { server { listen 8000; location / { proxy_pass http://localhost:8001; add_header X-Trip-Time $request_time; add_header X-Process-Time $upstream_response_time; } } } Running: python3 -…

问题原文

原帖链接 · Mario Ishac · CC BY-SA 4.0

Given /etc/nginx/nginx.conf:

events {}

http {
    server {
        listen 8000;

        location / {
            proxy_pass  http://localhost:8001;

            add_header X-Trip-Time    $request_time;
            add_header X-Process-Time $upstream_response_time;
        }
    }
}

Running:

python3 -u -m http.server 8001 > app.log 2>&1 & sudo nginx

followed by:

curl -i localhost:8000

shows these two headers:

X-Trip-Time: 0.001
X-Process-Time: -

I've seen this related question, which says this:

In case of internal error while connecting to an upstream or when a reply is taken from the cache, the variable is set to -

But my upstream server is successfully connected to as shown by cat app.log:

127.0.0.1 - - [03/Feb/2021 19:41:44] "GET / HTTP/1.0" 200 -

What is the cause for $upstream_response_time equaling -, yet $request_time works as expected?

已采纳解答

原帖链接 · Mario Ishac · CC BY-SA 4.0

I received this answer on the nginx mailing list:

That's because response headers are sent before the $upstream_response_time is known: it is only known when the response is fully received from the upstream server, including the response body, and this happens after the response headers are sent to the client. If you want to return something in the response headers, consider the $upstream_header_time variable instead (http://nginx.org/r/$upstream_header_time).

I couldn't find anything in the documentation about how nginx buffers responses (can only find documentation for requests) and whether it can be configured, but sure enough changing $upstream_response_time to $upstream_header_time is working and the times are very close when looking in the logs, atleast when the response body size is miniscule such as in my case.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-02-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。