shell——nginx日志分析

需安装nginx
yum install nginx
man nginx可查看一些信息,如下为截取部分

FILES
     /run/nginx.pid
             Contains the process ID of nginx.  The contents of this file are not sensitive, so it can be world-readable.

     /etc/nginx/nginx.conf
             The main configuration file.

     /var/log/nginx/error.log
             Error log file.

Nginx日志分析

nginx日志格式

日志格式:常用日志格式

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

!!!取列的时候不能看这个,要看日志,因为是按空格分隔的
日志:此日志格式为,ip不仅记录代理的IP还记录远程客户端真实IP。

log_format main1 '$proxy_add_x_forwarded_for - $remote_user [$time_local]' 
				'"$request" $status $body_bytes_sent' '"$http_referer" "$http_user_agent"';

参数:

$remote_addr:远程IP; $1
- $2
$remote_user:远程用户;$3
$time_local:时间;$4(年月日时分秒) $5(待探索)
$request:用来记录请求的url与http协议;$6 (请求方式GET/POST) $7(请求的文件位置)$8(协议版本(http))
$status:用来记录请求状态;成功是200;$9
$body_bytes_sent:记录发送给客户端文件主体内容大小;$10
$http_referer:用来记录从那个页面链接访问过来的;$11
$http_user_agent:记录客户浏览器的相关信息;$12$13$14$15$16$17$18$19$20$21$22待验证
$http_x_forwarded_for:访问用户的真实 IP 地址;$23$24待验证

统计2019年9月5日的PV量(Page View点击量)
其中wc -l的作用是统计输入的行数

grep '05/Sep/2017' 日志文件 |wc -l

统计2019年9月5日访问大于100次的IP

grep '05/Sep/2017' 日志文件 |awk '{ ips[$1]++ } END{ for(i in ips){ if(ips[i]>100) {print i,ips[i]} } } '

统计2019年9月5日访问最多的10个页面
head -n10等于head 10 就是输出前10行

grep '05/Sep/2017' 日志文件 |awk '{ urls[$7]++ } END{ for(i in urls){{print i,urls[i]}}} ' |sort -kl -rn |head -n10

统计2019年9月5日 每个IP访问状态码的数量
数组的索引用IP和状态码的结合即可:
ip_code[$1" "$9]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值