Nginx日志细节处理

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

官网地址
意思是排除掉掉set_real_ip_from 100.64.0.0/10中的 IP 剩下的 IP 就是用户真实 IP

  1. 确认 Nginx 安装了 realip 模块

    # 可在编译过程增加 --with-http_realip_module
     nginx -V |grep realip
    
  2. 配置对应的配置文件

    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;
    
  3. 配置文件(httpserverlocation)增加段配置

    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;
    
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值