[英]How to set the timestamp to a header in request and response in nginx?
在我的用例中,我想在 nginx 的请求和响应中设置当前时间戳。 我想计算上游服务器所花费的时间。 因此,当请求到达 nginx 服务器时,我想将当前时间戳设置为标头,然后当响应来自上游时,我想将另一个标头设置为当时的时间戳。 我将确保上游服务器将请求时间戳添加到响应中。
这是我的 nginx 配置,
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
keepalive_timeout 65;
client_max_body_size 500M;
#timeouts
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 1m;
send_timeout 600;
upstream backend {
server abcd.com:443;
keepalive 64;
}
#
# The default server
#
server {
listen 80;
server_name http_server;
error_log /var/log/nginx/error.log debug;
location /products {
rewrite ^/products/(.*) /$1/$2 break;
proxy_pass https://backend;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
}
# redirect not found pages to the static page /404.html
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
有人可以帮我吗?
使用对机器和人类都友好的$time_iso8601
变量。
不是真正回答问题的答案,但可以增加一些价值。
这里列出了几个可用的变量http://nginx.org/en/docs/varindex.html
其中很大一部分与上游服务器相关,例如$upstream_connect_time
、 $upstream_first_byte_time
、 $upstream_header_time
等。 这些会给你更多的洞察力,就像你只计算“in”和“out”之间的差异一样。 然而,我不清楚哪个$upstream_*
变量或变量组会让你获得与上游服务器交互的总时间,也许其他人可以发布该信息。
您可以创建自定义日志格式,将这些变量的值添加到日志文件中,或者可能将它们添加到响应标头中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.