Nginx 响应时间序列分析

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_addrClient IP10.1.118.113​
2$hostnameNginx 服务名
3$host域名http://api.firfor.cn
4$time_local用具记录访问时间和时区,日期格式:18/Feb/2017:14:10:17 +080002/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_statuskeeps 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 server0.032​官方文档
12$request_length请求的长度(包括请求的地址,http请求头和请求主体)request length (including request line, header, and request body) (1.3.12, 1.2.7)132734​
13$bytes_sentnumber of bytes sent to a client - 传输给客户端的全部字节数,包含响应头等信息201​
14$http_refererurl跳转来源,用来记录从哪个页面链接访问过来的,例如:https://proxy.mimvp.com-
15$http_user_agent用户所使用的代理(一般为浏览器)"Apache-HttpClient/4.5.12 (Java/1.8.0_291)"​
16$upstream_connect_timeThe time spent on establishing a connection with an upstream server0.002​
17$msec1) time in seconds with a milliseconds resolution at the time of the log write​2) current time in seconds with the milliseconds resolution (1.3.9, 1.2.6)1675324865.381​
18$proxy_hostBy 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_receivedEmbedded 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_lengthEmbedded 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_timeEmbedded 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 server0.032​官方文档upstream-header_time

以下是时间模型:

Nginx 响应时序

总结

  • $request_time 包含nginx处理的所有时间,
    • 并不包含clientnginx的连接时间. 这部分时间在弱网环境下时间也不可忽略.
  • $upstream_response_time 是Nginxupstream的所有处理时间
    • 包括: upstream的接收数据和处理请求以及返回response数据 所有时间.
    • $upstream_connect_time是nginx到upstream的连接时间. 这个一般在内网环境时会非常短.
  • $upstream_header_time 是从建立连接到Nginx接收到upstream的第一个返回字节的时间.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值