文章目录
1、
ngx_http_sub_module
模块可以通过字符串替换的方式修改响应体内容。默认未安装,需要在configure
阶段指定--with-http_sub_module
参数。2、
sub_filter
指令用于替换响应内容,语法格式为sub_filter string replacement
,string
表示被替换内容,replacement
为替换后的内容。可配置在http
, server
, location
块中。3、
sub_filter_once
指令用于设置查找到的字符串仅替换一次还是全部替换。语法为sub_filter_once on | off
,默认值on
表示仅替换一次,off
表示全部替换。4、
sub_filter_last_modified
指令用于控制在使用sub_filter
修改响应体内容后原始响应头中Last-Modified
字段是否保留。默认值off
表示不保留,on
表示保留。语法为sub_filter_last_modified on | off
。5、测试
- 将
Thank you
改为Thanks
server {
listen 80;
server_name localhost;
location / {
root html;
sub_filter 'Thank you' 'Thanks';
}
}
- 将
nginx
全部替换为NGINX
server {
listen 80;
server_name localhost;
location / {
root html;
sub_filter 'nginx' 'NGINX';
sub_filter_once off;
}
}
- 同时修改多项内容
server {
listen 80;
server_name localhost;
location / {
root html;
sub_filter 'nginx''NGINX';
sub_filter '<head>''<head><meta charset="UTF-8">';
sub_filter 'server''服务器';
sub_filter 'Welcome to''欢迎来到';
sub_filter_once off;
}
}
- 修改代理返回的内容
server {
listen 80;
server_name localhost;
location / {
root html;
sub_filter 'nginx''NGINX';
sub_filter '<head>''<head><meta charset="UTF-8">';
sub_filter 'server''服务器';
sub_filter 'Welcome to''欢迎来到';
sub_filter_once off;
}
location /api/ {
proxy_pass http://192.168.1.16:8086/;
sub_filter '-''/';
sub_filter_once off;
}
}