Nginx:尝试将所有网址与foo匹配。

时间:2016-01-18 12:42:53

标签: regex nginx

我正在尝试匹配这些,以便它们代理到另一个URL。总会出现一些事情,但事情并非总是如此。

  1. www.xyz.com/foo/ - > www.abc.com/foo /
  2. www.xyz.com/bar-foo/ - > www.abc.com/bar-foo /
  3. www.xyz.com/bar-foo-this/ - > www.abc.com/bar-foo-this /
  4. 我知道我可以为每个案例使用新规则,但是,如果可能的话,我试图找到一种通用方法。

    我现在有这个:

    location ^~ /([^/]*foo[^/]+)/ {     
        proxy_pass https://localhost:8080/$1;
    }
    

1 个答案:

答案 0 :(得分:0)

我有这个正则表达式:

([^\/]*foo[^\/]*)

Live Demo

阐释:

1st Capturing group ([^\/]*foo[^\/]*)
    [^\/]* match a single character not present in the list below
        Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        \/ matches the character / literally
    foo matches the characters foo literally (case sensitive)
    [^\/]* match a single character not present in the list below
        Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        \/ matches the character / literally
g modifier: global. All matches (don't return on first match)