阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

436次阅读
没有评论

共计 4644 个字符,预计需要花费 12 分钟才能阅读完成。

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

第一步,配置 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;
}

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

3. 测试访问
访问正确路径
curl http://log.linuxidc.com/q.gif

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

访问错误路径:
curl http://log.linuxidc.com/c.gif

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

新的模块以及生效

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

7. 重启 nginx 进程

/etc/init.d/nginx restart

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

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

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

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

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

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

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

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

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

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

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

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

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

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4644字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7987161
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
安装并使用谷歌AI编程工具Antigravity(亲测有效)

安装并使用谷歌AI编程工具Antigravity(亲测有效)

  安装并使用谷歌 AI 编程工具 Antigravity(亲测有效) 引言 Antigravity...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
支付宝、淘宝、闲鱼又双叕崩了,Cloudflare也瘫了连监控都挂,根因藏在哪?

支付宝、淘宝、闲鱼又双叕崩了,Cloudflare也瘫了连监控都挂,根因藏在哪?

支付宝、淘宝、闲鱼又双叕崩了,Cloudflare 也瘫了连监控都挂,根因藏在哪? 最近两天的互联网堪称“故障...
亚马逊云崩完,微软云崩!当全球第二大云“摔了一跤”:Azure 宕机背后的配置风险与警示

亚马逊云崩完,微软云崩!当全球第二大云“摔了一跤”:Azure 宕机背后的配置风险与警示

亚马逊云崩完,微软云崩!当全球第二大云“摔了一跤”:Azure 宕机背后的配置风险与警示 首先来回顾一下 10...
星哥带你玩飞牛NAS硬件03:五盘位+N5105+双网口的成品NAS值得入手吗

星哥带你玩飞牛NAS硬件03:五盘位+N5105+双网口的成品NAS值得入手吗

星哥带你玩飞牛 NAS 硬件 03:五盘位 +N5105+ 双网口的成品 NAS 值得入手吗 前言 大家好,我...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
星哥带你玩飞牛NAS-16:不再错过公众号更新,飞牛NAS搭建RSS

星哥带你玩飞牛NAS-16:不再错过公众号更新,飞牛NAS搭建RSS

  星哥带你玩飞牛 NAS-16:不再错过公众号更新,飞牛 NAS 搭建 RSS 对于经常关注多个微...
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍

浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍

浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍 前言 在 AI 自动化快速发展的当下,浏览器早已不再只是...
仅2MB大小!开源硬件监控工具:Win11 无缝适配,CPU、GPU、网速全维度掌控

仅2MB大小!开源硬件监控工具:Win11 无缝适配,CPU、GPU、网速全维度掌控

还在忍受动辄数百兆的“全家桶”监控软件?后台偷占资源、界面杂乱冗余,想查个 CPU 温度都要层层点选? 今天给...
自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个 AI 智能体 — 跟创业大佬对话 前言 智能体(Agent)已经成为创业者和技术人绕...