本文已停止更新,反代系列教程汇总到 http://jybb.me/nginx-proxy-pass

请人工302过去查看= =

 

本文适合全新安装,也适合安装了Lnmp.org一键包的安装

 

1、下载substitutions4nginx模块,这个模块用于替换。

pkill nginx
/etc/init.d/nginx stop #停止nginx

cd /root
apt-get update
apt-get install -y git gcc g++ make automake
#安装依赖包,Centos将apt-get更改为yum
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

2、重新编译nginx

wget http://nginx.org/download/nginx-1.2.6.tar.gz
tar zxvf nginx-1.2.6.tar.gz
cd nginx-1.2.6
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=/root/ngx_http_substitutions_filter_module
make
make install

4、新建一个虚拟主机用于反代

vi /usr/local/nginx/conf/vhost/example.com.conf  #example.com是你要绑定的域名,当然你也可以用其他名字.conf

输入以下内容:
server{
listen 80;
server_name example.com;    #绑定的域名
index index.php;      #默认首页
access_log off;		#off 关闭日志
location / {
subs_filter www.hostloc.com example.com;  #替换掉域名
subs_filter static/image/common/logo.png http://xxx/1.jpg; #替换掉LOGO
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;	#强制定义Referer,程序验证判断会用到
proxy_set_header   Host www.hostloc.com;  		#定义主机头,如果目标站点绑定的域名个server_name项的吻合则使用$host
proxy_pass http://www.hostloc.com;			#指定目标,建议使用IP或者nginx自定义池
proxy_set_header Accept-Encoding "";			#清除编码
        }
}

缓存移步这篇文章

 

如果要替换中文,例如

subs_filter '美国' '米国';

要先把conf文件转成utf-8模式。具体操作:用科学编辑器打开conf文件,另存为utf-8-without-BOM编码,再上传即可。

关于中文替换,看这篇文章。。。http://jybb.me/nginx-subsfilter-utf-8

5、重启LNMP

/etc/init.d/nginx restart

如无意外,此时访问你的域名就会看到成功反代并替换了。

 

 

Update 2012-12-30

1、更改nginx地址

2、新增依赖库安装

Update 2012-12-15

1、更换subsititutions模块地址为github

2、合并For Lnmp0.9与全新安装教程

   

已有 54 條評論

  1. しら 10 年前 (2015-12-05)
    @

    这主题好眼熟

    • JyLee 10 年前 (2015-12-05)
      @

      2333

  2. tmdab123 13 年前 (2013-08-20)
    @

    我碰到一个很郁闷的问题,反向代理后,发现页面像是静态文件没加载的样子,怎么处理呢

    • 飛天鼠 13 年前 (2013-08-24)
      @

      帖下你的配置看看?

    • tmdab123 13 年前 (2013-08-25)
      @

      不在办公室,回头贴给你看看。谢谢解答了。太感谢了/

  3. Iccen 13 年前 (2013-08-06)
    @

    你好,按你的方法下载安装了substitutions4nginx模块,安装也没提示什么错误.为什么只能存在一条subs_filter语句,多了运行nginx时就提示有重复.何解?

    • Iccen 13 年前 (2013-08-07)
      @

      自己找到错误了,原来我一直用的是sub_filter 而不是subs_fiter少了一个s,难怪一直不行

  4. ahao358 13 年前 (2013-03-02)
    @

    在LNMP论坛里看到军哥是用:svn checkout http://substitutions4nginx.googlecode.com/svn/trunk/ substitutions4nginx-read-only请问和博主的:git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module这两个模块有什么区别呢?谢谢!

    • 飛天鼠 13 年前 (2013-03-02)
      @

      作者抛弃了googlecode,转向github

    • 飛天鼠 13 年前 (2013-03-02)
      @

      作者抛弃了googlecode,转向github

  5. eagle 14 年前 (2012-11-06)
    @

    配置nginx.conf 编辑/etc/nginx/nginx.conf加在最前面了 启动就报这玩样~~ngi nx: [emerg] unknown directive “server” in /etc/nginx/nginx.conf:1

    • 飛天鼠 14 年前 (2012-11-09)
      @

      。。。不能加在最前面

    • 飛天鼠 14 年前 (2012-11-09)
      @

      要在http段下面最好在倒数第二行开始加

  6. eagle 14 年前 (2012-11-02)
    @

    全新编译nginx 这个怎么实现 添加多个反代呢

    • 飛天鼠 14 年前 (2012-11-02)
      @

      添加多个server段

      • eagle 14 年前 (2012-11-02)
        @

        呵呵我的意思是 命令是什么 [嘻嘻]

        • 飛天鼠 14 年前 (2012-11-02)
          @

          – -直接编辑conf就是啦

  7. 野草 14 年前 (2012-10-27)
    @

    proxy_pass这里我想写IP是不是这样proxy_pass 202.39.132.242。比如xxx.com的虚拟主机的IP是127.0.0.1,我想把xxx.com解析到VPS,然后vps再访问虚拟主机IP,再返回数据给访客,这个功能怎么实现?我试着用这里面的不行,反代理功能可以,谢谢!

  8. hzqim 14 年前 (2012-10-06)
    @

    只用发代,没有玩过替换,所以没有那么复杂。http://www.hzq.im/1268

  9. ourvps 14 年前 (2012-10-01)
    @

    apt-get install subversion subversion-tools

  10. 夜央 14 年前 (2012-08-21)
    @

    此文马大克一下,正在想玩这种东西。。

  11. 夜央 14 年前 (2012-08-21)
    @

    此文马克一下。正在想玩这种东西。。。

  12. Zeraba 14 年前 (2012-08-13)
    @

    那个风骚的来自xxx 咋整了

    • JyLee 14 年前 (2012-08-14)
      @

      – -那个我没强制加进去。。。。

    • JyLee 14 年前 (2012-08-14)
      @

      ==,你说来自哪里的吧?那个是看你用什么登陆的- –

  13. 小人物 14 年前 (2012-07-31)
    @

    好吧我服了你了。。。 月小姐 来舔~

    • JyLee 14 年前 (2012-07-31)
      @

      — —尼玛。。。。。。。。。

      还有我不记得编译那里对不对了。。。你当小白鼠试验下

  14. 门门 14 年前 (2012-07-31)
    @

    支持 支持

  15. 敏感词 14 年前 (2012-07-28)
    @

    膜拜 球带

  16. 手印 14 年前 (2012-07-27)
    @

    好牛逼的样子