在运行服务的时候,我们经常会遇见服务很卡,但是我们不知道是服务配置问题,服务器问题还是网络的问题,这篇文章对tomcat搭建的和nginx映射转发的服务进行排查,对配置文件进行设置,使得日志反馈出响应时间,以便排查问题。
tomcat
在tomcat的server.xml里找到如下配置,取消注释并加上红框处配置
- %a - Remote IP address --远程IP地址
- %A - Local IP address --本地IP地址
- %b - Bytes sent, excluding HTTP headers, or '-' if zero --发送的字节数(Bytes sent), 不包括HTTP headers的字节,如果为0则展示'-'
- %B - Bytes sent, excluding HTTP headers --发送的字节数(Bytes sent), 不包括HTTP headers的字节
- %h - Remote host name (or IP address if
enableLookups
for the connector is false) --远程主机名称(如果resolveHosts为false则展示IP) - %H - Request protocol --请求协议
- %l - Remote logical username from identd (always returns '-') --远程用户名,始终为'-'(Remote logical username from identd)
- %m - Request method (GET, POST, etc.) --请求的方法(GET, POST等)
- %p - Local port on which this request was received. See also
%{xxx}p
below. --接受请求的本地端口 - %q - Query string (prepended with a '?' if it exists) --查询字符串,如果存在,有一个前置的'?'
- %r - First line of the request (method and request URI) --请求的第一行(包括请求方法和请求的URI)
- %s - HTTP status code of the response --response的HTTP状态码(200,404等)
- %S - User session ID --用户的session ID
- %t - Date and time, in Common Log Format --日期和时间,Common Log Format格式
- %u - Remote user that was authenticated (if any), else '-' --被认证的远程用户, 不存在则展示'-'
- %U - Requested URL path --请求URL路径
- %v - Local server name --本地服务名
- %D - Time taken to process the request, in millis --处理请求的时间,单位为毫秒
- %T - Time taken to process the request, in seconds --处理请求的时间,单位为秒
- %F - Time taken to commit the response, in millis --提交响应的时间,以毫秒为单位
- %I - Current request thread name (can compare later with stacktraces) --当前请求的线程名
此处参考大佬文章Tomcat AccessLog 格式化 - Jager - 博客园 (cnblogs.com)
大家可以根据需求设置
然后重启服务
然后你在日志里就会发现access_log的日志
日志里的最后两个数字就是你设置的处理响应时间和响应时间,单位是毫秒
nginx
nginx先打开nginx.conf
找到红色框的代码位置,取消注释,加上蓝框的配置,重启nginx
找到日志位置,找到access日志
进去之后可以看到红框位置分别对应的请求时间和响应时间,这里时间单位是秒
nginx排查完毕
如果这两个数据都没有明显异常,就可以看看
1.是不是服务器配置问题,内存不足,带宽不够等。
2.服务器映射的时候,应用服务器和nginx转发的服务器距离太远,比如应用服务器在广州,nginx服务器在北京,甚至在国外,那有延迟也是正常的。
3.是不是用域名访问的服务,域名解析慢也有可能。