nginx反代加替换傻瓜教程

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

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

pkill nginx
/etc/init.d/nginx stop #停止nginx
cd /root
apt-get install -y git
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module



2、重新编译nginx

wget -c http://soft.vpser.net/web/nginx/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz
./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/mjj.baidu.wf.conf  #mjj.baidu.wf是你要绑定的域名,当然你也可以用其他名字.conf
输入以下内容:
server{
listen 80;
server_name mjj.baidu.wf;    #绑定的域名
index index.php;      #默认首页
access_log off;        #off 关闭日志
location / {
subs_filter www.hostloc.com mjj.baidu.wf;  #替换掉域名
subs_filter static/image/common/logo.png http://img.jybb.me/logo/baiduwflogo.png; #替换掉LOGO
subs_filter 'http://mjj.baidu.wf/uc_server/avatar.php?uid=1&size' 'http://mjj.baidu.wf/uc_server/images/noavatar_middle.gif?size';   #替换掉头像
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://www.oiplay.com/html/459.html

5、重启LNMP

/etc/init.d/nginx restart


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

原文地址 http://jybb.me/nginx-proxy-pass-and-substitutions4nginx

文章评论已关闭