找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 14168|回复: 0

nginx反代加替换教程

[复制链接]
发表于 2012-11-13 22:25:40 | 显示全部楼层 |阅读模式
1、安装Nginx$ n1 b9 @. y/ L5 S/ F* n4 ^6 ~
[AppleScript] 纯文本查看 复制代码
yum -y install gcc g++ vim libncurses5-dev make libxml2-dev
yum -y install subversion
yum -y install libpcre3 libpcre3-dev libcurl4-openssl-dev
yum -y install pcre* zlib* openssl*
wget -c  http://nginx.org/download/nginx-1.0.5.tar.gz
tar -zxf nginx-1.0.5.tar.gz
wget -c http://wiki.nginx.org/images/5/51/Nginx-accesskey-2.0.3.tar.gz
tar -zxf Nginx-accesskey-2.0.3.tar.gz
svn checkout http://substitutions4nginx.googlecode.com/svn/trunk/ substitutions4nginx-read-only
curdir=$(pwd)
cd nginx-1.0.5
./configure --user=nobody --group=nobody  --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --pid-path=/var/run/nginx.pid  --conf-path=/etc/nginx/nginx.conf   --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-pcre --with-http_sub_module --add-module=$curdir/substitutions4nginx-read-only --add-module=$curdir/nginx-accesskey-2.0.3
make
make install
/ `5 k  b7 j# V- f/ b6 Q

) e; }2 d) k- A* I7 A8 [: a谷歌代码即将关闭,新增以下自己对照修改上面代码:6 H& x# O6 A+ K1 o( w! F
[AppleScript] 纯文本查看 复制代码
killall -9 nginx
/etc/init.d/nginx stop #停止nginx

cd /root
yum update
yum install -y git gcc g++ make automake #安装依赖包
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
& n  U  Q1 D1 x, g7 x: ~

7 P7 X- [+ m/ X0 w1 n- `0 n; c9 R# W2、配置nginx.conf
  X  L: S. h6 X7 A
编辑/etc/nginx/nginx.conf0 b: ?5 W/ z0 {% f  P% }
[AppleScript] 纯文本查看 复制代码
server{
listen 80;
server_name t.playvps.com;  #绑定的域名
access_log off;                #off 关闭日志
location / {
subs_filter 'www.baidu.com' 't.playvps.com' gi;  #substitutions4nginx替换 (使用方法参照官方)
proxy_set_header   X-Real-IP  $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header   Referer http://www.baidu.com;        #强制定义Referer,程序验证判断会用到
proxy_set_header   Host www.baidu.com;                  #定义主机头,如果目标站点绑定的域名个server_name项的吻合则使用$host
proxy_pass http://www.baidu.com;                        #指定目标,建议使用IP或者nginx自定义池
proxy_set_header Accept-Encoding "";                        #清除编码
        }
}

* z& @  a% ~( u: n7 P3 U7 Q" J" o3.重启Nginx
; G: C9 w2 }! T  p
[AppleScript] 纯文本查看 复制代码
pkill nginx #关闭进程
nginx #启动Nginx

4 u+ n2 g/ T' F' b% g0 R# H* r
- F7 r3 ^! m" R% ]: P3 H以下为一些配置例子作为参考
+ s& I% e0 N& z8 q4 Y4 Z9 p如果替换中文要注意网站配置文件的编码如果是utf8则汉字utf8转二进制表现形式(文后解释)
7 [  \3 `3 A4 e% J! Y4 o9 |
[AppleScript] 纯文本查看 复制代码
server{
 
listen 80;
 
server_name a.a; 
access_log off;                
location / {
 
proxy_cache cache_one;
 
proxy_cache_valid  200 304 1s; 
proxy_cache_key $host$uri$is_args$args;
 
subs_filter www.hostloc.com j.baidu.wf;
 
subs_filter static/image/common/logo.png /img/xxx.jpg;
 
subs_filter 全球主机交流论坛 全球MJJ交流论坛
 
subs_filter '美国' '米国' ;
 
proxy_set_header   X-Real-IP  $remote_addr;
 
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 
proxy_set_header   Referer http://www.hostloc.com;        
proxy_set_header   Host www.hostloc.com;                  
proxy_pass http://www.hostloc.com;                        
proxy_set_header Accept-Encoding "";                
expires 5h; 
        }
 

}
+ U. N* h, }  ^$ ]
8 r5 p5 _8 |6 I" r5 S: C7 W  @
中文还是弄不明白,看这:8 h$ C4 F7 C1 M' r$ R" _
中文的特殊性中文匹配替换可以在本模块中进行,但是需要知道所匹配中文的编码方式及其最终的二进制表示形式,而且只能采用正则匹配替换的方法。
" C& u/ _8 E. y' {比如:
7 t/ \  f6 |+ I+ v- p“你好”的unicode表示为:  0x4f60 0x597d4 ~6 y9 M+ q$ i8 y2 F" s. C- I$ w
  * m& i6 y3 x& d+ j7 h. |
而其utf-8的表示为:  0xe4bda0 0xe5a5bd: l% g/ i  e$ S$ V' [, [3 b
  
( G2 K1 D4 T2 N: H( @1 l% N查看二进制中文的表示方法方法1如果是Unicode或者GB的有其固定对应的二进制表示形式,你可以去查表。utf-8你可以自己转换,但是比较麻烦。转换的方法看这里(http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html)
! o; a- m+ h6 Z7 y方法2把对应编码的网页下载下来,然后用二进制形式查看其内容,可以看到对应汉字的二进制表示(vim中可以输入命令':%!xxd')。
) F- j' e# l( y6 G' W0 z例子把‘你好’替换成‘aaabbb’  subs_filter '\xe4\xbd\xa0\xe5\xa5\xbd' 'aaabbb' r;
! G% y* n; P( R" Y6 \; c% M9 x- h. B 5 x2 ]+ m2 b  E5 G
在线转换这里有:http://www.araaa.com/zhanzhang/7 ?7 }2 z# E# f/ j9 g
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-10-5 02:51 , Processed in 0.056390 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表