A-A+

NginX 反代系列教程

2016年08月30日 Linux 暂无评论 阅读 浏览:986 次

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

如果你用的是军哥的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

标签:

给我留言

Copyright © 龙网博客 保留所有权利.   Theme  Ality

用户登录