原文: openresty/replace-filter-nginx-module

1. 概要

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;
}

2. 描述

该 Nginx 输出过滤模块尝试尽可能以非缓存模式执行正则表达式替换。

该模块没有使用像 PCRE 这样的传统回溯正则表达式 engines,而是使用由作者实现的新的 sregex 库,它从一开始就考虑了流处理。

sregex 支持 Perl 5 正则表达式的一个很好的公共子集。关于完整的功能列表,可查看 sregex 的文档: sregex Syntax Supported.

响应体数据仅在绝对必要时才进行缓存,例如面对属于数据块边界附近可能匹配的不完整捕获。

3. 指令

3.1 replace_filter

syntax: replace_filter <regex> <replace>

syntax: replace_filter <regex> <replace> <options>

defaultL no

context: http, server, location, location if

phase: output body filter

通过可选地正则标志指定正则模式和要被替换为的文本。

By default, the filter topped matching after the first match is found. This behavior can be changed by specifying the g regex option.

支持如下正则选项:

  • g:全局搜索和替换(默认关闭该功能)
  • i:不区分大小写(默认关闭)

在一个单独的字符串参数中可以联合多个选项,如下:

replace_filter hello hiya ig;

Nginx 变量可以插入到要替换的文本中,如下:

replace_filter \w+ "[$foo,$bar]";

如果要使用 '$' 字符,则使用 '$$',例如:

replace_filter \w "$$";

支持使用子匹配捕获变量,如 $&, $1, $2 等等,如下示例:

replace_filter [bc]|d [$&-$1-$2] g;

子匹配捕获变量的语义与 Perl 5 语言完全相同。

在同一范围下支持多个 replace_filter 指令,所有的 pattern 将在 tokenizer 中同时应用。不会使用最长的 token 匹配语义,而是根据配置文件中的顺序对 pattern 进行优先级排序。

如下示例是从 C/C++ 源文件中删除所有的 C/C++ 注释:

replace_filter "'(?:\\\\[^\n]|[^'\n])*'" $& g;
replace_filter '"(?:\\\\[^\n]|[^"\n])*"' $& g;
replace_filter '/\*.*?\*/|//[^\n]*' '' g;

Content-Encoding 响应头部不为空(类似 gzip)时,响应主体将始终保持不变。因此,如果是 ngx_proxy 模块的话,通常需要在 nginx.conf 中添加如下行,以在后端服务器响应中禁用 gzip 压缩:

proxy_set_header Accept-Encoding '';

响应仍然可以在 Nginx 服务器级别上进行 gzip 压缩。

3.2 replace_filter_types

syntax: replace_filter_types <mime-type> ...

default: replace_filter_types text/html

context: http, server, location, location if

phase: output body filter

指定要被处理的一个或多个 MIME 类型(在 Content-Type 响应头部中)。

默认情况下,仅处理 text/html 类型的响应。

3.3 replace_filter_max_buffered_size

syntax: replace_filter_max_buffered_size <size>

default: replace_filter_max_buffered_size 8k

context: http, server, location, location if

phase: output body filter

限制模块在运行时缓冲的数据的总大小,默认为 8k。

当达到限制值时,replace_filter 将立即停止处理,并保留所有剩余的响应正文数据。

3.4 replace_filter_last_modified

syntax: replace_filter_last_modified keep | clear

default: replace_filter_last_modified clear

context: http, server, location, location if

phase: output body filter

控制如何去处理现有的 Last-Modified 响应头。

默认情况下,该模块将清除 Last-Modified 响应头(如果有)。可以通过指定如下行来保留原始的 Last-Modified 响应头:

replace_filter_last_modified keep;

3.5 replace_filter_skip

syntax: replace_filter_skip $var

default: no

context: http, server, location, location if

phase: output header filter

该指令控制是否基于每个请求跳过所有的 replace_filter 规则。

该指令支持常量值或者包含 Nginx 变量的字符串。

当在请求输出 header 阶段将值评估为空值("")或者值 "0" 时,将不会跳过当前请求的 replace_filter 规则。否则,将会跳过当前请求的所有 replace_filter 规则。

如下一个简单的示例:

set $skip '';
location /t {
content_by_lua '
ngx.var.skip = 1
ngx.say("abcabd")
';
replace_filter_skip $skip;
replace_filter abcabd X;
}

4. 安装

首先安装 sregex 库: https://github.com/agentzh/sregex

然后重新编译 Nginx:

./configure --add-module=/path/to/replace-filter-nginx-module

如果 sregex 不是安装在默认前缀路径下(如 /usr/local),则可以在执行 ./configure 脚本前,通过 SREGEX_INCSREGEX_LIB 环境变量来指定 sregex 的安装位置。

export SREGEX_INC=/opt/sregex/include
export SREGEX_LIB=/opt/sregex/lib

从 Nginx 1.9.11 版本以上,可以在 ./configure 命令行上通过使用 --add-dynamic-module=PATH 选项来代替 --add-module=PATH ,从而将该模块编译为动态模块,然后在 ngxin.conf 上通过 load_module 指令显示加载该模块。

load_module /path/to/modules/ngx_http_replace_filter_module.so;

OpenResty之replace-filter-nginx-module的更多相关文章

  1. Emiller's Advanced Topics In Nginx Module Development

    Emiller的Nginx模块开发指南 By Evan Miller DRAFT: August 13, 2009 (changes) 翻译:Kongch @2010年1月5日 0:04am -- 2 ...

  2. 实战开发一个Nginx扩展 (Nginx Module)

    repo地址 https://github.com/wujunze/nginx-http-echo-module nginx_module_echo 使用echo指令输出一个字符串 Nginx 版本 ...

  3. 用lua nginx module搭建一个二维码

    用lua nginx module搭建一个二维码(qr code)生成器 作者 vinoca 發布於 2014年10月31日 如果有VPS,或者开源的路由器,安装一个nginx,添加lua-nginx ...

  4. openresty如何完美替换nginx

    下载openresty wget https://openresty.org/download/openresty-1.15.8.1.tar.gz tar zxvf openresty-1.15.8. ...

  5. Filebeat Nginx Module 自定义字段

    Filebeat Nginx Module 自定义字段 一.修改/usr/local/nginx/conf/nginx.conf中 log_format access '$remote_addr - ...

  6. Nginx research, nginx module development

    catalog . 初探nginx架构 . handler模块 . Nginx编译.安装.配置 . Hello World模块开发 1. 初探nginx架构 nginx在启动后,在unix系统中会以d ...

  7. openresty HTTP status constants nginx api for lua

    https://github.com/openresty/lua-nginx-module context: init_by_lua, set_by_lua, rewrite_by_lua, acce ...

  8. openresty安装lua和nginx相关

    server{ listen ; server_name a.com; index index.php; root /usr/share/nginx/html; location / { if (!- ...

  9. FastDFS+Nginx+Module

    1.安装libevent wget  https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.g ...

  10. Nginx+lua+openresty精简系列

    1. CentOS系统安装openresty 你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令).运行下面的 ...

随机推荐

  1. git推送代码Gogs报401错误

    1.git push 报错:RPC failed; HTTP 401 curl 22 The requested URL returned error: 401 The remote end hung ...

  2. Jerry带您了解Restful ABAP Programming模型系列之二:Action和Validation的实现

    相信通过Jerry的前一篇文章 30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用,想必大家对Restful ABAP Programming模型已经有 ...

  3. nginx的gzip模块详解以及配置

    文章来源 运维公会:nginx的gzip模块详解以及配置   1.gzip模块作用 gzip这个模块无论在测试环境还是生产环境都是必须要开启,这个模块能高效的将页面的内容,无论是html或者css.j ...

  4. Yarn介绍(设计理念与基本架构)

    Yarn : 新的计算框架,是一个全局资源管理器,负责整个集群的资源管理和分配   一. Yarn产生背景       Hadoop1.0MR有局限性,概括为以下几个方面 :  扩展性差 可靠性差 资 ...

  5. 阿里P7架构师是如何解决跨域问题的!你有遇到吗?

    现在越来越多的项目就算是一个管理后端也偏向于使用前后端分离的部署方式去做,为了顺应时代的潮流,一前后端分离就产生了跨域问题,所以许多同学把跨域和前后端分离项目联系在了一起,其实跨域产生的原因并不是前后 ...

  6. 用js刷剑指offer(数组中出现次数超过一半的数字)

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  7. <<构建之法>>--第二次作业

    GIT地址 https://github.com/Panghu98/AchaoCalculator.git GIT用户名 Panghu98 学号后五位 62632 博客地址 https://www.c ...

  8. es6 class extends

    Class和普通构造函数有何区别   JS构造函数 function MathHandle(x, y){ this.x = x; this.y = y; } MathHandle.prototype. ...

  9. Three.js入门详解

    什么是WebGL   WebGL(Web 图形库)是一种 JavaScript API,用于在任何兼容的 Web 浏览器中呈现交互式 3D 和 2D 图形,而无需使用插件.WebGL 通过引入一个与 ...

  10. Web前端 --- BOM和DOM

    目录 前端基础之BOM和DOM BOM DOM 前端基础之BOM和DOM BOM是指浏览器对象模型,他使JavaScript有能力与浏览器进行对话 DOM是指文档对象模型,通过它,可以访问HTML文档 ...