
发现以前写的几篇教程,挺模糊的,有人看了摸不清头脑。(由于当时我也不太懂……),现在重新写一遍,并增加注释。
如果你用的是军哥的LNMP,而且又需要替换内容,则需要重新编译nginx。如果不需要替换内存,仅用于加速自己的网站,则不需要重新编译。
1.安装Nginx
你可以用源安装/编译安装/一键包(此处省略)
2.新增虚拟主机配置文件用于反代
找到您的nginx conf所在位置,一般编译的在/usr/local/nginx/conf/,从源安装的在/etc/nginx
在nginx.conf的http层加入以下内容:
proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_temp_path /home/cache/temp; #临时文件目录 proxy_cache_path /home/cache/path levels=1:2 keys_zone=cache_one:5m inactive=7d max_size=1g; #5m为内存占用,1g为最大硬盘占用,cache_one为缓存区名字,如果修改则下文的配置亦要相应修改。
mkdir /home/cache/path -p mkdir /home/cache/temp chmod 777 -R /home/cache
新增虚拟主机配置:
vi /usr/local/nginx/conf/vhost/example.com.conf #example.com是你要绑定的域名
配置文件内容:{后端(ip为1.2.3.4)绑定域名example.com,前端绑定域名example.com,域名解析到前端,实现cdn加速。}
server{
listen 80;
server_name example.com www.example.com;
#绑定的域名
index index.php;
#默认首页
access_log off;
#off 关闭日志
location / {
proxy_cache_key "$scheme://$host$request_uri";
#缓存key规则,用于自动清除缓存。
proxy_cache cache_one;
#缓存区名称,与前面定义的相同
proxy_cache_valid 200 304 3h;
proxy_cache_valid 301 3d;
proxy_cache_valid any 10s;
#200 304状态缓存3小时
301状态缓存3天
其他状态缓存(如502 404)10秒
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#向后端传递访客ip
proxy_set_header Referer http://example.com;
#强制定义Referer,程序验证判断会用到
proxy_set_header Host $host;
#定义主机头
proxy_pass http://1.2.3.4;
#指定后端ip,可以加端口
#proxy_cache_use_stale invalid_header error timeout http_502;
#当后端出现错误、超时、502状态时启用过期缓存,慎用。
}
}
如无意外,重启nginx后把example.com绑定到前端就可以访问了
/etc/init.d/nginx restart
下面介绍的是反代别人的网站(类似于小偷),并替换相关内容
1.编译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 wget http://nginx.org/download/nginx-1.2.8.tar.gz tar zxvf nginx-1.2.8.tar.gz cd nginx-1.2.8 ./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
如果您用的系统是Debian,就不需要编译了。
echo "deb http://packages.dotdeb.org squeeze all" >>/etc/apt/sources.list echo "deb-src http://packages.dotdeb.org squeeze all" >>/etc/apt/sources.list #添加dotdeb源,已多次介绍dotdeb源的好处 apt-get update apt-get install nginx-full #nginx-full这个包里面包含着所有需要用到的模块。
2.修改nginx.conf
编译的在/usr/local/nginx/conf/nginx.conf,源安装的在/etc/nginx/nginx.conf
以example.com反代www.baidu.com并替换内容为例:
user www;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
… #此处省略一万字
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /home/cache/temp;
proxy_cache_path /home/cache/one levels=1:2 keys_zone=cache_one:3m inactive=7d max_size=1g;
server {
listen 80;
server_name example.com;
index index.php;
#默认首页
location / {
subs_filter_types text/html text/css text/xml;
subs_filter www.baidu.com example.com gi;
#替换模块,下文详解。
proxy_cache_key "$scheme://$host$request_uri";
#缓存key规则,用于自动清除缓存。
proxy_cache cache_one;
#缓存区名称,必须与前面定义的相同
proxy_cache_valid 200 304 3h;
proxy_cache_valid 301 3d;
proxy_cache_valid any 10s;
#200 304状态缓存3小时
#301状态缓存3天
#其他状态缓存(如502 404)10秒
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#向后端传递访客ip
proxy_set_header Referer http://www.baidu.com;
#强制定义Referer,程序验证判断会用到
proxy_set_header Host www.baidu.com;
#定义主机头
proxy_pass http://1.2.3.4;
#指定后端ip
proxy_set_header Accept-Encoding "";
#清除编码
proxy_cache_use_stale invalid_header error timeout http_502;
#当后端出现错误、超时、502状态时启用过期缓存
}
}
}
温馨提示:如果您要替换的内容里面有中文,请将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
ssl证书到期了
月妹纸,你忘记加2行注释了。。害我报错了。
雷大大来看看
我有2台服务器 一台做为前台反代 一台作为后台,域名解析到前台反代主机,前台和后台服务器都有IP,多个域名,请问能否帮忙写一个配置文件nginx.conf给我做参考呢?我的邮箱是[email protected] 谢谢!!
请问如何反代动态图片站比如 tumblr 的图片链接?
反代别人的网站?为什么呀?作用是?
proxy_cache_use_stale invalid_header error timeout http_502你在这里漏了一个分号proxy_cache_use_stale invalid_header error timeout http_502;
这个我发现了- -但是不想改。。。。。。。。。。加了句
转走了
test
话说你后面那些都是要在装好了lnmp的前提下做的?
nginx就行
这个站为什么无法替换中文啊? 确定 conf已经是utf-8格式, 替换规则也没有问题, subs_filter ‘给力小说网’ ‘百度’; 但始终只能替换 英文.无法替换中文.月月给帮忙看看啊…
另存为utf-8 without BOM
来学习一下,不太懂的
完全不明白
我跑去研究Android去了……
手机不给力。。。不研究了= =
。。。。哈哈
个别文章有被墙的迹象?樱花VPS装什么了?
– -在測試防C的,現在搞好了
摘月,你的代码部分在firefox里不显示
唔- -RP問題
好复杂好深奥
好详细的,来学习一下
搞了,还是没你的快