Nginx 日志处理
过滤冗杂日志
使用官网默认模块 ngx_http_map_module过滤指定 URL 或者IP 不在日志中进行记录
官网地址
配置proxy
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# $request_uri 系统内设变量
# $loggable 自定义变量
# if=$loggable 引用判断
map $request_uri $loggable {
"/" 0;
"/health.html" 0;
default 1;
}
log_format log_main '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"server_host":"$host",'
'"url":"$uri",'
'"url-info":"$request_uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log /path/access.log log_main if=$loggable;
配置 Nginx 获取真实IP
配置阿里云 SLB 负载 ECS 服务器获取真实用户 IP
官网地址无锡妇科医院排行榜 http://mobile.chfk120.com/
意思是排除掉掉set_real_ip_from 100.64.0.0/10中的 IP 剩下的 IP 就是用户真实 IP
确认 Nginx 安装了 realip 模块
# 可在编译过程增加 --with-http_realip_module
nginx -V |grep realip
配置对应的配置文件
log_format log_main '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"server_host":"$host",'
'"url":"$uri",'
'"url-info":"$request_uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log /path/access.log log_main;
配置文件(http、server、location)增加段配置
set_real_ip_from 100.64.0.0/10; # 阿里云 SLB 内网地址
set_real_ip_from 172.17.0.0/16; # 过滤docker 内网 IP
real_ip_header X-Forwarded-For;
real_ip_recursive on;