ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串。这个模块已经内置在nginx中,但是默认未安装,需要安装需要加上配置参数:–with-http_sub_module 如果已经安装nginx,只需要再添加这个模块就可以了。
sub_filter string(原字符串) replacement(用于替换的字符串);
用于设置需要使用说明字符串替换说明字符串.string是要被替换的字符串,replacement是 新的字符串,它里面可以带变量。
sub_filter_last_modified on | off;
用于设置网页内替换后是否修改 可在nginx.conf的 http, server, location三个位置配置使 用,默认值是off;
sub_filter_once on | off;
用于设置字符串替换次数,默认只替换一次。如果是on,默认只替换第一次匹配到的到字 符,如果是off,那么所有匹配到的字符都会被替换;
sub_filter_types *
用于指定需要被替换的MIME类型,默认为“text/html”,如果制定为*,那么所有的;
参考:https://blog.csdn.net/sayyy/article/details/121178813
使用yum安装的nginx,已启用 ngx_http_sub_module。
location / {
proxy_pass http://target_domain;
sub_filter 'm.xxx.cn' 'mydomain';
sub_filter 'img.xxx.cn' 'mydomain';
sub_filter_once off;
sub_filter_types *;
}
无法绕开gzip时,参考这里:https://www.jianshu.com/p/6ba9a78b69b1
第1次代理:可根据替换内容进行扩展(图片保持压缩)
location ~* ^/proxy/input/xxx1/(.*\.(jpg|png).*) {
proxy_pass http://mydomain/proxy/out/$scheme/$1$is_args$args;
proxy_set_header referer "";
add_header Custom_proxy_input_type "image";
add_header Custom_proxy_input_url "${scheme}://${host}:${server_port}${request_uri}";
add_header Custom_proxy_input_proxy_pass "http://mydomain/proxy/out/$scheme/$1$is_args$args";
}
location ~* ^/proxy/input/xxx1/(.*) {
proxy_pass http://mydomain/proxy/strip/proxy/out/$scheme/$1$is_args$args;
proxy_set_header referer "";
add_header Custom_proxy_input_type "text";
add_header Custom_proxy_input_url "${scheme}://${host}:${server_port}${request_uri}";
add_header Custom_proxy_input_proxy_pass "http://mydomain/proxy/strip/proxy/out/$scheme/$1$is_args$args";
sub_filter 'www.xxx1.com' 'mydomain/proxy/input/xxx1/www_xxx1_com';
sub_filter 'www-cdn.xxx1.com' 'mydomain/proxy/input/xxx1/www_xxx1_com';
sub_filter_once off;
sub_filter_types *;
}
location ~* ^/proxy/input/xxx2/(.*\.(jpg|png).*) {
proxy_pass http://mydomain/proxy/out/$scheme/$1$is_args$args;
proxy_set_header referer "";
add_header Custom_proxy_input_type "image";
add_header Custom_proxy_input_url "${scheme}://${host}:${server_port}${request_uri}";
add_header Custom_proxy_input_proxy_pass "http://mydomain/proxy/out/$scheme/$1$is_args$args";
}
location ~* ^/proxy/input/xxx2/(.*) {
proxy_pass http://mydomain/proxy/strip/proxy/out/$scheme/$1$is_args$args;
proxy_set_header referer "";
add_header Custom_proxy_input_type "text";
add_header Custom_proxy_input_url "${scheme}://${host}:${server_port}${request_uri}";
add_header Custom_proxy_input_proxy_pass "http://mydomain/proxy/strip/proxy/out/$scheme/$1$is_args$args";
sub_filter 'www.xxx2.com' 'mydomain/proxy/input/xxx2/www_xxx2_com';
sub_filter 'www-cdn.xxx2.com' 'mydomain/proxy/input/xxx2/www_xxx2_com';
}
第2次代理:脱掉gzip
location /proxy/strip/ {
proxy_pass http://mydomain/;
proxy_set_header referer "";
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "Upgrade";
#proxy_http_version 1.1;
proxy_set_header Accept-Encoding 'gzip';
gunzip on;
#add_header Cache-Control no-store;
#expires 0s;
add_header Access-Control-Allow-Origin *;
add_header Custom_proxy_strip_url "${scheme}://${host}:${server_port}${request_uri}";
add_header Custom_proxy_strip_proxy_pass "http://mydomain/...";
}
第3次代理:可扩充N多个域名
location /proxy/out/http/www_xxx1_cn/ {
proxy_pass http://www.xxx.cn/;
proxy_set_header referer "";
add_header Custom_proxy_out_url "${scheme}://${host}:${server_port}${request_uri}";
}
location /proxy/out/https/www_xxx1_cn/ {
proxy_pass https://www.xxx.cn/;
proxy_set_header referer "";
add_header Custom_proxy_out_url "${scheme}://${host}:${server_port}${request_uri}";
}
location /proxy/out/http/www_xxx2_cn/ {
proxy_pass http://www_xxx2.cn/;
proxy_set_header referer "";
add_header Custom_proxy_out_url "${scheme}://${host}:${server_port}${request_uri}";
}
location /proxy/out/https/www_xxx2_cn/ {
proxy_pass https://www_xxx2.cn/;
proxy_set_header referer "";
add_header Custom_proxy_out_url "${scheme}://${host}:${server_port}${request_uri}";
}
参考:https://www.zhihu.com/question/285467878
去掉gzip
proxy_set_header Accept-Encoding "";
替换的MIME类型,默认为“text/html”,添加css MIME类型。
sub_filter_types text/css;
subs_filter_types text/css text/plain application/x-javascript application/javascript;
https://nginx.org/en/docs/http/ngx_http_sub_module.html
https://www.cnblogs.com/kenwar/p/8288882.html
https://www.cnblogs.com/kenwar/p/8296508.html