nginx:HTTP过滤模块的调用流程
在content阶段时,会生成返回给用户的响应内容,对用户的响应内容,实际上还要做加工处理,HTTP过滤模块就是对响应内容做加工处理的。
就是在content阶段之后,log阶段之前去介入请求处理
gzip_filter 必须要在 copy_filter模块之后。
gzip_filter需要对内存中的内容做压缩
copy_filter把文件中的内容拷贝一份到内存中,供gzip进行压缩
一.sub模块(替换响应中的字符串)
功能:将响应中指定的字符串,替换成新的字符串
ngx_http_sub_filter_module模块:默认未编译金nginx,通过--with-http_sub_module启用
1.sub_filter指令
语法:sub_filter string replacement;
默认:空
放置位置:http,server,location
2.sub_filter_last_modified指令
语法:sub_filter_last_modified on | off;
默认:sub_filter_last_modified off;
放置位置:http,server,location
3.sub_filter_once指令(只替换一次)
语法:sub_filter_once on | off;
默认:sub_filter_once on;
放置位置:http,server,location
4.sub_filter_types指令(针对什么类型的文件进行替换)
语法:sub_filter_types mime-type ...;
默认:sub_filter_types text/html;
放置位置:http,server,location
二.addition模块
功能:在响应前或者响应后增加内容,而增加内容的方式是通过新增子请求的响应完成。
模块:ngx_http_addition_filter_module。默认未编译进nginx,通过--with-http_addition_module启用。
1.add_before_body指令(在body之前添加一些内容,让nginx访问uri,根据子请求的内容,增加到body之前)
语法:add_before_body uri;
默认:空
放置位置:http,server,location
2.add_after_body 指令
语法:add_after_body uri;
默认:空
放置位置:http,server,location
3.addition_types指令 (指定特殊类型才能使用此指令)
语法:addition_types mime-type ...;
默认:addition_types text/html;
放置位置:http,server,location