Nginx 响应时间序列分析
摘要
本文主要通过Nginx日志分析和还原整个Nginx的HTTP请求的响应时序. 为分析服务性能问题时,提供分析依据.
背景
在问题: springboot应用,controller方法执行时间和接口响应时间相差巨大,可能是什么原因?中有说过关于服务响应时间序列的问题. 在服务出现性能问题时,需要分析出花费的时间都用到了哪. 那其中最主要的一环就是Niginx反向代理服务器的时间分析.
Nginx服务日志
nginx的日志配置相关可以参考这篇英文文章: NGINX Access Logs and Error Logs. 本文主要来厘清下关于响应时间相关的一些配置与时间之间的对应关系.
以下是一个真实的配置项:
log_format main '$remote_addr\t$hostname\t$host\t$time_local\t'
'$scheme\t$request\t'
'"$status"\t$upstream_status\t'
'$request_time\t$upstream_addr\t$upstream_response_time\t'
'$request_length\t$bytes_sent\t'
'$http_referer\t"$http_user_agent"\t'
'$upstream_connect_time\t$msec\t'
'$proxy_host\t'
'$upstream_bytes_received\t$upstream_response_length\t$upstream_header_time';
我们一一看下每一项的值
# | 配置项 | 相关解释 | 参考日志 | 相关链接 |
---|---|---|---|---|
1 | $remote_addr | Client IP | 10.1.118.113 | |
2 | $hostname | Nginx 服务名 | ||
3 | $host | 域名 | http://api.firfor.cn | |
4 | $time_local | 用具记录访问时间和时区,日期格式:18/Feb/2017:14:10:17 +0800 | 02/Feb/2023:16:01:05 +0800 | |
5 | $scheme | 协议类型: http/https/ftp等 | http | |
6 | $request | 用于记录请求的url以及请求方法,格式:"GET /free.php?proxy=in_tp HTTP/1.1" | POST /fresh-web/api/user | |
7 | $status | 记录HTTP状态码 | “200” | |
8 | $upstream_status | keeps status code of the response obtained from the upstream server.Status codes of several responses are separated by commas and colonslike addresses in the $upstream_addr variable.If a server cannot be selected, the variable keeps the 502 (Bad Gateway) status code. | 200 | 官方文档 |
9 | $request_time | 整个请求的总时间,从接收用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间,程序响应时间,输出响应数据时间 | 6.280 | |
10 | $upstream_addr | 后台提供服务的地址(即转发处理的目标地址) | 10.14.1.77:80 | |
11 | $upstream_response_time | 从nginx向后端建立连接开始到接受完数据然后关闭连接为止的时间The time between establishing a connection andreceiving the last byte of the response body from the upstream server | 0.032 | 官方文档 |
12 | $request_length | 请求的长度(包括请求的地址,http请求头和请求主体)request length (including request line, header, and request body) (1.3.12, 1.2.7) | 132734 | |
13 | $bytes_sent | number of bytes sent to a client - 传输给客户端的全部字节数,包含响应头等信息 | 201 | |
14 | $http_referer | url跳转来源,用来记录从哪个页面链接访问过来的,例如:https://proxy.mimvp.com | - | |
15 | $http_user_agent | 用户所使用的代理(一般为浏览器) | "Apache-HttpClient/4.5.12 (Java/1.8.0_291)" | |
16 | $upstream_connect_time | The time spent on establishing a connection with an upstream server | 0.002 | |
17 | $msec | 1) time in seconds with a milliseconds resolution at the time of the log write2) current time in seconds with the milliseconds resolution (1.3.9, 1.2.6) | 1675324865.381 | |
18 | $proxy_host | By default, NGINX redefines two header fields in proxied requests,“Host” and “Connection”, and eliminates the header fields whose values are empty strings.“Host” is set to the $proxy_host variable, and “Connection” is set to close. | online-venv-ingress | |
19 | $upstream_bytes_received | Embedded Variables: number of bytes received from an upstream server (1.11.4).Values from several connections are separated by commas and colons like addresses in the $upstream_addr variable. | 157 | 官方文档 |
20 | $upstream_response_length | Embedded Variables: keeps the length of the response obtained from the upstream server (0.7.27); the length is kept in bytes.Lengths of several responses are separated by commas and colons like addresses in the $upstream_addr variable. | 64 | 官方文档 |
21 | $upstream_header_time | Embedded Variables: ( 从建立连接成功到接收第一个字节之间的时间 )keeps time spent on receiving the response header from the upstream server (1.7.10);the time is kept in seconds with millisecond resolution. Times of several responses are separated by commas and colons like addresses in the $upstream_addr variable.—The time between establishing a connection and receiving the first byte of the response header from the upstream server | 0.032 | 官方文档upstream-header_time |
以下是时间模型:
Nginx 响应时序
总结
$request_time
包含nginx处理的所有时间,- 并不包含
client
到nginx
的连接时间. 这部分时间在弱网环境下时间也不可忽略.
- 并不包含
$upstream_response_time
是Nginx
的upstream
的所有处理时间- 包括:
upstream
的接收数据和处理请求以及返回response
数据 所有时间. $upstream_connect_time
是nginx到upstream
的连接时间. 这个一般在内网环境时会非常短.
- 包括:
$upstream_header_time
是从建立连接到Nginx
接收到upstream
的第一个返回字节的时间.