NGINX 日志log 格式定义(format) & 筛选记录(map)

一、 NGINX日志格式自定义(format)

在http配置中添加log_format,并在使用log的地方选择模板,可以按照自定义格式输出日志。

这里需要注意的点:如果要在http中引入其他文件的配置,其他文件中若需要使用nginx.conf中的log_format,请将log_format配置放置于include语句之前。

# /nginx/conf/nginx.conf

worker_processes  2;

error_log  logs/error.log  info;

events {
    worker_connections  1024;
}


http {
	# 自定义模板
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

	# 在日志输出位置后加上模板名称,则表示使用自定义模板输出日志
    access_log  logs/access.log  main;
    
    include       mime.types;
    
    # 如果include中内容需要使用模板,需要将include语句放置于模板定义之后
    include hosts/*.conf;
    include hosts/upstm/*.conf;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

    #gzip  on;
    client_max_body_size 20M;
    client_body_buffer_size 20M;
    fastcgi_intercept_errors on;

    server {
        listen       8088;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

二、NGINX日志筛选记录(map)

使用map,可以控制哪些日志输出,哪些日志不输出。假设一种情况:我们控制某台ip的访问日志不打入至log中,则可以使用remote字段+正则来过滤日志,具体实现方式如下:

http {
	# 使用log_format自定义模板
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    # 使用map定义过滤规则,map [字段变量] [过滤规则名称定义] {}
    map $remote_addr $iploggable {
        default 1; # default 为默认情况下,1为输出,0为不输出
        ~^172.28.126.101 0; # ~后跟正则,^172.28.126.101为正则(表示以这个ip开头的),0表示不输出,所以此过滤规则为:172.28.126.101的访问请求日志不打印
    }

	# 在日志输出位置后加上模板名称,则表示使用自定义模板输出日志,在模板名称后加上if=[过滤规则],则可以控制当前日志过滤规则
    access_log  logs/access.log  main if=$iploggable;
}

假设另外一种情况,有探活的机器来检查我们项目的check_health.html文件,并且频率极高,这样会导致我们log文件增长快,并且记录不到真实的访问log,根据请求的log规律,我们可以这样设置:
在这里插入图片描述

http {
	# 使用log_format自定义模板
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" - $uri'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    # 使用map定义过滤规则,map [字段变量] [过滤规则名称定义] {}
    map $uri $check_health_log {
        default 1; # default 为默认情况下,1为输出,0为不输出
        ~^/check_health.html 0; # ~后跟正则,^/check_health.html为正则,0表示不输出,所以此过滤规则为:uri为/check_health.html 的访问请求日志不打印
    }

	# 在日志输出位置后加上模板名称,则表示使用自定义模板输出日志,在模板名称后加上if=[过滤规则],则可以控制当前日志过滤规则
    access_log  logs/access.log  main if=$check_health_log;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值