本教程共三部分内容:
Nginx配合ngx_http_substitutions_filter_module模块搭建反向代理服务器,并替换目标站内容【之一】
Nginx配合ngx_http_substitutions_filter_module模块搭建反向代理服务器,并替换目标站内容【之二】
Nginx反向代理并替换内容模块ngx_http_substitutions_filter_module的使用说明
二、下面通过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,就不需要编译了。
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
以xxorg.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 xxorg.com; index index.php; #默认首页 location / { subs_filter_types text/html text/css text/xml; subs_filter www.baidu.com xxorg.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状态时启用过期缓存 } } }
注意:如果您要通过Nginx的nginx_substitutions_filter模块替换的内容里面有中文,请将conf文件保存为utf-8 without BOM编码。
本教程共三部分内容:
Nginx配合ngx_http_substitutions_filter_module模块搭建反向代理服务器,并替换目标站内容【之一】
Nginx配合ngx_http_substitutions_filter_module模块搭建反向代理服务器,并替换目标站内容【之二】
Nginx反向代理并替换内容模块ngx_http_substitutions_filter_module的使用说明
博主,请教个问题
网站有很多相同的标签,我想替换成不同的内容,怎么实现正则替换的内容从指定的文件中抽取内容出来替换
subs_filter ‘(.*)’ ‘从content.txt文档中抽取一行内容’ gr;