欢迎来到云服务器

服务器租用

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

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_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;
}

Nginx反向署理并替换内容涉及指令: * 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*: 正则。

腾讯云代理

Copyright © 2003-2021 MFISP.COM. 国外vps服务器租用 梦飞主机测评 版权所有 粤ICP备11019662号