欢迎来到梦飞科技

服务器租用

当前优惠活动:

Nginx反向署理替换内容模块ngx_http_substitutions_filter_module

======

以前写过http://www.mfisp.com/html/help/server/2016/0912/4185.html">Nginxhttp://www.mfisp.com/html/help/server/2016/0916/4455.html">反向署理通过with-http_sub_module和substitutions4nginx模块替换正文内容和URL和在军哥lnmp的情况下设置反向署理处事器的要领教程

本教程基于军哥lnmp情况,其他Nginx类同。区别在于nginx conf的位置,一般编译的在/usr/local/nginx/conf/,从源安装的在/etc/nginx。

lnmp的安装这里省略,下面教程是在已经安装好的lnmp情况下设置反向署理处事器,并实现替换内容

一、仅仅作为反向署理处事器,做cdn加快,不替换内容

1.新增虚拟主机设置文件用于反代

找到您的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/xxorg.com.conf

#xxorg.com是你要绑定的域名

设置文件内容:{后端(ip为1.2.3.4)绑定域名xxorg.com,前端绑定域名xxorg.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后把xxorg.com绑定到前端就可以会见了

也可以用以下要领查察设置文件是否正确:

执行:

/usr/local/nginx/sbin/nginx -t

查抄设置是否正常,假如显示:

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

就说明nginx的设置文件正常,不然按错误提示修改设置。

然后执行

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

使设置生效,

/etc/init.d/nginx restart

=========================

二、下面通过Nginx反向署理别人的网站,并替换相关内容

1.编译nginX:

apt-get update#nginx-full这个包内里包括着所有需要用到的模块
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,就不需要编译了。

梦飞科技 - 全球数据中心基础服务领先供应商

Copyright © 2003-2019 MFISP.COM. 国外服务器租用 IDC公司 版权所有 粤ICP备11019662号