Nginx rwrite and add Nginx modules that do not record specific statuses

Source: Internet
Author: User

Nginx rwrite and add Nginx modules that do not record specific statuses

Requirement: log.bkjia.com/q.gif is successfully accessed, 404 is returned for other paths, and 404 is not logged.

Step 1: Configure nginx rewrite
1. Configure nginx rewrite
Server {
Listen 8000;
Server_name log.bkjia.com;
# Index q.gif;
Root/app/data/qm_log;

If urlis not q.gif, 404 is returned.
If ($ uri !~ ^/Q.gif ){
Return 404;
}
Location ~ *. * \. (Jpg | jpeg | gif | png | bmp) $ {
Expires 30d;
Break;
}
Error_log/app/data/log/nginx/www. error. log;
}

2. Restart nginx
/Etc/init. d/nginx restart

3. Test Access
Access to the correct path
Curl http://log.bkjia.com/q.gif

Logs:
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

Access Error Path:
Curl http://log.bkjia.com/c.gif

Logs:
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

Step 2: nginx 404 does not record logs
1. Download Module
Address: https://github.com/cfsego/ngx_log_if

2. Usage 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.
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. decompress the downloaded file
Cd/tmp/soft/
Unzip ngx_log_if-master.zip

4. recompile nginx without Overwriting
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

Back up the original file and copy the new file:
Mv/app/local/nginx/sbin/nginx. bak
Cp./objs/nginx/app/local/nginx/sbin/

5. view the module
#/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

New modules and effective modules

6. added the n1_404 status to keep logs.
Server {
Listen 8000;
Server_name log.bkjia.com;
# Index q.gif;
Root/app/data/qm_log;
Access_log_bypass_if ($ status = 404 );

If ($ uri !~ ^/Q.gif ){
Return 404;
}

Location ~ *. * \. (Jpg | jpeg | gif | png | bmp) $ {
Expires 30d;
Break;
}

Error_log/app/data/log/nginx/www. error. log;
}

7. Restart the nginx Process

/Etc/init. d/nginx restart

8. Access the Error Path again and find 404 or not recorded

Tailf/app/data/log/nginx/access. log

For more Nginx tutorials, see the following:

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.