<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[利用Nginx sub模块对网页内容进行替换]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Thu, 20 May 2021 00:22:01 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	在我们环境下,经常需要运营对网站进行测试,但是偶尔会出现不知道是在测试环境还是正式环境,因为测试环境都是通过Nginx反向代理进行访问.因此本文利用Nginx进行网页内容替换,然后在醒目位置进行提醒,以达到对测试环境的标识作用<br/><div class="code"><br/>yum -y install openssl openssl-devel<br/></div><br/>在编译nginx的时候添加--with-http_sub_module以加载sub模块<br/>nginx官网下载安装包：<a href="http://nginx.org/en/download.html" target="_blank">http://nginx.org/en/download.html</a><br/><div class="code"><br/>wget http://nginx.org/download/nginx-1.11.5.tar.gz<br/>tar -zxvf nginx-1.11.5.tar.gz<br/>cd nginx-1.11.5<br/>./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module&nbsp;&nbsp; --without-http_rewrite_module<br/>make<br/>make install<br/></div><br/><br/>如果全是测试环境的反向代理就只需要在http里头添加,当然也可以针对单个站点<br/><div class="code"><br/>sub_filter &lt;body &#039;&lt;div style=&quot;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;&quot;&gt;测试服务器&lt;/div&gt;&lt;body&#039;;<br/>sub_filter_once off;<br/>sub_filter_types application/json application/x-javascript text/plain;<br/></div><br/><br/>我试图反向代理我的网站并修改内容.<br/>为此,我使用sub_filter编译了Nginx.<br/>它现在接受sub_filter指令,但它不能以某种方式工作.<br/><div class="code"><br/>server &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8080;<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;&nbsp;www.xxx.com;<br/>&nbsp;&nbsp;&nbsp;&nbsp;access_log&nbsp;&nbsp;/var/log/Nginx/www.goparts.access.log&nbsp;&nbsp;main;<br/>&nbsp;&nbsp;&nbsp;&nbsp;error_log&nbsp;&nbsp;/var/log/Nginx/www.goparts.error.log;<br/>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp; /usr/share/Nginx/html;<br/>&nbsp;&nbsp;&nbsp;&nbsp;index&nbsp;&nbsp;index.html index.htm;<br/>&nbsp;&nbsp;&nbsp;&nbsp;## send request back to apache1 ##<br/>&nbsp;&nbsp;&nbsp;&nbsp;location / &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub_filter 502 http_503 http_504;<br/>&nbsp;&nbsp;&nbsp;&nbsp; proxy_redirect off;<br/>&nbsp;&nbsp;&nbsp;&nbsp; proxy_buffering off;<br/>&nbsp;&nbsp;&nbsp;&nbsp; proxy_set_header&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Host&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$host;<br/>&nbsp;&nbsp;&nbsp;&nbsp; proxy_set_header&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X-Real-IP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $remote_addr;<br/>&nbsp;&nbsp;&nbsp;&nbsp; proxy_set_header&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X-Forwarded-For $proxy_add_x_forwarded_for;<br/>&nbsp;&nbsp; &#125;<br/>&#125;<br/></div><br/>请帮我<br/><br/>最佳答案<br/>检查上游源是否已打开gzip,如果需要,请执行此操作<br/><div class="code"><br/>proxy_set_header Accept-Encoding &quot;&quot;;<br/></div><br/>所以整个事情都会像<br/><div class="code"><br/>location / &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Accept-Encoding &quot;&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass http://upstream.site/;<br/>&nbsp;&nbsp;&nbsp;&nbsp;sub_filter_types text/css;<br/>&nbsp;&nbsp;&nbsp;&nbsp;sub_filter_once off;<br/>&nbsp;&nbsp;&nbsp;&nbsp;sub_filter .upstream.site special.our.domain;<br/>&#125;<br/></div><br/>检查这些链接<br/><br/>============================<br/><br/>二、常用指令：<br/><br/>2.1 sub_filter指令： sub_filter string（原字符串） replacement（用于替换的字符串）;<br/><br/>用于设置需要使用说明字符串替换说明字符串.string是要被替换的字符串，replacement是 新的字符串，它里面可以带变量。<br/><br/>2.2 sub_filter_last_modified指令： sub_filter_last_modified on &#124; off;<br/><br/>用于设置网页内替换后是否修改 可在nginx.conf的 http, server, location三个位置配置使 用，默认值是off；<br/><br/>2.3 sub_filter_once指令：sub_filter_once on &#124; off;<br/><br/>用于设置字符串替换次数，默认只替换一次。如果是on，默认只替换第一次匹配到的到字 符，如果是off，那么所有匹配到的字符都会被替换；<br/><br/>2.4 sub_filter_types指令：sub_filter_types *<br/><br/>用于指定需要被替换的MIME类型,默认为“text/html”，如果制定为*，那么所有的；<br/><br/>说明：以上指令可在nginx.conf的http, server, location三个位置配置使用；<br/><br/>三、反向代理动态替换网页内容实例参考：<br/><br/>upstream <a href="http://www.iptest.cn" target="_blank">www.iptest.cn</a> &#123;<br/><br/>server 118.184.180.46:80;<br/><br/>&#125;<br/><br/>#反向代理，如果有多个服务端ip，还可以添加负载均衡方式，权重等策略#<br/><br/>server &#123;<br/><br/>listen 80;<br/><br/>#监听端口#<br/><br/>server_name <a href="http://www.iptest.cn;" target="_blank">www.iptest.cn;</a><br/><br/>#设置server name#<br/><br/>charset utf-8;<br/><br/>#设置字符编码为utf-8,可根据实际情况调整#<br/><br/>location / &#123;<br/><br/>proxy_pass <a href="http://www.iptest.cn$request_uri;" target="_blank">http://www.iptest.cn$request_uri;</a><br/><br/>#反向代理规则#<br/><br/>proxy_set_header Accept-Encoding deflate;<br/><br/>#设置反向代理头部，有时候源站响应的是gzip格式，替换的时候会有问题，可通过此项解决#<br/><br/>subs_filter '183.251.160.127' '123.181.128.17';<br/><br/>#替换指定ip，此处将183.251.160.127替换成123.181.128.17#<br/><br/>subs_filter '福建省龙岩市 移动' '河北省唐山市 电信';<br/><br/>#替换城市、运营商信息，nginx此替换模块支持中文替换#<br/><br/>subs_filter_types text/html;<br/><br/>#指定被替换的MIME类型#<br/><br/>sub_filter_once on;<br/><br/>#指定字符串替换次数，on表示只替换第一次匹配到的字符，off表示替换所有匹配到的字符#<br/><br/>&#125;<br/><br/>&#125;<br/><br/>说明：<br/><br/>1、此模块替换不区分大小写；<br/><br/>2、支持中文替换；<br/><br/>Nginx.conf 完整配置：
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] 利用Nginx sub模块对网页内容进行替换]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>