nginx作为正向和反向代理服务器还能修改http响应体内容
				
									
					
					
						|  | 
							admin 2025年6月28日 22:22
								本文热度 1117 | 
					
				 
				- ngx_http_sub_module模块可以通过字符串替换的方式修改响应体内容。默认未安装,需要在configure阶段指定- --with-http_sub_module参数。
- sub_filter指令用于替换响应内容,语法格式为- sub_filter string replacement,string表示被替换内容,replacement为替换后的内容。可配置在- http, server, location块中。
- sub_filter_once指令用于设置查找到的字符串仅替换一次还是全部替换。语法为- sub_filter_once on | off,默认值on表示仅替换一次,off表示全部替换。
- sub_filter_last_modified指令用于控制在使用sub_filter修改响应体内容后原始响应头中Last-Modified字段是否保留。默认值off表示不保留,on表示保留。语法为- sub_filter_last_modified on | off。
- server {
 listen 80;
 server_name localhost;
 
 location / {
 root html;
 sub_filter 'Thank you' 'Thanks';
 }
 }
 
   
- 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;
 }
 }
 
阅读原文:原文链接
该文章在 2025/7/1 23:46:04 编辑过