nginx自定义日志获取CDN真实IP,使用简单语句获取日志常用信息

nginx自定义日志获取CDN真实IP,使用简单语句获取日志常用信息

1.使用网宿的CDN

需要获取用户的真实IP,但是$remote_addr使用获取的是CDN的地址。

修改nginx的配置:

log_format  main_zdy  '$request_time - IP:$remote_addr - RealIP:$HTTP_CDN_SRC_IP - [$time_local] $request - $status - $http_user_agent';

在站点配置文件中添加:

access_log /data/logs/www.funet8.com-access.log main_zdy;

生成的文件格式:

0.192 – IP:xxx.xxx.227.107 – RealIP:xxx.xxx.250.173 – [19/Jul/2016:11:02:42 +0800] GET /play/gid/xxx HTTP/1.1 – 200 – Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)

网宿CDN帮助文档:
https://si.chinanetcenter.com/purview/help/FAQ.html?menuCode=other#41

24.使用CDN加速后如何获取最终用户IP?
源站可通过CDN回源请求header的特定字段获取最终用户IP。
CDN支持两种字段获取:
1、 默认配置通过Cdn-Src-Ip字段获取。例:
GET / HTTP/1.1
Host: www.abc.com
User-Agent: testagent
Cdn-Src-Ip:1.1.1.1
2、 可配置通过X-fowarded-for字段获取。例:
GET / HTTP/1.0
Host: www.abc.com
User-Agent: testagent
X-fowarded-for:1.1.1.1
若用我司提供的CDN-SRC-IP方式则可根据贵司网站编写的脚本语言自行选择下列代码(注意:以下代码需要在切换后才能正式生效,如未切换则可能导致获取IP错误)。
Php:
$user_IP = ($_SERVER[“HTTP_CDN_SRC_IP”],1);
Asp:
<% Request.ServerVariables(“HTTP_Cdn-Src-Ip”) %>
Java:
String srcIp = request.getHeader(“Cdn-Src-Ip”,1);
Asp(用C#):
String srcIp = Request.Headers[“Cdn-Src-Ip”];
Asp.net:
如贵司用ServerVariables来获取参数,因为ServerVariables有编码解码的过程,因此应该用HTTP_Cdn_Src_Ip来获取变量,如:
result = HttpContext.Current.Request.ServerVariables[“HTTP_Cdn_Src_Ip”];


2.使用的是阿里云CDN

使用$http_x_forwarded_for这个变量获取用户真实IP

log_format  main_aliyun  '$request_time - IP:$remote_addr - RealIP:$http_x_forwarded_for - [$time_local] $request - $status - $http_user_agent';

在站点配置文件中添加:

access_log /data/logs/www.funet8.com-access.log main_aliyun; 

生成的nginx日志文件格式:
0.078 – IP:xxx.xxx.128.65 – RealIP:xxx.xxx.103.185 – [19/Jul/2016:11:18:45 +0800] GET /game?appid=60 HTTP/1.1 – 200 – Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586

阿里云CDN帮助文档
https://help.aliyun.com/knowledge_detail/40074.html

3.使用语句获取信息


1.获取真实用户的IP

cat /data/logs/www.funet8.com-access.log |awk '{print $5}'| awk -F":" '{print $NF}' |sort|uniq -c|sort -nr|head -10

2.获取CDN的ip或者绑定hosts的ip

cat /data/logs/www.funet8.com-access.log |awk '{print $3}'| awk -F":" '{print $NF}' |sort|uniq -c|sort -nr|head -10

3.输出404url地址到/root/404page.txt

awk '($9 ~/404/)' /data/wwwroot/log/www.7477.com-error.log | awk '{print $9,$7}' | sort > /root/404page.txt
cat /data/logs/www.funet8.com-error.log |grep 'No such file or directory' | awk '{print $7,$16,$21}' |uniq -c |sort -nr >/root/404url.txt

4.访问次数最多的时间段

awk '{print  $7}' /data/logs/www.funet8.com-access.log|cut -c 14-18|sort|uniq -c|sort -nr|head -n 10

5.查看访问排行前10的url

cat /data/logs/www.funet8.com-access.log | awk '{print $10}' | sort | uniq -c | sort -nr | head -n 10

6.查看某天访问排行前10的url:

cat /data/logs/www.funet8.com-access.log | grep "10/Dec/2010" | awk '{print $10}' | sort | uniq -c | sort -nr | head -n 10

7.查看访问日志执行时间最长的前10条的记录

cat /data/logs/www.funet8.com-access.log | sort -nr | head -n 10

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇