之前写过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. VIM格式化代码(How to format code with VIM)

    1) 按两下小写g,即gg,定位光标到第一行.(2) 按住Shift+v,即大写V,进入可视化编辑的列编辑模式.(3) Shift+g,即大写G,选中整个代码.(4) 按下等号=,格式化所有代码.

  2. Node Sass could not find a binding for your current environment 解决办法

    具体错误如下: 解决办法: 命令行执行  npm rebuild node-sass  命令(如果不行,则先运行npm install node-sass命令执行再执行 npm rebuild nod ...

  3. 【UML 建模】类图介绍

    1.类图是面向对象系统建模中最常用和最重要的图,是定义其它图的基础.类图主要是用来显示系统中的类.接口以及它们之间的静态结构和关系的一种静态模型. 2.类的关系有泛化(Generalization). ...

  4. tp5上传图片添加永久素材到微信公众号

    $file = request()->file('image');if(!$file){ $res['status'] = false; $res['msg'] = '必须上传文件'; retu ...

  5. java传值与传引用总结

    基本数据类型 我们先来看一个代码 public class ParamTest { public static void main(String[] arge) { double percent = ...

  6. 解决phpstorm ftp自动保存文件问题

    初次使用phpstorm, 1.配置ftp时,远程文件要用/ftp用户名/文件夹名: 2.由于版本管理的原因(猜测),直接从本地原有文件修改时各种办法都无法上传,结果从服务器上下载一份再修改,解决这个 ...

  7. 使用Spring框架实现用户登录实例

    以下要讲的案例来自于<Spring 3.X 企业应用开发实战>这本书. 针对我一周的摸索,现在总结几个易错点,当然,这是在我自己犯过错误的前提下总结出来的,如果有说的不到位的地方,欢迎大家 ...

  8. C++ 对象成员函数(非静态方法)

    1.神奇的inline语法与语义 inline语义C99和C++98都有.之前在单源文件编写的时候一直没有发现问题,但是一考虑到多文件的链接,就发现矛盾了. 一些inline的原则: 1. inlin ...

  9. 使用python实现计算器功能

    学习python过程中的作业.实现了+.-.×./.及幂运算,支持括号优先级. 代码为python3.5 import re def formatEquation(string): string = ...

  10. Jni中C++和Java的参数传递(转)

    如何使用JNI的一些基本方法和过程在网上多如牛毛,如果你对Jni不甚了解,不知道Jni是做什么的,如何建立一个基本的jni程序,或许可以参考下面下面这些文章:利用VC++6.0实现JNI的最简单的例子 ...