nginx反向代理替换html页面的字符及内容
1修改nginx.conf配置文件
server {
listen 8080;
server_name nc-csc.cm-worklink.com; #访问的域名
location / {
proxy_pass http://10.0.2.20/; #代理到目标地址
# 指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值
proxy_redirect http://10.0.2.20/ https://nc-test.com/; #重定向到另一个url地址
proxy_set_header Accept-Encoding ""; 替换gzip需要加上
proxy_set_header X-Forwarded-Proto https;
sub_filter 'http://10.0.2.20:80/' 'https://nc-test.com/'; #替换html页面中的字符串
sub_filter '10.0.2.20' 'nc-test.com'; # 替换 '旧字符' '新字符'
sub_filter_types *; #指定替换类型,默认包括text/html 对所有请求响应类型都替换
sub_filter_once off; #替换所有的(全局),默认是on,替换第一个
}
}
2.参数说明
1)proxy_redirect
语法:proxy_redirect [ default|off|redirect replacement ]
默认值:proxy_redirect default
使用字段:http, server, location
如果需要修改从被代理服务器传来的应答头中的"Location"和"Refresh"字段,可以用这个指令设置。
假设被代理服务器返回Location字段为: http://localhost:8000/two/some/uri/
这个指令:
proxy_redirect http://localhost:8000/two/ http://frontend/one/;
将Location字段重写为http://frontend/one/some/uri/。
在代替的字段中可以不写服务器名:
proxy_redirect http://localhost:8000/two/ /;
这样就使用服务器的基本名称和端口,即使它来自非80端口。
将被代理服务器发出的重定向http协议的location改为https协议:proxy_redirect ~^http://([^:]+)(:\d+)?(.*)$ https://$1$2$3;
参考链接:https://www.cnblogs.com/kevingrace/p/8073646.html
https://blog.csdn.net/u010391029/article/details/50395680