之前写过nginx反代替换的教程(传送门),使用了ngx_http_substitutions_filter_module模块。不过这货只能替换同一行,具有局限性-_-#

现在一个更强大的替换模块来了……replace-filter-nginx-module

下面只翻译一下,再加个安装教程,因为我自己也没弄懂怎样玩= =

.安装此模块需要先安装sregex运行库

apt-get update;
apt-get install git make gcc -y
#Centos改成yum
git clone https://github.com/agentzh/sregex
cd sregex
make
make install
cd ..
git clone https://github.com/agentzh/replace-filter-nginx-module
wget http://nginx.org/download/nginx-1.2.6.tar.gz
tar zxvf nginx-1.2..tar.gz
cd nginx-1.2.
./configure --add-module=../replace-filter-nginx-module #自行加其他编译参数
make
make install
nginx.conf的用法举例: location /t {
default_type text/html;
echo abc;
replace_filter 'ab|abc' X;
} location / {
# proxy_pass/fastcgi_pass/... # caseless global substitution:
replace_filter '\d+' 'blah blah' 'ig';
replace_filter_types text/plain text/css;
}
Syntax语法: ^ 匹配起始行数
$ 匹配末尾行数 \A match only at beginning of stream
\z match only at end of stream \b match a word boundary
\B match except at a word boundary . match any char
\C match a single C-language char (octet) [ab0-] character classes (positive)
[^ab0-] character classes (negative) \d match a digit character ([-])
\D match a non-digit character ([^-]) \s match a whitespace character ([ \f\n\r\t])
\S match a non-whitespace character ([^ \f\n\r\t]) \h match a horizontal whitespace character
\H match a character that isn't horizontal whitespace \v match a vertical whitespace character
\V match a character that isn't vertical whitespace \w match a "word" character ([A-Za-z0-9_])
\W match a non-"word" character ([^A-Za-z0-9_]) \cK control char (example: VT) \N match a character that isn't a newline ab concatenation; first match a, and then b
a|b alternation; match a or b (a) capturing parentheses
(?:a) non-capturing parantheses a? match or times, greedily
a* match or more times, greedily
a+ match or more times, greedily a?? match or times, not greedily
a*? match or more times, not greedily
a+? match or more times, not greedily a{n} match exactly n times
a{n,m} match at least n but not more than m times, greedily
a{n,} match at least n times, greedily a{n}? match exactly n times, not greedily (redundant)
a{n,m}? match at least n but not more than m times, not greedily
a{n,}? match at least n times, not greedily 作者信息:
Yichun “agentzh” Zhang (章亦春) agentzh@gmail.com

Syntax Supported


The following Perl 5 regex syntax features have already been implemented.


^             match the beginning of lines
$ match the end of lines \A match only at beginning of stream
\z match only at end of stream \b match a word boundary
\B match except at a word boundary . match any char
\C match a single C-language char (octet) [ab0-9] character classes (positive)
[^ab0-9] character classes (negative) \d match a digit character ([0-9])
\D match a non-digit character ([^0-9]) \s match a whitespace character ([ \f\n\r\t])
\S match a non-whitespace character ([^ \f\n\r\t]) \h match a horizontal whitespace character
\H match a character that isn't horizontal whitespace \v match a vertical whitespace character
\V match a character that isn't vertical whitespace \w match a "word" character ([A-Za-z0-9_])
\W match a non-"word" character ([^A-Za-z0-9_]) \cK control char (example: VT) \N match a character that isn't a newline ab concatenation; first match a, and then b
a|b alternation; match a or b (a) capturing parentheses
(?:a) non-capturing parantheses a? match 1 or 0 times, greedily
a* match 0 or more times, greedily
a+ match 1 or more times, greedily a?? match 1 or 0 times, not greedily
a*? match 0 or more times, not greedily
a+? match 1 or more times, not greedily a{n} match exactly n times
a{n,m} match at least n but not more than m times, greedily
a{n,} match at least n times, greedily a{n}? match exactly n times, not greedily (redundant)
a{n,m}? match at least n but not more than m times, not greedily
a{n,}? match at least n times, not greedily

The following escaping sequences are supported:


\t          tab
\n newline
\r return
\f form feed
\a alarm
\e escape
\b backspace (in character class only)
\x{}, \x00 character whose ordinal is the given hexadecimal number
\o{}, \000 character whose ordinal is the given octal number

Escaping a regex meta character yields the literal character itself, like \{ and \..


Only the octet mode is supported; no multi-byte character encoding love (yet).

Installation

make
make install

Gnu make and gcc are required. (On operating systems like FreeBSD and Solaris, you should typegmake instead of make here.)

It will build libsregex.so (or libsregex.dylib on Mac OS X), libsregex.a, and the command-line utility sregex-cli and install them into the prefix /usr/local/ by default.

If you want to install into a custom location, then just specify the PREFIX variable like this:

make PREFIX=/opt/sregex
make install PREFIX=/opt/sregex

If you are building a binary package (like an RPM package), then you will find the DESTDIR variable handy, as in

make PREFIX=/opt/sregex
make install PREFIX=/opt/sregex DESTDIR=/path/to/my/build/root

If you run make distclean before make, then you also need bison 2.7+ for generating the regex parser files.

 

Synopsis


    location /t {
default_type text/html;
echo abc;
replace_filter 'ab|abc' X;
} location / {
# proxy_pass/fastcgi_pass/... # caseless global substitution:
replace_filter '\d+' 'blah blah' 'ig';
replace_filter_types text/plain text/css;
} location /a {
# proxy_pass/fastcgi_pass/root/... # remove line-leading spaces and line-trailing spaces,
# as well as blank lines:
replace_filter '^\s+|\s+$' '' g;
} location /b {
# proxy_pass/fastcgi_pass/root/... # only remove line-leading spaces and line-trailing spaces:
replace_filter '^[ \f\t]+|[ \f\t]+$' '' g;
} location ~ '\.cpp$' {
# proxy_pass/fastcgi_pass/root/... replace_filter_types text/plain; # skip C/C++ string literals:
replace_filter "'(?:\\\\[^\n]|[^'\n])*'" $& g;
replace_filter '"(?:\\\\[^\n]|[^"\n])*"' $& g; # remove all those ugly C/C++ comments:
replace_filter '/\*.*?\*/|//[^\n]*' '' g;
}

比ngx_http_substitutions_filter_module 更强大的替换模块sregex的replace-filter-nginx-module的更多相关文章

  1. Python的regex模块——更强大的正则表达式引擎

    Python自带了正则表达式引擎(内置的re模块),但是不支持一些高级特性,比如下面这几个: 固化分组    Atomic grouping 占有优先量词    Possessive quantifi ...

  2. summerDao-比mybatis更强大无需映射配置的dao工具

    summerDao是summer框架中的一个数据库操作工具,项目地址:http://git.oschina.net/xiwa/summer. 怎么比mybatis更强大,怎么比beetlsql更简单, ...

  3. 使用Nginx反向代理和内容替换模块实现网页内容动态替换功能

    2016年11月21日 10:30:00 xian_02 阅读数:10943   Nginx是一款轻量级高性能服务器软件,虽然轻量,但功能非常强大,可用于提供WEB服务.反向代理.负载均衡.缓存服务. ...

  4. zwPython,字王集成式python开发平台,比pythonXY更强大、更方便。

    zwPython,字王集成式python开发平台,比pythonXY更强大.更方便. 更强大,内置opencv.cuda/opencl.NLTK自然语言.pygame游戏设计等多个重量级模块库. 更方 ...

  5. Excel催化剂开源第24波-较VBA更强大的.Net环境的正则表达式

    在VBA上可以调用正则表达式库,从而编写正则表达式自定义函数,这个相信不少VBA开发者已经熟知,但VBA的VBScript正则表达式库毕竟是一个过时的产品,不像.Net那样是与时俱进的,所以两者实现出 ...

  6. 思维导图软件TheBrain 8全新发布 提供更强大的信息管理

    TheBrain思维导图软件是全球唯一一款动态的网状结构的思维导图软件,广泛用于学习.演讲.项目管理.会议.需求调研与分析等.其独特的信息组织方式使得用户可以创建并连接到数以万计的数字想法,为此在全球 ...

  7. cVim—Chrome上更强大的vim插件

    cVim——Chrome上更强大的vim插件 介绍 也许很多人在chrome上都用过类似Vimium, ViChrome的插件,这些插件的目的都差不多,就是在浏览器中提供一些类似vim的操作来提高效率 ...

  8. 功能更强大的格式化工具类 FormatUtils.java

    package com.util; import java.text.DecimalFormat; import java.text.ParseException; import java.text. ...

  9. 10个工具让你的 shell 脚本更强大

    10个工具让你的 shell 脚本更强大 很多人误以为shell脚本只能在命令行下使用.其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等.你可以控制最终的输出,光标位 置还有各种输 ...

随机推荐

  1. UVa 10954,Add All

    Huffman编码简化版,优先队列搞定. 1A 调试的时候发现一个问题..木有想明白...问题代码里给出,哪位大神给解释下. #include <iostream> #include &l ...

  2. 基于zookeeper的Swarm集群搭建

    简介 Swarm:docker原生的集群管理工具,将一组docker主机作为一个虚拟的docker主机来管理. 对客户端而言,Swarm集群就像是另一台普通的docker主机. Swarm集群中的每台 ...

  3. 使用OpenCV训练Haar like+Adaboost分类器的常见问题

    <FAQ:OpenCV Haartraining>——使用OpenCV训练Haar like+Adaboost分类器的常见问题 最近使用OpenCV训练Haar like+Adaboost ...

  4. 操作系统--进程管理1--单个CPU情况

    1.进程概念 进程:一个正在执行的程序:操作系统提出进程概念目的:是为了跟踪程序在执行期间的状态.而程序只是一段代码,是一个静态的概念 无法准确描述程序执行时候发生的一切.程序代码被加载进内存后就以进 ...

  5. Problem O

    Problem Description Before bridges were common, ferries were used to transport cars across rivers. R ...

  6. Linux必知必会的目录与启动过程

    第1章 /etc/目录 1.1 /etc/sysconfig/network-scripts/ifcfg-eth0 linux第一块网卡的配置文件 [root@znix ~]# cat /etc/sy ...

  7. HQL语法

    HQL:Hibernate Query Language HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征. $下面介绍HQL语句的语法 1.from子句 from Person 表明从P ...

  8. 状态码为 200 from cache和304 Not modified的区别

    1.请求状态码为 200  from cache: 表示该资源已经被缓存过,并且在有效期内,所以不再向浏览器发出请求,直接使用本地缓存. 如下图: 2.状态码为 304 Not modified: 表 ...

  9. jQuery 插件格式 规范

    方式一(自定义对象): (function($, window, document) {  var Plugin, defaults, pluginName; 调用时的函数名:     pluginN ...

  10. Problem C: 是否回文数?

    Description 定义Data类,有一个int类型的属性.定义其构造函数.setValue函数和isPalindrome函数,其中setValue函数用于设置属性值,isPalindrome用于 ...