找回密码
 立即注册
查看: 9219|回复: 0

可替换多行的nginx反代替换模块

[复制链接]
发表于 2015-10-31 09:41:01 | 显示全部楼层 |阅读模式
比ngx_http_substitutions_filter_module 更强大的替换模块:replace-filter-nginx-module/ O7 W& R7 B: K+ X
这家伙能替换多行文本!
" ]) d, n3 h; P& v4 O" u
/ c# L. V/ v/ O- W5 @- V下面只翻译一下,再加个安装教程,因为我自己也没弄懂怎样玩= =8 k1 ?* s- T* F: q- R" ]5 O

; E* W3 L' \6 ~; n2 r; ` 1.安装此模块需要先安装sregex运行库2 v% J: r2 c0 Y) i$ q& Z6 Y

6 n4 u; R1 ~9 M" e# l[mw_shl_code=applescript,true]apt-get update;apt-get install git make gcc -y  #Centos改成yum, @5 E, v! }6 k" Q" x8 ~' n$ \1 e) x
git clone https://github.com/agentzh/sregex
, w" s  _) ^; Pmake/ `* T7 i, _4 z7 k5 j
make install
- Y) T: n, v* O" acd ..2 i+ Y/ A8 b! ?6 Z& q
git clone https://github.com/agentzh/replace-filter-nginx-module
1 q' h! K# u- v6 M+ I' h, Uwget http://nginx.org/download/nginx-1.2.6.tar.gz
% G- W9 i) x% X7 N7 \3 ?tar zxvf nginx-1.2.6.tar.gz
3 C- k2 x( P( G+ }* L& f  r% hcd nginx-1.2.6. }8 q; |! V) \9 s8 n7 W
./configure --add-module=../replace-filter-nginx-module  #自行加其他编译参数+ K  Q2 B# a2 F- J; w
make
  T6 C: @, j* H8 Qmake install[/mw_shl_code]
3 N8 f  d$ S8 {* p  rnginx.conf的用法举例:
# ^, w5 h6 d  ~[mw_shl_code=applescript,true]location /t {  ?! z& K2 e# s2 j% f+ y; |: e
    default_type text/html;
9 y0 M5 c% h" `& u# H7 @% X    echo abc;3 P3 w, _3 @* J2 s
    replace_filter 'ab|abc' X;8 C; x/ [6 l; K3 P/ v2 `3 M
}
& G4 U7 ?' _, L/ [6 S
2 J5 i$ a% j* k2 klocation / {
; q6 `2 R* j' a, E, W$ i; M- F6 W7 Q$ q    # proxy_pass/fastcgi_pass/...
. I% B( l+ V& {/ \) Y9 X* g! h% }
& y, Q2 ~7 K0 P9 c- P    # caseless global substitution:; B( z; T- s0 I: e
    replace_filter '\d+' 'blah blah' 'ig';
* _1 u2 l" R7 e4 X+ Q" x    replace_filter_types text/plain text/css;* f+ x4 N# Y( Y- C  R
}[/mw_shl_code]
& q( k0 M1 t4 Z$ c  I: a, ^, USyntax语法:8 t8 V8 V% `7 N& ^1 D4 ]
[mw_shl_code=applescript,true]^             匹配起始行数: f2 j3 o, K! u5 ]) r
$             匹配末尾行数
# J& ?( V# Z; T; M2 O( j. H3 ~6 S) l* Z
\A match only at beginning of stream
5 h8 H+ Z3 |& {: W( {% _\z match only at end of stream9 j. R: S) r1 b: {# f; a

- D) r$ S! O! z1 j" V\b match a word boundary* H! Z6 g: h1 ?- v
\B match except at a word boundary
4 k8 z4 _9 d2 O9 S  f; v% ^4 ?1 M
. G2 w, M3 w; H, [& H2 {. match any char; a$ x  E* R3 j9 y0 h% \2 T
\C match a single C-language char (octet)
8 l3 y  j$ o  _8 e+ o: h! E. {
  V" M% ?+ ?2 d! K: w9 ], b[ab0-9] character classes (positive)
; G3 \# D9 g' f; N4 W[^ab0-9] character classes (negative)4 P" t# s9 `" b' v# \; c0 a

/ B' x1 |- r- Z) Y$ S\d match a digit character ([0-9])
" A; j- t0 d" r( h5 e\D match a non-digit character ([^0-9])
- q4 _, K% c! t9 H
& O6 h: f( s% r& i0 Y- h. J' _3 j\s match a whitespace character ([ \f\n\r\t])) h" {! ~+ ~8 t1 r
\S match a non-whitespace character ([^ \f\n\r\t])
7 V# s; ?) ]' w1 F: _6 z  F8 q
  _0 I& H6 C5 O, ?( a6 Z. A: U\h match a horizontal whitespace character
2 u9 n. I' J& H8 J2 G* [' u\H match a character that isn't horizontal whitespace
& G* U7 c) O% Y+ i3 @* F0 L4 z8 `" U8 e- _6 Q
\v match a vertical whitespace character
- d4 J% z% e5 [  o\V match a character that isn't vertical whitespace
0 E5 S  E; c% O/ Y: B6 n  z/ B% x: H
\w match a "word" character ([A-Za-z0-9_])2 N9 d& {  l! k9 L" k! \
\W match a non-"word" character ([^A-Za-z0-9_])
4 n3 D! K* R3 Q( u
- }6 O$ E' V8 s) b/ G3 e0 A( s! Y8 v6 |\cK control char (example: VT)( I) Y) l; F" I2 v/ l) ?' J
2 c4 \, y# P- c8 j" U9 m# z* A
\N match a character that isn't a newline
  b8 z  M% X1 t0 Y' q
% h; m) {6 y# Vab concatenation; first match a, and then b
2 I& ~# V. u7 Va|b alternation; match a or b
2 u4 Y! I  L) y; `% y( S- o. W7 d- f) p# k8 [* |4 T4 ]' U
(a) capturing parentheses4 d5 y! q- V- V/ E2 V
(?:a) non-capturing parantheses
6 U8 t7 n( g  l$ Y) ~- G6 {9 o/ R3 c
a? match 1 or 0 times, greedily
# w: N& `6 r8 }5 _a* match 0 or more times, greedily
9 m/ [4 B7 ?! h) w' \9 q) Ua+ match 1 or more times, greedily, J% ~7 s" m' G" \0 |( n

! w" ~4 R. J( E2 e- b9 O% H8 ta?? match 1 or 0 times, not greedily
6 d8 v) U. Z5 n& C6 ^a*? match 0 or more times, not greedily" X+ W! s7 c5 N& J" y
a+? match 1 or more times, not greedily
$ y- i- w% T; f* f) X( j" j
5 t& S! F1 Q. v2 M- R6 [a{n} match exactly n times7 i$ D, P0 V! t7 M6 ?4 j% z7 \
a{n,m} match at least n but not more than m times, greedily+ c( S7 [0 A3 r
a{n,} match at least n times, greedily
$ d3 ~9 C  T$ I- X! V( w2 [4 C& A4 B1 p- ]4 n
a{n}? match exactly n times, not greedily (redundant)( V8 O; L- `" G* R! m( E/ ^
a{n,m}? match at least n but not more than m times, not greedily9 ?7 B  }- ?5 O. B3 B7 C/ L* {
a{n,}? match at least n times, not greedily[/mw_shl_code]7 B6 K6 V1 _0 C! @( F
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2024-4-25 02:30 , Processed in 0.060801 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表