利用Nginx sub模块对网页内容进行替换 不指定

jed , 2021-5-20 08:22 , 服务器技术 , 评论(0) , 阅读(1571) , Via 本站原创 | |
在我们环境下,经常需要运营对网站进行测试,但是偶尔会出现不知道是在测试环境还是正式环境,因为测试环境都是通过Nginx反向代理进行访问.因此本文利用Nginx进行网页内容替换,然后在醒目位置进行提醒,以达到对测试环境的标识作用

yum -y install openssl openssl-devel

在编译nginx的时候添加--with-http_sub_module以加载sub模块
nginx官网下载安装包:http://nginx.org/en/download.html

wget http://nginx.org/download/nginx-1.11.5.tar.gz
tar -zxvf nginx-1.11.5.tar.gz
cd nginx-1.11.5
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module   --without-http_rewrite_module
make
make install


如果全是测试环境的反向代理就只需要在http里头添加,当然也可以针对单个站点

sub_filter <body '<div style="border: red 1px solid;width: 100px;text-align: center;line-height: 30px;height: 30px;font-size: 14px;z-index: 999999999;position: fixed;top: 80px;font-weight: bold;background-color: yellow;left: 10px;">测试服务器</div><body';
sub_filter_once off;
sub_filter_types application/json application/x-javascript text/plain;


我试图反向代理我的网站并修改内容.
为此,我使用sub_filter编译了Nginx.
它现在接受sub_filter指令,但它不能以某种方式工作.

server {
    listen       8080;
    server_name  www.xxx.com;
    access_log  /var/log/Nginx/www.goparts.access.log  main;
    error_log  /var/log/Nginx/www.goparts.error.log;
    root   /usr/share/Nginx/html;
    index  index.html index.htm;
    ## send request back to apache1 ##
    location / {
       sub_filter 502 http_503 http_504;
     proxy_redirect off;
     proxy_buffering off;
     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

请帮我

最佳答案
检查上游源是否已打开gzip,如果需要,请执行此操作

proxy_set_header Accept-Encoding "";

所以整个事情都会像

location / {
    proxy_set_header Accept-Encoding "";
    proxy_pass http://upstream.site/;
    sub_filter_types text/css;
    sub_filter_once off;
    sub_filter .upstream.site special.our.domain;
}

检查这些链接

============================

二、常用指令:

2.1 sub_filter指令: sub_filter string(原字符串) replacement(用于替换的字符串);

用于设置需要使用说明字符串替换说明字符串.string是要被替换的字符串,replacement是 新的字符串,它里面可以带变量。

2.2 sub_filter_last_modified指令: sub_filter_last_modified on | off;

用于设置网页内替换后是否修改 可在nginx.conf的 http, server, location三个位置配置使 用,默认值是off;

2.3 sub_filter_once指令:sub_filter_once on | off;

用于设置字符串替换次数,默认只替换一次。如果是on,默认只替换第一次匹配到的到字 符,如果是off,那么所有匹配到的字符都会被替换;

2.4 sub_filter_types指令:sub_filter_types *

用于指定需要被替换的MIME类型,默认为“text/html”,如果制定为*,那么所有的;

说明:以上指令可在nginx.conf的http, server, location三个位置配置使用;

三、反向代理动态替换网页内容实例参考:

upstream www.iptest.cn {

server 118.184.180.46:80;

}

#反向代理,如果有多个服务端ip,还可以添加负载均衡方式,权重等策略#

server {

listen 80;

#监听端口#

server_name www.iptest.cn;

#设置server name#

charset utf-8;

#设置字符编码为utf-8,可根据实际情况调整#

location / {

proxy_pass http://www.iptest.cn$request_uri;

#反向代理规则#

proxy_set_header Accept-Encoding deflate;

#设置反向代理头部,有时候源站响应的是gzip格式,替换的时候会有问题,可通过此项解决#

subs_filter '183.251.160.127' '123.181.128.17';

#替换指定ip,此处将183.251.160.127替换成123.181.128.17#

subs_filter '福建省龙岩市 移动' '河北省唐山市 电信';

#替换城市、运营商信息,nginx此替换模块支持中文替换#

subs_filter_types text/html;

#指定被替换的MIME类型#

sub_filter_once on;

#指定字符串替换次数,on表示只替换第一次匹配到的字符,off表示替换所有匹配到的字符#

}

}

说明:

1、此模块替换不区分大小写;

2、支持中文替换;

Nginx.conf 完整配置:
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]