频道栏目
首页 > 资讯 > 其他 > 正文

nginx的日志设置解析

17-12-11        来源:[db:作者]  
收藏   我要投稿
nginx日志设置

安装完nginx后,配置文件nginx.conf里的http模块下,日志格式默认是注释掉的。

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_log  logs/access.log  main;

在上面log_format指令的下面一行添加

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

然后在vhost/lee.conf下添加

access_log   logs/lee.access.log main;

重新加载nginx

/usr/local/nginx/sbin/nginx -s reload 

浏览器访问首页后,可以在logs/lee.access.log下看到访问日志

192.168.2.100 - - [29/Nov/2017:21:34:29 +0800] "GET /misc.php?mod=patch&action=ipnotice&_r=0.6885789419146087&inajax=1&ajaxtarget=ip_notice HTTP/1.1" 200 65 "http://192.168.2.118/home.php?mod=space&uid=1&do=profile" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"  0.037
参数含义
符号-                 代表空
log_format          定义日志格式
main                定义调用的日志名称
$remote_addr       访问的客户端ip地址
$remote_user       访问的客户端用户名称
$time_local        访问的时间,本地客户端的时间
$request           http协议内容和用户请求的URL,注意双引号
$status            返回http请求的状态
$body_bytes_sent   发送给客户端的字节数,不包括响应头,单位是字节
$http_referer      记录从哪个链接页面访问过来的,一般http://开头,注意双引号
$http_user_agent   客户端浏览器的相关信息,包括版本,浏览器的爬虫访问等
$request_time      请求处理时间,与dns/对方收不收到无关等,仅仅是本身处理的时间,单位秒,精度毫秒,越小越好,有时候显示0,瞬间处理,是因为客户端访问的时候本身读取的是浏览器缓存,比如图片,缓存状态码是304
$server_name       虚拟主机名称
$http_x_forwarded_for  请求方的真实ip地址
$upstream_status   upstream的状态
$ssl_protocol      ssl协议版本
$bytes_sent        发送给客户端的总字节数
$connection_requests       当前一个连接获得的请求数量
$request_length    请求的长度,包含了请求行/请求头/请求内容
$msec              记录日志写入时间,需要消耗i/o
$upstream_response_time  后端的接受与处理,程序的响应时间,服务从后端建立连接开始到接收数据然后关闭连接之间的总时间

有时候$http_referer内容会显示空,也就是符号-,就是无从查到从哪里来访问本页面的链接,有可能是直接在服务器里使用curl -s 网址,从而直接访问页面内容。

把上面logs/lee.access.log下的日志内容分割来看

1.192.168.2.100 访问的客户端ip \
2.- remote_user为空 \
3.[29/Nov/2017:21:34:29 +0800] 访问的时间 \
4.”GET /misc.php?mod=patch&action=ipnotice&_r=0.6885789419146087&inajax=1&ajaxtarget=ip_notice HTTP/1.1” 访问的方式,访问的页面url,访问的http协议 \
5.200 返回的状态 \
6.65 发送给客户端的字节数 \
7.”http://192.168.2.118/home.php?mod=space&uid=1&do=profile” 从哪个页面访问过来的 \
8.”Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36” 客户端方面的信息,浏览器是谷歌,win系统等 \
9.0.217 请求处理时间,一般越小越好 \

常用日志信息查询

查看不同ip的数量

cat lee.access.log | awk {'print $1'} | sort | uniq | wc –l
awk {'print $1'} lee.access.log  | uniq | wc -l

查看访问量前20的ip

awk {'print $1'} lee.access.log  | sort | uniq -c | sort -nr | head -20

查看pv总量

awk '{print $7}' lee.access.log | wc -l

查看404等错误页面出现次数大于20

awk '{if ($9~/502|499|500|503|404/) print $1,$9}' lee.access.log  | sort |uniq -c | sort -r | awk '$1 > 1 {print $2}'

查看访问量前2名的页面

awk '{print $7}' lee.access.log |sort -n | uniq -c | sort -nr | head -2

查看处理时间大于2秒的页面,显示处理时间/url/访客ip

awk '$NF > 0.1 {print $NF,$7,$1}' lee.access.log

查看某个时间段的访问量

awk '/29\/Nov\/2017:21:50:21/,/29\/Nov\/2017:22:02:07/ {print $0}' lee.access.log  | wc –l
sed -n '/29\/Nov\/2017:21:50:21/,/29\/Nov\/2017:22:02:07/p' lee.access.log  | wc -l

备注:
1) nginx代理到后端的apache,此时apache的访问日志中访问ip为nginx服的ip而不是真实访问ip。
在ngingx.conf配置文件中的location下添加

proxy_set_header  X-Real-IP  $remote_addr;

在http.conf配置文件中,把

LogFormat "%h %l %u %t \"%r\" %>s %b" common

修改为

LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b" common

也就是把%h替换为%{X-Real-IP}i。
重新加载nginx和apache,再次访问,就可以看到真实的访问ip。
这里写图片描述

相关TAG标签
上一篇:在redhat5.8x86_64位系统下安装asm+grid+oracle11gR2的教程
下一篇:BIND的配置及安装详情
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站