nginx rwrite及增加不记录特定状态日志Nginx模块

2.重启nginx
/etc/init.d/nginx restart

日志:
12 192.168.1.11 - - [31/May/2016:13:56:47 +0800] "GET /q.gif HTTP/1.1" 200 43 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36" - 968 1464674207.162 7 0.000
192.168.1.11 - - [31/May/2016:13:56:47 +0800] "GET /q.gif HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36" - 1048 1464674207.736 8 0.000

日志:
192.168.1.11 - - [31/May/2016:13:48:33 +0800] "GET / HTTP/1.1" 404 564 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36" - 1068 1464673713.147 8 0.000

第二步,nginx 404不记录日志
1.下载模块
地址:https://github.com/cfsego/ngx_log_if

2.用法样例:
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.
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');
    }
}

3.解压下载文件
cd /tmp/soft/
unzip ngx_log_if-master.zip

4.重新编译nginx,不覆盖编译
cd /tmp/soft/nginx-1.9.14
 ./configure  \
--prefix=/app/local/nginx \
--pid-path=/app/local/nginx \
--user=nginx \
--group=nginx \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module  \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_perl_module \
--with-mail \
--with-http_gzip_static_module \
--with-http_auth_request_module  \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module \
--with-zlib=/tmp/soft/zlib-1.2.8 \
--with-stream \
--with-stream_ssl_module \
--with-pcre=/tmp/soft/pcre-8.37 \
--with-openssl=/tmp/soft/openssl-1.0.2 \
--with-libatomic \
--add-module=/tmp/soft/ngx_log_if-master
make

备份原文件及拷贝新生成文件:
mv /app/local/nginx/sbin/nginx /app/local/nginx/sbin/nginx.bak
 cp ./objs/nginx /app/local/nginx/sbin/

5.查看模块
# /app/local/nginx/sbin/nginx -V
nginx version: nginx/1.9.14
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.0.2 22 Jan 2015
TLS SNI support enabled
configure arguments: --prefix=/app/local/nginx --pid-path=/app/local/nginx --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_perl_module --with-mail --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module --with-zlib=/tmp/soft/zlib-1.2.8 --with-stream --with-stream_ssl_module --with-pcre=/tmp/soft/pcre-8.37 --with-openssl=/tmp/soft/openssl-1.0.2 --with-libatomic --add-module=/tmp/soft/ngx_log_if-master

新的模块以及生效

7.重启nginx进程

/etc/init.d/nginx restart

8.再次访问错误路径,发现404以及不记录

tailf /app/data/log/nginx/access.log

更多Nginx相关教程见以下内容

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

相关推荐