文章目录


1、ngx_http_sub_module模块可以通过字符串替换的方式修改响应体内容。默认未安装,需要在configure阶段指定--with-http_sub_module参数。
2、sub_filter指令用于替换响应内容,语法格式为sub_filter string replacementstring表示被替换内容,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;
 }
}

在这里插入图片描述

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐