nginx总结

log_format⽤用于设置⽇日志格式,格式为log_format 格式名 样式
配置字段http
默认的combined
log_format combined '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ‘;
不不需要配置,默认的,如果在配置会提示重复nginx: [emerg] duplicate "log_format" name "combined" in /etc/nginx/nginx.conf:45
$server_name:虚拟主机名称
$remote_addr:远程客户端的IP地址
$remote_user:远程客户端⽤用户名称,⽤用于记录浏览者进⾏行行身份验证时提供的名字,⽆无则空⽩白
[$time_local]:访问的时间与时区[12/Jun/2016:20:18:19 +0800]
$request:请求的URI和HTTP协议(*)
$status:记录请求返回的http状态码 200/301/302/404/403/400/502
$http_referer:来源
$http_user_agent:客户端浏览器器信息
$http_x_forwarded_for:客户端的真实ip
$ssl_protocol:SSL协议版本
$ssl_cipher:交换数据中的算法,⽐比如RC4-SHA
$request_time:整个请求的总时间
$body_bytes_sent 发送给客户端的字节数,不不包括响应头的⼤大⼩小
$bytes_sent 发送给客户端的总字节数。
$connection 连接的序列列号。
$connection_requests 当前通过⼀一个连接获得的请求数量量。
$msec ⽇日志写⼊入时间。单位为秒,精度是毫秒。
$pipe 如果请求是通过HTTP流⽔水线(pipelined)发送,pipe值为“p”,否则为“.”。
$request_length 请求的⻓长度(包括请求⾏行行,请求头和请求正⽂文)。
在nginx.conf中http字段中添加
log_format yslog '$server_name $remote_addr - $remote_user[$time_local] "$request" '
'$status $body_bytes_sent "$http_referer"'
' "$http_user_agent" "$http_x_forwarded_for"';
查看当前服务器器tcp状态统计报告
netstat -n|grep ^tcp|awk '{print $NF}'|sort -nr|uniq -c
查看总请求数
wc -l nginx_nginx_access.log | awk '{print $1}’
查看独⽴立ip数
awk '{print $1}' nginx_nginx_access.log |sort |uniq |wc -l
每秒客户端请求数top5
awk -F'[ []' '{print $5}' nginx_access.log |sort|uniq -c|sort -rn|head -5
访问最频繁IP Top5
awk '{print $1}' nginx_access.log|sort |uniq -c | sort -rn |head -5
访问最频繁的URL TOP5
awk '{print $7}' nginx_access.log|sort |uniq -c | sort -rn |head -5
查找指定URL的访问频率
cat /app/nginx/logs/access.log |grep /tykf-itr-services/services/login/thirdParty|awk '{print $1}' | sort -nr | uniq -c | sort -n
查询访问最频繁的URL
awk '{print $7}' nginx_access.log|sort | uniq -c |sort -n -k 1 -r|more
根据时间段统计查看⽇日志
cat access.log| sed -n '/14/Mar/2015:21/,/14/Mar/2015:22/p'|more
响应⼤大于10秒的URL TOP5
awk '{if ($12 > 10){print $7}}' nginx_access.log|sort|uniq -c|sort -rn |head -5
HTTP状态码(⾮非200)统计 Top5
awk '{if ($13 != 200){print $13}}' nginx_nginx_access.log|sort|uniq -c|sort -rn|head -5
分析请求数⼤大于50000的源IP的⾏行行为
awk '{print $1}' nginx_access.log|sort |uniq -c |sort -rn|awk '{if ($1 > 5000){print $2}}' > tmp.txt
for i in $(cat tmp.txt)
do
echo $i >> analysis.txt
echo "访问⾏行行为统计" >> analysis.txt
grep $i nginx_access.log|awk '{print $6}' |sort |uniq -c | sort -rn |head -5 >> analysis.txt
echo "访问接⼝口统计" >> analysis.txt
grep $i nginx_access.log|awk '{print $7}' |sort |uniq -c | sort -rn |head -5 >> analysis.txt
echo -e "\n" >> /root/analysis/$Ydate.txt
done
统计来⾃自blog.rekfan.com 包含 ________wd=***********&_____ 并将***********提取出来
awk '{print $11}' nginx.log|head -n 50|grep "blog.rekfan.com"|sed 's/^.*wd=//g'| sed 's/&.*$//g’
利利⽤用grep ,wc命令统计某个请求或字符串串出现的次数,⽐比如我要统计GET /task/showContent接⼝口在某天的调⽤用次数,则可以使⽤用如下
命令:
cat nginx-ad-access.log | grep 'GET /task/showContent' | wc -l,
其中cat⽤用来读取⽇日志内容,grep进⾏行行匹配的⽂文本搜索,wc则进⾏行行最终的统计。
当然只⽤用grep也能实现上述功能:
grep 'GET /task/showContent' nginx-ad-access.log -c
统计所有接⼝口的调⽤用次数并显示出现次数最多的前⼆二⼗十的URL
cat nginx-ad-access.log|awk '{split($7,b,"?");COUNT[b[1]]++;}END{for(a in COUNT) print COUNT[a], a}'|sort -k1 -nr|head -n20
这⾥里里awk是按照空格把每⼀一⾏行行⽇日志拆分成若⼲干项,其中$7对应的就是URL,当然具体对应的内容和使⽤用nginx时设置的⽇日志格式有关。
这样就可以通过拆分提取出IP,URL,状态码等信息。split是awk的内置函数,在此的意思是按照“?”将URL进⾏行行分割得到⼀一个数组,
并赋值给b。COUNT[b[1]]++表示相同的接⼝口数⽬目加1。sort⽤用来排序,-k1nr表示要把进⾏行行排序的第⼀一列列作为数字看待,并且结果倒序
排列列。head -n20意为取排名前⼆二⼗十的结果。
统计报错的接⼝口,统计nginx⽇日志中报错较多的接⼝口,对于分析服务器器的运⾏行行情况很有帮助,也可以有针对性的修复bug和性能优化。
cat nginx-ad-access.log|awk'{if($9==500) print $0}'|awk '{split($7,b,"?");COUNT[b[1]]++;}END{for(a in COUNT) print COUNT[a],
a}'|sort -k 1 -nr|head -n10
先⽤用awk’{if( 9==500)print 0}’过滤出500错误的⽇日志,然后在此基础上做统计。
统计HTTP响应状态码
通过统计响应状态码可以看出服务器器的响应情况,⽐比如499较多时可以判断出服务器器响应缓慢,再结合3可以找出响应慢的接⼝口,这样
就能有针对性进⾏行行性能分析和优化
cat nginx-ad-access.log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}' | sort -k 2 -nr
统计服务器器并发量量
cat nginx-ad-access.log |grep '10.15.19.138'| awk '{COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}'|sort -k 2 -nr|head -n20
nginx转发请求时可以记录响应请求的服务器器IP,先通过grep过滤出某个服务器器所有的请求,然后统计各个时间点的并发请求响应的数量量
即可得到某个服务器器的并发量量。$4对应的是响应时间。当然,如果把grep的内容更更换成某个接⼝口也就可以统计出该接⼝口对应的并发量量
了了。
grep多条件与或操作
有时候我们需要在nginx⽇日志通过多个条件来查找某些特定请求,⽐比如我需要找个某个⽤用户浏览⽂文章的请求,则可以需要同时匹配两个
条件:浏览⽂文章接⼝口GET /task/showContent和userId=59h7hrrn,grep对应的与操作命令如下:
grep -E "GET /task/showContent.*userId=59h7hrrn" nginx-ad-access.log
grep与命令格式: grep -E “a.*b” file,ab条件同时成⽴立
⽽而grep或命令的格式为:grep -E “a|b” file ,ab两个条件有⼀一个成⽴立即可。
grep打印匹配的前后⼏几⾏行行
有时候我们需要查找某个特定请求的前后⼏几⾏行行的请求,以观察⽤用户的关联操作情况。grep提供了了⼀一下⼏几条命令:
grep -C 5 ‘parttern’ inputfile //打印匹配⾏行行的前后5⾏行行
grep -A 5 ‘parttern’ inputfile //打印匹配⾏行行的后5⾏行行
grep -B 5 ‘parttern’ inputfile //打印匹配⾏行行的前5⾏行行
⽐比如grep -C 10 ‘GET /task/showContent/1agak?userId=59h7hrrn’ nginx-ad-access.log就可以打印出该请求的前后10条请求 .
查看某⼀一个url被访问的次数:
grep "/index.php" log_file | wc -l
查看每⼀一个IP访问了了多少个⻚页⾯面:
awk '{++S[$1]} END {for (a in S) print a,S[a]}' log_file > log.txt
sort -n -t ' ' -k 2 log.txt # 配合sort进⼀一步排序
将每个IP访问的⻚页⾯面数进⾏行行从⼩小到⼤大排序:
awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -n
查看某⼀一个IP访问了了哪些⻚页⾯面:
grep ^111.111.111.111 log_file| awk '{print $1,$7}'
去掉搜索引擎统计的⻚页⾯面:
awk '{print $12,$1}' log_file | grep ^\"Mozilla | awk '{print $2}' |sort | uniq | wc -l
查看2015年年8⽉月16⽇日14时这⼀一个⼩小时内有多少IP访问:
awk '{print $4,$1}' log_file | grep 16/Aug/2015:14 | awk '{print $2}'| sort | uniq | wc -l
查看访问前⼗十个ip地址
awk '{print $1}' |sort|uniq -c|sort -nr |head -10 access_log
# uniq -c 相当于分组统计并把统计数放在最前⾯面
cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
cat access.log|awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}
访问次数最多的10个⽂文件或⻚页⾯面
cat log_file|awk '{print $11}'|sort|uniq -c|sort -nr | head -10
cat log_file|awk '{print $11}'|sort|uniq -c|sort -nr|head -20
awk '{print $1}' log_file |sort -n -r |uniq -c | sort -n -r | head -20 # 访问量量最⼤大的前20个ip
通过⼦子域名访问次数,依据referer来计算,稍有不不准
cat access.log | awk '{print $11}' | sed -e ' s/http:\/\///' -e ' s/\/.*//' | sort | uniq -c | sort -rn | head -20
列列出传输⼤大⼩小最⼤大的⼏几个⽂文件
cat www.access.log |awk '($7~/\.php/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -100
列列出输出⼤大于200000byte(约200kb)的⻚页⾯面以及对应⻚页⾯面发⽣生次数
cat www.access.log |awk '($10 > 200000 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100
如果⽇日志最后⼀一列列记录的是⻚页⾯面⽂文件传输时间,则有列列出到客户端最耗时的⻚页⾯面
cat www.access.log |awk '($7~/\.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -100
列列出最最耗时的⻚页⾯面(超过60秒的)的以及对应⻚页⾯面发⽣生次数
cat www.access.log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100
列列出传输时间超过 30 秒的⽂文件
cat www.access.log |awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -20
列列出当前服务器器每⼀一进程运⾏行行的数量量,倒序排列列
ps -ef | awk -F ' ' '{print $8 " " $9}' |sort | uniq -c |sort -nr |head -20
查看apache当前并发访问数
#对⽐比httpd.conf中MaxClients的数字差距多少。
netstat -an | grep ESTABLISHED | wc -l
可以使⽤用如下参数查看数据
ps -ef|grep httpd|wc -l
#1388
#统计httpd进程数,连个请求会启动⼀一个进程,使⽤用于Apache服务器器。
#表示Apache能够处理理1388个并发请求,这个值Apache可根据负载情况⾃自动调整。
netstat -nat|grep -i "80"|wc -l
#4341
#netstat -an会打印系统当前⽹网络链接状态,⽽而grep -i "80"是⽤用来提取与80端⼝口有关的连接的,wc -l进⾏行行连接数统计。
#最终返回的数字就是当前所有80端⼝口的请求总数。
netstat -na|grep ESTABLISHED|wc -l
#376
#netstat -an会打印系统当前⽹网络链接状态,⽽而grep ESTABLISHED 提取出已建⽴立连接的信息。 然后wc -l统计。
#最终返回的数字就是当前所有80端⼝口的已建⽴立连接的总数。
netstat -nat||grep ESTABLISHED|wc
#可查看所有建⽴立连接的详细记录
输出每个ip的连接数,以及总的各个状态的连接数
netstat -n | awk '/^tcp/ {n=split($(NF-1),array,":");if(n<=2)++S[array[(1)]];else++S[array[(4)]];++s[$NF];++N} END {for(a in S)
{printf("%-20s %s\n", a, S[a]);++I}printf("%-20s %s\n","TOTAL_IP",I);for(a in s) printf("%-20s %s\n",a, s[a]);printf("%-20s
%s\n","TOTAL_LINK",N);}'
分析⽇日志⽂文件下 2012-05-04 访问⻚页⾯面最⾼高 的前20个 URL 并排序
cat access.log |grep '04/May/2012'| awk '{print $11}'|sort|uniq -c|sort -nr|head -20
查询受访问⻚页⾯面的URL地址中 含有 www.abc.com ⽹网址的 IP 地址
cat access_log | awk '($11~/\www.abc.com/){print $1}'|sort|uniq -c|sort -nr
获取访问最⾼高的10个IP地址 同时也可以按时间来查询
cat linewow-access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
时间段查询⽇日志时间段的情况
cat log_file | egrep '15/Aug/2015|16/Aug/2015' |awk '{print $1}'|sort|uniq -c|sort -nr|head -10
分析2015/8/15 到 2015/8/16 访问"/index.php?g=Member&m=Public&a=sendValidCode"的IP倒序排列列
cat log_file | egrep '15/Aug/2015|16/Aug/2015' | awk '{if($7 == "/index.php?g=Member&m=Public&a=sendValidCode") print
$1,$7}'|sort|uniq -c|sort -nr
($7~/\.php/) $7⾥里里⾯面包含.php的就输出,本句句的意思是最耗时的⼀一百个PHP⻚页⾯面
cat log_file |awk '($7~/\.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -100
列列出最最耗时的⻚页⾯面(超过60秒的)的以及对应⻚页⾯面发⽣生次数
cat access.log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100
统计⽹网站流量量(G)
cat access.log |awk '{sum+=$10} END {print sum/1024/1024/1024}'
统计404的连接
awk '($9 ~/404/)' access.log | awk '{print $9,$7}' | sort
统计http status.
cat access.log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}'
cat access.log |awk '{print $9}'|sort|uniq -c|sort -rn
每秒并发:
watch "awk '{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}' log_file|sort -k 2 -nr|head -n10"
带宽统计
cat apache.log |awk '{if($7~/GET/) count++}END{print "client_request="count}'
cat apache.log |awk '{BYTE+=$11}END{print "client_kbyte_out="BYTE/1024"KB"}'
找出某天访问次数最多的10个IP
cat /tmp/access.log | grep "20/Mar/2011" |awk '{print $3}'|sort |uniq -c|sort -nr|head
当天ip连接数最⾼高的ip都在⼲干些什什么:
cat access.log | grep "10.0.21.17" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10
⼩小时单位⾥里里ip连接数最多的10个时段
awk -vFS="[:]" '{gsub("-.*","",$1);num[$2" "$1]++}END{for(i in num)print i,num[i]}' log_file | sort -n -k 3 -r | head -10
找出访问次数最多的⼏几个分钟
awk '{print $1}' access.log | grep "20/Mar/2011" |cut -c 14-18|sort|uniq -c|sort -nr|head
取5分钟⽇日志
if [ $DATE_MINUTE != $DATE_END_MINUTE ] ;then #则判断开始时间戳与结束时间戳是否相等START_LINE=`sed -n
"/$DATE_MINUTE/=" $APACHE_LOG|head -n1` #如果不不相等,则取出开始时间戳的⾏行行号,与结束时间戳的⾏行行号
查看tcp的链接状态
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}'
netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'
netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
netstat -ant|awk '/ip:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -n
netstat -ant|awk '/:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -rn|head -n 10
awk 'BEGIN{printf ("http_code\tcount_num\n")}{COUNT[$10]++}END{for (a in COUNT) printf a"\t\t"COUNT[a]"\n"}'
查找请求数前20个IP(常⽤用于查找攻来源):
netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n20
⽤用tcpdump嗅探80端⼝口的访问看看谁最⾼高
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20
查找较多time_wait连接
netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
找查较多的SYN连接
netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
根据端⼝口列列进程
netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1
查看了了连接数和当前的连接数
netstat -ant | grep $ip:80 | wc -l
netstat -ant | grep $ip:80 | grep EST | wc -l
查看IP访问次数
netstat -nat|grep ":80"|awk '{print $5}' |awk -F: '{print $1}' | sort| uniq -c|sort -n
分析⽇日志查看当天的ip连接数
cat default-access_log | grep “10/Dec/2010″ | awk ‘{print $2}’ | sort | uniq -c | sort -nr
查看指定的ip在当天究竟访问了了什什么url
cat default-access_log | grep “10/Dec/2010″ | grep “218.19.140.242″ | awk ‘{print $7}’ | sort | uniq -c | sort -nr
查看访问次数最多的⼏几个分钟(找到热点)
awk ‘{print $4}’ default-access_log |cut -c 14-18|sort|uniq -c|sort -nr|head
统计⼀一个⽂文本中包含字符个数
cat pic.access.log |grep /2012/ |wc -l
cat c_access.log |grep "/message/publishmsg/\|/message/publish/" >test1.log
查看当天返http状态代码. 当天(26/Mar/2015)
cat nginx_access.log | grep "26/Mar/2015" | awk '{print $9}'|sort|uniq -c|sort -nr
cat nginx_access.log | grep "26/Mar/2015" | awk '{print $9}'|sort|uniq -c|sort -nr
GET请求访问最多的⻚页⾯面
awk '{print $6,$7}' nginx_access.log |grep GET |sort|uniq -c|sort -rn|head -n 10
awk '{print $6,$7}' nginx_access.log |grep GET |sort|uniq -c|sort -rn|head -n 10
POST请求访问最多的⻚页⾯面
awk '{print $6,$7}' nginx_access.log |grep POST |sort|uniq -c|sort -rn|head -n 10
awk '{print $6,$7}' nginx_access.log |grep POST |sort|uniq -c|sort -rn|head -n 10
查看某⼀一时间段的ip统计数 (35-37分内的)
cat nginx_access.log |grep "26/Mar/2015:14:3[5-7]"| awk '{print $1}' |sort|uniq -c|sort -nr
cat nginx_access.log |grep "26/Mar/2015:14:3[5-7]"| awk '{print $1}' |sort|uniq -c|sort -nr
统计404错误⻚页⾯面
cat nginx_access.log |awk '{ if( $9 == 404 ) print $9,$11}'|grep http|uniq -c
cat nginx_access.log |awk '{ if( $9 == 404 ) print $9,$11}'|grep http|uniq -c
访问内容最⼤大的⽂文件排序
cat nginx_access.log |awk '{print $10,$11,$7}'|sort -r -k1 -n|head -n 10
cat nginx_access.log |awk '{print $10,$11,$7}'|sort -r -k1 -n|head -n 10

posted @ 2019-11-04 13:54  大川哥  阅读(255)  评论(0编辑  收藏  举报