之前写过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. electron 写入注册表 实现开机自启动

    windows平台 首先先明确:开机自启动写入注册表的位置,在KEY_CURRENT_USER\Software\\Microsoft\\Windows\\CurrentVersion\\Run 打开 ...

  2. Mysql数据库之auto_increment

    一.概述 在数据库应用中,我们经常需要用到自动递增的唯一编号来标识记录.在MySQL中,可通过数据列的auto_increment属性来自动生成.可在建表时可用“auto_increment=n”选项 ...

  3. svn的简介

    Svn(Subversion)是近年来崛起的版本管理工具,在当前的开源项目里(J2EE),几乎95%以上的项目都用到了SVN.Subversion项目的初衷是为了替换当年开源社区最为流行的版本控制软件 ...

  4. label联动checkbox

    label联动checkbox时,若label包含在checkbox外层时label不需for属性,设置label的display属性为block时可以使整个div联动.

  5. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  6. Akka(34): Http:Unmarshalling,from Json

    Unmarshalling是Akka-http内把网上可传输格式的数据转变成程序高级结构话数据的过程,比如把Json数据转换成某个自定义类型的实例.按具体流程来说就是先把Json转换成可传输格式数据如 ...

  7. iOS 之 runtime --- 集百家之言

    runtime runtime用在什么地方? 说法 在程序运行过程中,动态的创建一个类(比如KVO的底层实现) 在程序运行过程中,动态地为某个类添加属性.方法,修改属性值\方法(method swiz ...

  8. Shuffle过程的简单介绍

    Shuffle是连接Map和Reduce的桥梁 Shuffle分为Map端的Shuffle和Reduce端的Shuffle Map端的shuffle 1输入数据和执行任务: 分片后分配Map任务,每个 ...

  9. BZOJ-1864-[Zjoi2006]三色二叉树(树形dp)

    Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...

  10. TestNG并行测试

    并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者子组件的能力.TestNG允许我们以并行(多线程)的方式来执行测试.这就意味着基于TestNG测试组 ...