V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
fantasts
V2EX  ›  NGINX

nginx 反代模块 sub_filter 如何替换单引号

  •  1
     
  •   fantasts · 2016-02-19 00:27:31 +08:00 · 8313 次点击
    这是一个创建于 2983 天前的主题,其中的信息可能已经有所发展或是发生改变。

    心好累,写出来不是通不过 nginx 的检测就是没效果。
    我想替换的内容里带有单引号',例如:'<img src=http'
    我先这么尝试 subfilter '<img src=http' ''<img src=https'' //错误<
    尝试改成正则 sub_filter '(.)img src=http' ''$1img src=https'’ r; //错误$
    _(:з」∠)
    不是支持正则么为啥$都错误了

    13 条回复    2018-05-09 13:18:58 +08:00
    rrfeng
        1
    rrfeng  
       2016-02-19 00:42:19 +08:00
    知道单引号有问题为撒用正则屏蔽了 < ……
    rrfeng
        2
    rrfeng  
       2016-02-19 00:43:30 +08:00
    哪里有单引号了……
    xuhaoyangx
        3
    xuhaoyangx  
       2016-02-19 00:46:12 +08:00
    还有其他一些在后边章节中有特殊用处的标点符号,在前面加 "\" 后,就代表该符号本身。比如:^, $ 都有特殊意义,如果要想匹配字符串中 "^" 和 "$" 字符,则表达式就需要写成 "\^" 和 "\$"。
    来自复制粘贴
    fantasts
        4
    fantasts  
    OP
       2016-02-19 02:26:43 +08:00
    @rrfeng
    @xuhaoyangx
    就算想植入一段统计代码都不知如何是好
    fantasts
        5
    fantasts  
    OP
       2016-02-19 02:32:21 +08:00
    @fantasts 大概有点明白了,大概解决了。
    demo
        6
    demo  
       2016-02-19 09:45:58 +08:00
    \'
    popok
        7
    popok  
       2016-02-19 09:48:56 +08:00
    @xuhaoyangx 这叫转义
    然而正则里面单引号是不需要转义的。

    还有 LZ 发帖把问题说清楚点
    xuhaoyangx
        8
    xuhaoyangx  
       2016-02-19 11:36:32 +08:00
    @popok 我知道转义
    fantasts
        9
    fantasts  
    OP
       2016-02-19 16:25:11 +08:00
    @popok
    不知道咋整
    例如:
    sub_filter '<div.*<div.*<iframe.*</iframe></div><div class="content">' '<div class="content">' r;
    报错:
    nginx: [emerg] invalid number of arguments in "sub_filter" directive in

    如何才能正确的使用正则而不报错呢。。。
    fantasts
        10
    fantasts  
    OP
       2016-02-19 17:28:01 +08:00
    @rrfeng
    @xuhaoyangx
    @demo
    @popok
    原来 sub_filter 不能使用正则替换,支持正则的是另外一个模块 subs_filter
    xuhaoyangx
        11
    xuhaoyangx  
       2016-02-19 17:55:26 +08:00
    @fantasts 那個需要自己 add module
    jalena
        12
    jalena  
       2016-02-19 19:27:08 +08:00
    用转义符转义啊 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    liuyinltemp
        13
    liuyinltemp  
       2018-05-09 13:18:58 +08:00
    @fantasts 这个正则怎么写?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2205 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 00:36 · PVG 08:36 · LAX 17:36 · JFK 20:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.