由于公司的业务比较特殊,对速度比较在意,客户最近反应我们的平台时间比较久,处理一个请求十秒左右才返回,领导要求找出原因,我想让nginx日志记录请求处理用了多长时间,后端处理用了多长时间,总共用了多长时间,哪里出现的瓶颈好进行针对性解决

配置nginx统计请求和后端服务Tomcat服务响应时间

编辑nginx的配置文件nginx.conf

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_response_time" "$request_time"';

  

上面的日志是我找的默认形式的,在你原先的日志基础就加入两点即可

$request_time

官网描述:request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client 。

说明:就是指从接受用户的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出响应时间等

$upstream_response_time

官网描述:keeps times of responses obtained from upstream servers; times are kept in seconds with a milliseconds resolution. Several response times are separated by commas and colons like addresses in the $upstream_addr variable

说明:是指从nginx向后端建立连接开始到后端处理完数据然后关闭连接为止的时间

从上面的描述可以看出$request_time可能会比$upstream_response_time值大一点,特别是使用POST方式传递参数时,因为nginx会把request body缓存住,接受完毕后才会把数据一起发送给后端。所以如果用户网络较差,或者传递数据较大时,$request_time会比$upstream_response_time的值大很多

Tomcat:

Tomcat则是通过改server.xml的日志格式%D %T统计请求响应时间

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b %D %F" />

%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,提交响应的时间,以毫秒为单位

详细说明:http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Logging

使用awk文本处理命令辅助统计access.log

统计nginx访问日志access.log的每分钟请求数

awk -F: '{count[$2":"$3]++} END {for (minute in count) print minute, count[minute]}' ./access.log | sort > count.log
[root@60 logs]# cat count.log
01:18 102
01:38 77
02:10 53
02:20 89
02:28 66
02:43 123
02:44 44
02:48 53
03:12 26
04:07 90
04:09 21
············

  

统计请求响应时间超过10秒的记录

awk '($NF > 10){print $0}' ./access.log > t10_20180525.log

  

推荐参考:

https://www.ibm.com/developerworks/cn/linux/l-cn-awk-httplog/

配置nginx,Tomcat日志记录请求耗时的更多相关文章

  1. 配置分割Tomcat日志

    安装cronolog 官网下载:http://cronolog.org/usage.html ./configuremake;make install 或者 yum install cronolog ...

  2. Centos6.7配置Nginx+Tomcat简单整合

    系统环境:Centos 6.7 软件环境:JDK-1.8.0_65.Nginx-1.10.3.Tomcat-8.5.8 文档环境:/opt/app/ 存放软件目录,至于mkdir创建文件就不用再说了 ...

  3. 通过Nginx,Tomcat访问日志(access log)记录请求耗时

    一.Nginx通过$upstream_response_time $request_time统计请求和后台服务响应时间 nginx.conf使用配置方式: log_format main '$remo ...

  4. Tomcat配置自定义访问日志 --- 获取请求头部信息

    使用tomcat,搭建完个人网站后,默认记录来访游客的信息是十分有限的,主要有ip和路径以及方法等. 有时候为了获取更多来访信息,比如请求的头部信息,这个时候就需要我们手动配置log了. 开始 进入T ...

  5. 配置nginx+tomcat支持websocket

    问题情景:    最近开发新增加一个项目,需要支持https wss协议 访问https://test.aa.com  使用nginx反向代理到后端tomcat web应用 访问https://tes ...

  6. Apache/Nginx/IIS日志记录的各个字段内容与含义

    一.Apache 1.1 Apache日志文件名称及路径介绍 当我们安装并启动Apache后,Apache会自动生成两个日志文件,这两个日志文件分别是访问日志access_log(在Windows上是 ...

  7. 注意这几点,轻轻松松配置 Nginx + Tomcat 的集群和负载均衡

    Tomcat 集群是当单台服务器达到性能瓶颈,通过横向扩展的方式提高整体系统性能的有效手段.Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,可以通过简单的配置实现 Tomcat 集群 ...

  8. CentOS6.5 简单配置Nginx + tomcat

    1.配置nginx.conf vi /usr/local/nginx/conf/nginx.conf --这是你的安装目录 注:红框地方为任意位置,server在配置文件中已存在 我使用的是两个tom ...

  9. ELK收集Nginx|Tomcat日志

    1.Nginx 日志收集,先安装Nginx cd /usr/local/logstash/config/etc/,创建如下配置文件,代码如下 Nginx.conf input { file { typ ...

随机推荐

  1. php 使用curl 进行简单模拟提交表单

    //初始化curl $ch = curl_init(); $url = 'xxx'; $option = [ CURLOPT_URL => $url, CURLOPT_HEADER => ...

  2. 一起talk C栗子吧(第二十五回:C语言实例--二分查找)

    各位看官们,大家好,上一回中咱们说的是顺序查找的样例,这一回咱们说的样例是:二分查找.闲话休 提,言归正转. 让我们一起talk C栗子吧. 看官们,我们在上一回中说了查找的相关内容,而且介绍了一种查 ...

  3. 清空select下拉框的方法

    $("#search").find("option").remove(); //或者 $("#search").empty();

  4. iOS-去除NavigationBar边线

    解决办法: self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;

  5. IntelliJ中的main函数和System.out.println()快捷输入方式

    转自:https://blog.csdn.net/assassinsshadow/article/details/73557375 main快捷输入 psvm System.out.println() ...

  6. JRE not compatible with workspace .class file compatibility: 1.7

    在进行Eclipse开发的时候,经常会遇到一些小问题,现在开始每天积累一些小问题的解决方法.出现:JRE not compatible with workspace .class file compa ...

  7. 微信商城 Common Log Format Apache CustomLog

    w 0- /Apr/::: +] "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, ...

  8. 把配置和环境解耦 eliminate “works on my machine” problems when collaborating on code with co-workers docker架构与解决的问题

    Docker实践 - 懒人的技术笔记 - 博客频道 - CSDN.NET  http://blog.csdn.net/lincyang/article/details/43055061 Docker直 ...

  9. 剑指Offer——替换空格

    题目描述: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 分析: 如果从前往后替换空格,那 ...

  10. Scrapy框架-scrapy框架架构详解

    1.Scrapy框架介绍 写一个爬虫,需要做很多的事情.比如:发送网络请求.数据解析.数据存储.反反爬虫机制(更换ip代理.设置请求头等).异步请求等.这些工作如果每次都要自己从零开始写的话,比较浪费 ...