当前位置: 移动技术网 > IT编程>软件设计>架构 > Nginx日志细节处理

Nginx日志细节处理

2020年07月17日  | 移动技术网IT编程  | 我要评论

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;
    

本文地址:https://blog.csdn.net/u011607971/article/details/85990800

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网