nginx反向代理加替换教程

本文适合全新安装,也适合安装了Lnmp.org一键包的安装
1、下载substitutions4nginx模块,这个模块用于替换。

pkill nginx
/etc/init.d/nginx stop #停止nginx
cd
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 -c http://nginx.org/download/nginx-1.3.13.tar.gz
tar zxvf nginx-1.3.13.tar.gz
cd nginx-1.3.13
./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

3、新建一个配置用于反代
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 u.longsays.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://u.longsays.com; #强制定义Referer,程序验证判断会用到
proxy_set_header Host u.longsays.com; #定义主机头,如果目标站点绑定的域名个server_name项的吻合则使用$host
proxy_pass http://u.longsays.com; #指定目标,建议使用IP或者nginx自定义池
proxy_set_header Accept-Encoding ""; #清除编码
}
}

如果要替换中文,要先把conf文件转成utf-8模式。
4、重启Nginx
/etc/init.d/nginx restart
如无意外,此时访问你的域名就会看到成功反代并替换了。

《nginx反向代理加替换教程》上有25条评论

  1. Debian下如果执行./configure后出现
    ./configure: error: the HTTP rewrite module requires the PCRE library.
    ./configure: error: SSL modules require the OpenSSL library.
    ./configure: error: the HTTP gzip module requires the zlib library.

    以上任意提示,可以执行
    apt-get install libpcre3 libpcre3-dev openssl libssl-dev

    1. 阿里云主机么!
      优化神马的不必考虑太多…这只是极限负载和折腾才玩的吧…
      认真做站,默认就挺好
      而且最要优化的是php

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注