要统计反向代理中请求进入 Nginx 和请求离开 Nginx 的时间差,可以使用 Nginx 的日志模块和时间模块。具体步骤如下:

  1. 在 Nginx 配置文件中开启日志模块,并设置日志格式,包括请求的时间和响应的时间:
http {
    log_format custom '$remote_addr - $remote_user [$time_local] ' \
                      ''$request' $status $body_bytes_sent ' \
                      ''$http_referer' '$http_user_agent' ' \
                      '$request_time $upstream_response_time';
    access_log /var/log/nginx/access.log custom;
}

其中,$request_time 表示请求的时间,$upstream_response_time 表示响应的时间。

  1. 重启 Nginx 使配置生效。

  2. 使用日志分析工具(如 awk、grep、sed 等)提取出 $request_time$upstream_response_time,并计算它们的差值即可得到请求进入 Nginx 和请求离开 Nginx 的时间差。

例如,可以使用 awk 命令提取出请求时间和响应时间,并计算它们的差值:

awk '{print $10 - $11}' /var/log/nginx/access.log

这将输出所有请求的时间差,单位为秒。您可以使用其他命令对此进行过滤和分析,以获得更详细的数据。

Nginx 反向代理请求时间差统计方法

原文地址: https://www.cveoy.top/t/topic/nwos 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录