图片 1

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

注:
nginx 1.1.3之后已经默认支持mp4,flv模块,无须第三方模块支持。
 
操作步骤:
1:解压,编译nginx,编译添加mp4,flv模块:
# tar -zxvf nginx-1.3.14.tar.gz
# cd nginx-1.3.14
# ./configure 
# ./configure –prefix=/usr/local/nginx –user=nobody –group=nobody
–with-select_module –with-poll_module –with-file-aio
–with-http_ssl_module –with-http_realip_module
–with-http_gzip_static_module –with-http_secure_link_module
–with-http_sub_module –with-http_stub_status_module
–with-http_perl_module –with-http_mp4_module
–with-http_flv_module
# make&& make install

需求:访问log.linuxidc.com/q.gif
成功,其他路径则返回404,且404不记录日志

2:配置nginx.conf文件:(server选项配置)
# limit_conn_zone $binary_remote_addr zone=perip:10m;
server {
    listen 80 ;
    server_name 117.27.135.82;
    root  /usr/local/nginx/html/mp4_file/;    ———-根路径
    limit_rate 256k;
 
    location ~ \.flv$                      ———匹配flv文件
    {
        flv;
    }
 
    location ~ \.mp4$                    ———-匹配mp4文件
    {
    mp4;
#    mp4_buffer_size    1m;       
#    mp4_max_buffer_size  5m;
    limit_rate_after 5m;                ———下载5m后再限速
    limit_rate 100k;                    ———-限速100k
#    limit_conn perip 1;
    }

第一步,配置nginx rewrite
1.配置nginx rewrite
 server {
    listen      8000;
    server_name log.linuxidc.com;
    #index q.gif;
    root /app/data/qm_log;
     
#如果url不是q.gif,则返回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;
}

3:下载JWplayer,实现视频可拖拽功能。解压,并把player.swf文件放到视频文件目录下。

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

播放测试:
 在根目录下放入mp4文件VGA.mp4。在浏览器上播放视频。
 地址: 
(player.swf为播放器,VGA.mp4为要播放的视频文件)
 能正常播放,说明配置成功,如下图:

3.测试访问
访问正确路径
curl

图片 1

日志:
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

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

访问错误路径:
curl

CentOS
6.2实战部署Nginx+MySQL+PHP
http://www.linuxidc.com/Linux/2013-09/90020.htm

日志:
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搭建WEB服务器
http://www.linuxidc.com/Linux/2013-09/89768.htm

第二步,nginx 404不记录日志
1.下载模块
地址:

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程
http://www.linuxidc.com/Linux/2013-09/89692.htm

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’);
    }
}

CentOS 6.3下Nginx性能调优
http://www.linuxidc.com/Linux/2013-09/89656.htm

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

CentOS 6.3下配置Nginx加载ngx_pagespeed模块
http://www.linuxidc.com/Linux/2013-09/89657.htm

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

CentOS 6.4安装配置Nginx+Pcre+php-fpm
http://www.linuxidc.com/Linux/2013-08/88984.htm

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

Nginx安装配置使用详细笔记
http://www.linuxidc.com/Linux/2014-07/104499.htm

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

Nginx日志过滤 使用ngx_log_if不记录特定日志
http://www.linuxidc.com/Linux/2014-07/104686.htm

新的模块以及生效

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

6.增加ninx 404状态不记录日志
server {
    listen      8000;
    server_name log.linuxidc.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;
}

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133142.htm

7.重启nginx进程