码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx 多规则关键字替换模块 subs_filter_module

时间:2014-12-28 01:50:30      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:substitutions 关键词替换 多行替换 文本替换 nginx   sub_filter

本文介绍一个nginx第三方模块nginx_substitutions_filter,此模块作用是替换过滤响应主体,nginx也有一个类似这样的原生模块(http_sub_module),但有一个缺点,就是只能使用一条规则,而nginx_substitutions_filter则不限规则数量(测试效率比原生模块稍慢一点)。


安装方法

    1.svn checkout http://substitutions4nginx.googlecode.com/svn/trunk/ substitutions4nginx-read-only

    2.yum install subversion


----------------------------------------------------------

由于其他一些原因,可能您无法连接到googlecode  那么我这里已经有打包好的tar包,解压出来然后重新编译安装nginx即可   下载地址:http://pan.baidu.com/s/1pJBMk2Z   2014新版下载地址:

http://pan.baidu.com/s/1c0rPPj6

原googlecode版本好像已经停止更新了,作者似乎迁移到GitHub上了,https://github.com/yaoweibin/ngx_http_substitutions_filter_module/

最近更新是 2014.02.15   添加了

subs_filter_bypass

-------------------------------------------------------------


编译添加额外第三方模块的方法

cd   nginx/
./configure --add-module=path/substitutions4nginx-read-only

这里的path 是substitutions4nginx-read-only 的目录路径

make  &&  make install


用法:

    有两条指令:subs_filter_types,subs_filter

    subs_filter_types

    语法: subs_filter_types mime-type [mime-types]

    默认:subs_filter_types text/html (高版本的nginx如果有写着条,那么重启服务的时候会有警告信息,因为默认就是检查 text/html的信息,与程序有重复配置)

    适用区域:http, server, location

    subs_filter_types是用来指令需要替换的文件类型,默认是text/html类型。此模块无法处理经过压缩的内容,虽然能与gzip filter模块兼容,但无法处理反向代理返回的内容。当需要处理反向代理的内容时,可以使用如下语句禁用压缩:

 

   proxy_set_header Accept-Encoding ‘‘;


    subs_filter

    语法;subs_filter 源字段串 目标字段串 [gior]

    默认:无

    适用区域:http, server, location

    subs_filter指令允许在nginx响应输出内容时替换源字段串(正则或固定)为目标字符串。第三个标志含意如下:

    g(默认): 替换所有匹配的字段串。(默认可省略)

    i: 执行区分大小写的匹配。

    o: 仅替换首个匹配字符串。

    r: 使用正则替换模式,默认是固定模式。


模块官网:

https://github.com/yaoweibin/ngx_http_substitutions_filter_module/


substitutions 经测试支持中文替换。

本文出自 “Professor哥” 博客,转载请与作者联系!

Nginx 多规则关键字替换模块 subs_filter_module

标签:substitutions 关键词替换 多行替换 文本替换 nginx   sub_filter

原文地址:http://professor.blog.51cto.com/996189/1596720

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!