nginx日志模块与HTTP过滤模块与sub模块修改返回内容
日志格式使用指令
1 2 3 4 5 6 7 8 9 | 指令介绍 Syntax: log_format name [escape = default|json|none] string ...; Default: log_format combined "..." ; Context: http 默认的日志文件 log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' ' "$http_referer" "$http_user_agent" '; |
日志文件路径定义
1 2 3 4 5 6 7 8 9 10 | Syntax: access_log path [ format [ buffer = size] [gzip[ = level]] [flush = time] [ if = condition]]; access_log off; Default: access_log logs / access.log combined; Context: http, server, location, if in location, limit_except 路径可包含变量,不打开日志缓存时,每记录一条日志都需要打开关闭日志文件 if 通过变量值来判断日志是否记录 日志缓存: 功能:批量将内存日志写入文件中 写入磁盘的条件:所有待写入磁盘日志文件大小超出缓存大写;达到flush指定的过期时间;worker进程执行reopen命令,或正在关闭 日志压缩:批量压缩内存中日志,再写入磁盘。 buffer 默认大小为 64kb ;压缩级别默认为 1 ( 1 最快压缩率最低, 9 最快压缩来较高) |
对日志文件包含的变量名优化
指令
1 2 3 4 5 6 7 8 9 10 11 | Syntax: open_log_file_cache max = N [inactive = time] [min_uses = N] [valid = time]; open_log_file_cache off; Default: open_log_file_cache off; Context: http, server, location max :缓存内的最大文件句柄数;超出后用LRU算法淘汰 inactive:文件访问完在这段时间不会被关闭默认 10 秒 min_uses:在inactive时间内使用的次数超过min_uses才会继续保存在内存,默认 1 valid:超出这个时间后,会检查缓存日志文件是否存在,默认 60 秒 off:关闭缓存功能 |
过滤模块工作位置
默认sub_module未编译进nginx
启用此模块
1 2 3 4 | [root@python nginx - 1.15 . 9 ] # ./configure --prefix=/data/web --sbin-path=/usr/bin --user=nginx --group=nginx --with-http_stub_status_module --with-http_auth_request_module --with-http_sub_module --add-module=/root/nginx-http-concat [root@python nginx - 1.15 . 9 ] # make [root@python nginx - 1.15 . 9 ] # mv /usr/bin/nginx{,.07.12.13.18} [root@python nginx - 1.15 . 9 ] # cp objs/nginx /usr/bin/ |
更改响应中的字符串
sub模块指令
1 2 3 4 5 6 7 8 9 10 11 12 | Syntax: sub_filter string replacement; #把匹配上的返回给用户响应中的字符串替换replacement指定的字符串 Default: — Context: http, server, location Syntax: sub_filter_last_modified on | off; # 是否还返回给用户未修改的内容呢;默认不会再返回了 Default: sub_filter_last_modified off; Context: http, server, location Syntax: sub_filter_once on | off; # 是否只替换一次,如果关闭会返回所有的响应报文的boyd内容都做替换 Default: sub_filter_once on; Context: http, server, location Syntax: sub_filter_types mime - type ...; # 只对什么类型文件的响应做替换 Default: sub_filter_types text / html; Context: http, server, location |
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | server { server_name www.chenxi.com sub.com; access_log logs / sub.log main; root html; index index.html index.htm; location / { #sub_filter 'Nginx.oRg' '$host/nginx'; #sub_filter 'nginX.cOm' '$host/nginx'; #sub_filter_once on; #sub_filter_once off; #sub_filter_last_modified off; #sub_filter_last_modified on; } } |
在浏览器测试
修改配置打开替换的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@python vhast] # cat sub.conf server { server_name www.chenxi.com sub.com; access_log logs / sub.log main; root html; index index.html index.htm; location / { sub_filter 'Nginx.oRg' '$host/nginx' ; sub_filter 'nginX.cOm' '$host/nginx' ; #sub_filter_once on; #sub_filter_once off; sub_filter_last_modified off; #只替换一次 #sub_filter_last_modified on; } } |
测试
修改配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | server { server_name www.chenxi.com sub.com; access_log logs / sub.log main; root html; index index.html index.htm; location / { sub_filter 'Nginx.oRg' '$host/nginx' ; sub_filter 'nginX.cOm' '$host/nginx' ; #sub_filter_once on; sub_filter_once off; #sub_filter_last_modified off; sub_filter_last_modified on; } } |
测试
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏