发现以前写的几篇教程,挺模糊的,有人看了摸不清头脑。(由于当时我也不太懂……),现在重新写一遍,并增加注释。

如果你用的是军哥的LNMP,而且又需要替换内容,则需要重新编译nginx。如果不需要替换内存,仅用于加速自己的网站,则不需要重新编译。

1.安装Nginx

你可以用源安装/编译安装/一键包(此处省略)

2.新增虚拟主机配置文件用于反代

找到您的nginx conf所在位置,一般编译的在/usr/local/nginx/conf/,从源安装的在/etc/nginx

在nginx.conf的http层加入以下内容:

新增虚拟主机配置:

配置文件内容:{后端(ip为1.2.3.4)绑定域名example.com,前端绑定域名example.com,域名解析到前端,实现cdn加速。}

如无意外,重启nginx后把example.com绑定到前端就可以访问了

 

下面介绍的是反代别人的网站(类似于小偷),并替换相关内容

1.编译nginX:

如果您用的系统是Debian,就不需要编译了。

2.修改nginx.conf

编译的在/usr/local/nginx/conf/nginx.conf,源安装的在/etc/nginx/nginx.conf

以example.com反代www.baidu.com并替换内容为例:

温馨提示:如果您要替换的内容里面有中文,请将conf文件保存为utf-8 without BOM编码。

关于ngx_http_substitutions_filter_module的说明:(本模块是中国人写的,但是说明只有英文版,在此我翻译一下)

描述
nginx_substitutions_filter 是一个nginx替换模块,就跟apache的 mod_substitute模块一样

使用距离

location / {

subs_filter_types text/html text/css text/xml;
subs_filter st(\d*).example.com $1.example.com ir;
subs_filter a.example.com s.example.com;

}

涉及指令:
* subs_filter_types

* subs_filter

subs_filter_types
语法: *subs_filter_types mime-type [mime-types] *

默认: *subs_filter_types text/html*

内容: *http, server, location*

*subs_filter_types* 是用来指定替换文件类型的
默认仅仅替换text/html类型的文件。

*如果您反代的论坛出现登录跳转源站之类的问题,请检查这个项目。

 

proxy_set_header Accept-Encoding "";

subs_filter
语法: *subs_filter source_str destination_str [gior] *

默认: *none*

内容: *http, server, location*

*subs_filter* 是用来替换文本的,可以使用正则

* *g*(默认):替换匹配项。

* *i*:区分大小写的匹配

* *o*: 只匹配发现的第一个。

* *r*: 正则。

 

先讲这么多,有错误欢迎提出。

关于自动清空缓存的,我也没弄明白,先不写了。

 

*参考资料:

Nginx Wiki   http://wiki.nginx.org/HttpProxyModule

YaoWeibin Github    https://github.com/yaoweibin/ngx_http_substitutions_filter_module

   

已有 28 條評論

  1. Stone 8 年前 (2016-06-01)
    @

    ssl证书到期了

  2. 阿福 9 年前 (2015-07-10)
    @

    月妹纸,你忘记加2行注释了。。害我报错了。

  3. 雷子献 9 年前 (2015-05-18)
    @

    雷大大来看看

  4. 「有事燒紙」 9 年前 (2015-01-26)
    @

    拿百度测试了下,真好玩

  5. 08host 9 年前 (2015-01-16)
    @

    我有2台服务器 一台做为前台反代 一台作为后台,域名解析到前台反代主机,前台和后台服务器都有IP,多个域名,请问能否帮忙写一个配置文件nginx.conf给我做参考呢?我的邮箱是[email protected] 谢谢!!

  6. declanVox 10 年前 (2014-10-31)
    @

    请问如何反代动态图片站比如 tumblr 的图片链接?

  7. TechWeb 10 年前 (2014-05-16)
    @

    反代别人的网站?为什么呀?作用是?

  8. Showfom 11 年前 (2013-08-07)
    @

    proxy_cache_use_stale invalid_header error timeout http_502你在这里漏了一个分号proxy_cache_use_stale invalid_header error timeout http_502;

    • 飛天鼠 11 年前 (2013-08-09)
      @

      这个我发现了- -但是不想改。。。。。。。。。。加了句

  9. 残雪 11 年前 (2013-07-26)
    @

    转走了

  10. 飛天鼠 11 年前 (2013-07-16)
    @

    test

  11. 坑爹网 11 年前 (2013-05-16)
    @

    话说你后面那些都是要在装好了lnmp的前提下做的?

  12. mayapop 11 年前 (2013-04-18)
    @

    这个站为什么无法替换中文啊? 确定 conf已经是utf-8格式, 替换规则也没有问题, subs_filter ‘给力小说网’ ‘百度’; 但始终只能替换 英文.无法替换中文.月月给帮忙看看啊…

    • 飛天鼠 11 年前 (2013-05-17)
      @

      另存为utf-8 without BOM

  13. hostgator 11 年前 (2013-04-18)
    @

    来学习一下,不太懂的

  14. 婴儿游泳池 11 年前 (2013-04-18)
    @

    完全不明白

  15. 花七七 11 年前 (2013-04-14)
    @

    我跑去研究Android去了……

    • 飛天鼠 11 年前 (2013-04-14)
      @

      手机不给力。。。不研究了= =

      • 花七七 11 年前 (2013-04-14)
        @

        。。。。哈哈

  16. 大笨马优惠码 11 年前 (2013-04-12)
    @

    个别文章有被墙的迹象?樱花VPS装什么了?

    • 飛天鼠 11 年前 (2013-04-14)
      @

      – -在測試防C的,現在搞好了

  17. luguo 11 年前 (2013-04-12)
    @

    摘月,你的代码部分在firefox里不显示

    • 飛天鼠 11 年前 (2013-04-14)
      @

      唔- -RP問題

  18. 婴儿游泳池 11 年前 (2013-04-07)
    @

    好复杂好深奥

  19. ixwebhosting 11 年前 (2013-04-05)
    @

    好详细的,来学习一下

  20. ★Extreme★ 11 年前 (2013-04-04)
    @

    搞了,还是没你的快