nginx的with-http_sub_module模块使用之替换字符串

一、介绍

该ngx_http_sub_module模块是一个过滤器,通过将一个指定的字符串替换为另一个字符串来修改响应。该模块不是默认生成的,它应该使用--with-http_sub_module 配置参数启用 。

二、指令介绍

2.1、sub_filter指令

作用:用于替换字符串

用法:sub_filter   要替换的字符串   替换后的字符串,不区分字符串的大小写

范围:http   server   location

默认:-

2.2、sub_filter_last_modified指令

作用:允许在替换期间保留来自原始响应的“Last-Modified”标题字段以促进响应缓存。默认情况下,在处理期间修改响应的内容时,标题字段被删除。

用法:sub_filter_last_modified  on | off

范围:http   server   location

默认:sub_filter_last_modified  on

 2.3、sub_filter_once

作用:指示是否查找每个字符串以替换一次或重复替换。

用法:sub_filter_once  on | off

范围:http   server   location

默认:http   server   location

2.4、sub_filter_types

作用:指定MIME类型的字符串替换,除了“ text/html” 之外,还可以在指定MIME类型的响应中启用字符串替换。特殊值“ *”匹配任何MIME类型

用法:sub_filter_types mime-type ...

范围:http   server   location

默认:sub_filter_types text / html;

三、操作演示

3.1、编译安装nginx

编译安装nginx,注意加上--with-http_sub_module模块,这里提供编译的参数:

./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module \
--with-http_sub_module \

 

3.2、安装nginx

make && make  install

 

3.3修改配置文件nginx.conf

 location / {
           sub_filter 'nginx.com' 'baidu.com'; --把nginx.com替换成了baidu.com
           sub_filter 'nginx.org' 'buy.jiegeng.com';--把ngin.org替换成了buy.jiegeng.com
           sub_filter_types css/html;--替换的类型
           sub_filter_once off;--全部替换
           root  html;
           index  index.html index.htm;
        }

3.4、重启

/usr/sbin/nginx    -s   reload

3.5、验证

3.5.1修改后页面

 

3.5.2原始页面

 四、支持正则的第三方模块

使用ngx_http_sub_module模块好处是nginx内置该模块使用方便,不足之处在于该模块不支持正则替换,灵活性不够,

支持正则匹配替换的第三方模块ngx_http_substitutions_filter_module:

4.1、下载

下载地址:https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip

 添加参数--add-module=/模块解压后文件路径

./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module \
--with-http_sub_module \
--add-module=../ngx_http_substitutions_filter_module-master

4.2、subs_filter的使用介绍

* subs_filter
subs_filter 语法: subs_filter source_str destination_str [gior]
默认: none
适用: http,server,location
subs_filter 是用来替换文本的,可以使用正则
g(默认):替换匹配项。
i  :区分大小写的匹配
o : 只匹配发现的第一个。
r  : 正则匹配。

 

posted @ 2018-05-23 12:21  凉生墨客  阅读(11679)  评论(0编辑  收藏  举报