nginx日志分析
配置nginx access_log,高并发可以适当增大buffer
在Http 模块定义一个日志格式
#注意:此指令只支持http块,不支持server块
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,' #总的处理时间
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",' #后端应用服务器处理时间
'"http_host":"$host",'
'"uri":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
在server 级别就可以使用前面定义的log 格式
access_log logs/request.access.log access_json buffer=1024k;
#默认main 日志格式
$remote_addr #客户端的ip地址(代理服务器,显示代理服务ip)
$remote_user #用于记录远程客户端的用户名称(一般为“-”)
$time_local #用于记录访问时间和时区
$request #用于记录请求的url以及请求方法
$status #响应状态码,例如:200成功、404页面找不到等。
$body_bytes_sent #给客户端发送的文件主体内容字节数
$http_user_agent #用户所使用的代理(一般为浏览器)
$http_x_forwarded_for #可以记录客户端IP,通过代理服务器来记录客户端的ip地址
$http_referer #可以记录用户是从哪个链接访问过来的
统计IP访问量
awk '{print $1}' access.log | sort -n | uniq | wc -l
查看某一时间段的IP访问量(4-5点)
grep "07/Apr/2017:0[4-5]" access.log | awk '{print $1}' | sort | uniq -c| sort -nr | wc -l
#查看2022年6月30 14-15点 访问最多的ip,可以根据需要调整时间 grep "30/Jun/2022:14:1[4-5]" ,14:14-15 分钟
grep "30/Jun/2022:1[4-5]" saastest.meifu123.com.log |awk '{print $1}' | sort -n |uniq -c | sort -rn | head -n 100
查看访问最频繁的前100个IP
awk '{print $1}' access.log | sort -n |uniq -c | sort -rn | head -n 100
查看访问100次以上的IP
awk '{print $1}' access.log | sort -n |uniq -c |awk '{if($1 >100) print $0}'|sort -rn
查询某个IP的详细访问情况,按访问频率排序
grep '104.217.108.66' access.log |awk '{print $7}'|sort |uniq -c |sort -rn |head -n 100
页面访问统计
查看访问最频的页面(TOP100)
awk '{print $7}' access.log | sort |uniq -c | sort -rn | head -n 100
查看访问最频的页面([排除php页面】(TOP100)
grep -v ".php" access.log | awk '{print $7}' | sort |uniq -c | sort -rn | head -n 100
查看页面访问次数超过100次的页面
cat access.log | cut -d ' ' -f 7 | sort |uniq -c | awk '{if ($1 > 100) print $0}' | less
查看最近1000条记录,访问量最高的页面
tail -1000 access.log |awk '{print $7}'|sort|uniq -c|sort -nr|less
每秒请求量统计
统计每秒的请求数,top100的时间点(精确到秒)
awk '{print $4}' access.log |cut -c 14-21|sort|uniq -c|sort -nr|head -n 100
每分钟请求量统计
统计每分钟的请求数,top100的时间点(精确到分钟)
awk '{print $4}' access.log |cut -c 14-18|sort|uniq -c|sort -nr|head -n 100
每小时请求量统计
统计每小时的请求数,top100的时间点(精确到小时)
awk '{print $4}' access.log |cut -c 14-15|sort|uniq -c|sort -nr|head -n 100
性能分析
在nginx log中最后一个字段加入$request_time
列出传输时间超过 3 秒的页面,显示前20条
cat access.log|awk '($NF > 3){print $7}'|sort -n|uniq -c|sort -nr|head -20
列出php页面请求时间超过3秒的页面,并统计其出现的次数,显示前100条
cat access.log|awk '($NF > 1 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100
蜘蛛抓取统计
统计蜘蛛抓取次数
grep 'Baiduspider' access.log |wc -l
统计蜘蛛抓取404的次数
grep 'Baiduspider' access.log |grep '404' | wc -l
TCP连接统计
查看当前TCP连接数
netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l
用tcpdump嗅探80端口的访问看看谁最高
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr
awk '{print $1}' $logpath |sort -n|uniq|wc -l
echo "=====系统正在统计某一个时间段IP访问量为====="
#grep "16/Jun/2017:1[8:9]" $logpath |awk '{print $1}'|sort|uniq -c|sort -nr|wc -l
#sed -n "16/Jun/2017:18:00:00/,16/Jun/2017:21:30:00/"p $logpath|awk '{print $1}'|sort|uniq -c|sort -nr|wc -l
#sed -n '/16\/Jun\/2017:18:00:00/,/16\/Jun\/2017:21:00:00/p' $logpath|awk '{print $1}'|sort|uniq -c|sort -nr|wc -l
#sed -n '/16\/Jun\/2017:1[9]/,/16\/Jun\/2017:2[1]/p' /usr/local/nginx/logs/access.log |wc -l
sed -n '/22\/Jun\/2017:1[5]/,/22\/Jun\/2017:1[6]/p' $logpath|awk '{print $1}'|sort -n|uniq|wc -l
echo "=====访问100次以上的IP====="
awk '{print $1}' $logpath|sort -n|uniq -c|awk '{if($1>100) print $0}'|sort -rn
echo "=====访问最频繁的请求(TOP50)====="
awk '{print $7}' $logpath |sort |uniq -c|sort -rn |head -n 50
echo "=====统计每秒的请求数(TOP50)======"
awk '{print $4}' $logpath|cut -c 14-21|sort |uniq -c|sort -nr|head -n 50
echo "=====统计每分钟的请求数(TOP50)====="
awk '{print $4}' $logpath|cut -c 14-18|sort|uniq -c|sort -nr|head -n 50
echo "=====统计每小时请求数(TOP50)====="
awk '{print $4}' $logpath|cut -c 14-15|sort|uniq -c|sort -nr|head -n 50
echo "=====传输时间超过1秒的请求(TOP20)====="
cat $logpath|awk '($NF > 1){print $7}'|sort -n|uniq -c|sort -nr|head -20
ip限制跟重定向配置
1.nginx 配置新旧域名过渡,把访问域名www.taobao.com的转发到新域名 www.tb.com上
配置方式如下
server{
server_name www.taobao.com;
#方式1 rewrite ^/(.*)$ http://www.tb.com/$1 permanent;
# 方式2
if($host != ‘www.tb.com’){
rewrite ^/(.*)$ http://www.tb.com/$1 permanent;
}
}
2.nginx 实现ip访问控制
server{
deny 192.168.66.80;
allow 192.168.66.0/24;
allow 192.16.88.0/16;
deny all;
}
3.nginx要禁止访问*.txt,*.doc配置如下:
location ~* \.(txt|doc)${
root /data/index;
deny all;
}
转载:https://blog.csdn.net/weixin_34101784/article/details/92620011