Nginx使用ngx_log_if模块实现指定条件不记录日志

一般情况下我们不对css,ttf,图片的日志进行记录,此时我们可以使用access_log off指令来指定location不记录日志,但是如果我们想针对指定ua的请求指定uri指定返回http响应码等条件不记录请求该如何实现呢?这就需要用到ngx_log_if模块。

一、access_log off指令

该指令我们常在静态文件location内使用,在该location内同时设置静态文件缓存规则和关闭日志记录

该配置段匹配任意路径包含以下后缀名的资源,设置其过期时间为15天关闭日志记录,并设置资源防盗链为自己的域名,表明只有自己的域名能访问这些资源

nginx配置示例

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|ttf|ico)$ { # 指定需要使用防盗链的媒体资源
    access_log  off;                                        # 不记录日志
    expires 15d;                                            # 设置缓存时间
    valid_referers  *.awolon.fun;         # 表示仅允许这些域名访问上面的媒体资源
}

二、ngx_log_if模块

该模块是第三方的nginx模块,能实现指定条件不记录日志的功能,我们需要手动编译nginx时将该模块源代码编译进去

1.模块下载

请访问github仓库链接下载仓库至服务器

或者访问该直链下载zip文件

2.移动位置

将下载的文件重命名为解压后重命名为ngx_log_if

然后将其移动到/usr/local/src目录下

↓↓采用apt安装nginx或者不会源码编译添加模块的同学请先参考↓↓

Ubuntu 将Apt安装的Nginx 1.18.0 平滑升级到1.22.0

请先完成该文章中第二章节获取源码准备编译环境的内容

↓↓若你有源码编译的经验↓↓

3.查看已经安装的nginx模块

nginx -V
## 示例
nginx version: nginx/1.22.0
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module --with-http_image_filter_module --with-http_geoip_module --with-http_xslt_module --with-mail --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --sbin-path=/usr/sbin

将configure arguments冒号后的全部参数都复制下来,需要注意的是这个是我源码安装后的选项,所以末尾是有–sbin-path=/usr/sbin参数的,你可以使用which nginx来查看nginx二进制文件位置,并更换为自己的位置

4.编译

在nginx源码路径下使用如下格式的编译设置指令

./configure 先前编译参数 追加参数 

先前编译参数就是我们使用nginx -V后复制的编译参数

追加参数添加以下内容

--add-module=/usr/local/src/ngx_log_if

最终示例编译参数如下

./configure --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module --with-http_image_filter_module --with-http_geoip_module --with-http_xslt_module --with-mail --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --sbin-path=/usr/sbin --add-module=/usr/local/src/ngx_log_if

最后使用make命令编译nginx

进入obj路径即可看到新编译的二进制文件

使用./nginx -V来测试新二进制文件内是否有ngx_log_if模块

使用./nginx -t来测试是否与当前配置文件兼容

5.替换二进制文件

使用which nginx来查看nginx二进制文件路径

备份后将新二进制文件移动过来,并重启nginx服务即可

systemctl restart nginx

三、ngx_log_if模块使用

This directive allows you to define multiple conditions in the form of using the directive in multiple times in the same block. For example:

允许多次使用

server {
    access_log_bypass_if ($status = 400);
    access_log_bypass_if ($host ~* 'nolog.com');
    access_log_bypass_if ($uri = 'status.nginx') and;
    access_log_bypass_if ($status = 200);
}

In the case, nginx will not write access log when the status code is 400, or when the host is ‘nolog.com’, or when the uri is ‘status.nginx’ and the status code is 200.

在http400响应码,主机是’nolog.com’,请求uri是"status.nginx"并且它的http响应码是200的这些条件下nginx都不会记录日志

However, if you define them both in the blocks in the father child relationship, the child block will not inherit and merge the configuration in parent block, of course. FOr example:

server {
    access_log_bypass_if ($status = 400);

    location / {
        access_log_bypass_if ($host ~* 'nolog.com');
    }
}

In this case above, in location “/”, only “( h o s t   ∗ ′ n o l o g . c o m ′ ) " i s a v a i l a b l e , b u t " ( host ~* 'nolog.com')" is available, but "( host nolog.com)"isavailable,but"(status = 400)” is ignored and makes no difference.

http状态码400的过滤条件被忽略

四、参考文章

nginx 不记录指定类型日志

cfsego/ngx_log_if

  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值