nginx日志模块与HTTP过滤模块与sub模块修改返回内容

日志格式使用指令

指令介绍
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"';

  日志文件路径定义

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最快压缩来较高)

  对日志文件包含的变量名优化

指令

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

启用此模块

[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模块指令
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

  配置

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

  在浏览器测试

 

 修改配置打开替换的内容

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

  测试

 

 

 

 修改配置

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

  测试

 

posted @ 2019-07-12 15:14  烟雨楼台,行云流水  阅读(812)  评论(0编辑  收藏  举报