ngx_http_addition_module在响应之前或者之后追加文本内容,ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串。

编译安装nginx添加模块

ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串

  • 使用最新稳定版1.12.0,编译安装
  • 使用–with-http_addition_module添加ngx_http_addition_module模块
  • 使用–with-http_sub_module添加ngx_http_sub_module模块
mkdir -p /opt/tmp
cd /opt/tmp
wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0
./configure --prefix=/usr/local/nginx-1.12.0 --with-http_addition_module --with-http_sub_module
make
make install
sudo /usr/local/nginx-1.12.0/sbin/nginx
sudo /usr/local/nginx-1.12.0/sbin/nginx -s reload

配置例子

替换page为gophper.com,在响应之前追加header.html

location / {
           sub_filter  page 'gophper.com';
           sub_filter_once on;
           sub_filter_types *;
           add_before_body /header.html;
           add_after_body /footer.html;
           addition_types *;
           root   html;
           index  index.html index.htm;
       }

ngx_http_addition_module指令(Directives)

语法:     add_before_body uri;
默认值:     —
配置段:     http, server, location

发起一个子请求,请求给定的uri,并且将内容追加到主题响应的内容之前。

语法:     add_after_body uri;
默认值:     —
配置段:     http, server, location

发起一个子请求,请求给定的uri,并且将内容追加到主题响应的内容之后。

syntax: addition_types mime-type ...;
default: addition_types text/html;
context: http, server, location

这个指令在0.7.9开始支持,指定需要被追加内容的MIME类型,默认为“text/html”,如果指定为*,为所有(0.8.29).

ngx_http_sub_module指令(Directives)

语法:sub_filter string replacement;
默认值: —
配置段:http, server, location

设置需要使用说明字符串替换说明字符串.string是要被替换的字符串,replacement是新的字符串,它里面可以带变量。

语法:sub_filter_last_modified on | off;
默认值:sub_filter_last_modified off;
配置段:http, server, location

用于设置网页内替换后是否修改

语法:sub_filter_once on | off;
默认值:sub_filter_once on;
配置段:http, server, location

字符串替换一次还是多次替换,默认替换一次,如果为on为替换一次如果为off为替换所有

语法:sub_filter_types mime-type ...;
默认值:sub_filter_types text/html;
配置段:http, server, location

指定需要被替换的MIME类型,默认为“text/html”,如果指定为*,为所有(0.8.29)