nginx强大的替换模块sregex的replace-filter-nginx-module
nginx内容替换模块我知道的有3个:
- http_sub_module (nginx内置):非常好用,但是只能是普通字符串替换,无法正则替换。
- ngx_http_substitutions_filter_module (第三方):只尝试了30分钟,但是这30分钟很折磨,我本以为它是强大的,但事实上它只不过在同一行内容中进行不完全的正则匹配替换,连换行都无法匹配,这种很大程度上失去了正则的色彩,基本没啥大前途。
- replace-filter-nginx-module (第三方,
推荐
):完全的正则匹配,接下来整理了下这个模块的安装过程。
安装
因为博主是非源码安装的nginx,所以使用源码安装这个模块的时候注意下,但是相差不多。
安装此模块需要先安装sregex
运行库。
- 提前准备好环境,需要用到下面的底层库,如果直接下载源码则不用安装git
apt update
apt install git make gcc -y
- 安装
sregex
cd /usr/local/src
git clone https://github.com/agentzh/sregex
cd sregex
make
make install
- 下载nginx对应版本的源码
# V是大写的
nginx -V
下载对应的源码包
cd /usr/local/src
# nginx源码
wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar zxvf nginx-1.22.0.tar.gz
cd nginx-1.22.0
# replace-filter-nginx-module源码
git clone https://github.com/agentzh/replace-filter-nginx-module
- 重新编译nginx并安装
replace-filter-nginx-module
# 把新模块加入nginx编译命令(--add-module=/usr/local/src/nginx-1.22.0/replace-filter-nginx-module)
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/usr/local/src/nginx-1.22.0/replace-filter-nginx-module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
# 编译(最好不要直接make install,避免出问题)
make -j8
# 以上完成后,会在objs目录下生成一个nginx文件,先验证:
./nginx -t
# 查看新的模块是否已经开启
./nginx -V
如果在测试nginx阶段报错 ./objs/nginx: error while loading shared libraries: libsregex.so.0: cannot open shared object file: No such file or directory
则需要进行类库地址关联,因为我们明明已经成功安装了libsregex.so.0
从上图可以看到nginx只从/usr/lib64路径下寻找,那么我们将类库直接复制过去就行
cp /usr/local/lib/libsregex.so.0 /usr/lib64
- 替换nginx
mv /usr/sbin/nginx /usr/sbin/nginx.bak
cp objs/nginx /usr/sbin
- 重新启动nginx (不要reload)
service nginx restart
- 用法示例(博主已经在用,一瞬间发现,这个就是我需要的那个功能)
replace_filter_types text/css application/x-javascript application/javascript text/xml text/plain;
replace_filter '<div class="big\s+img">.*\n</table></div>' '' g;
模块使用
支持以下Perl 5正则表达式语法特性
^ 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
支持以下转义序列
\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
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭