繁体   English   中英

从位置和重写中使用正则表达式捕获组?

[英]Using regex capture groups from both the location and rewrite?

给出一些例子,例如:

location ~ ^/([\d]+)/ {
    rewrite ^/[\d]+/([\d]+)/(.*)$ #replacement
    
    proxy_pass https://httpbin.org/anything/$1/$2/$3 # ?

}

捕获组是什么? nginx 会取代它们吗? 合并他们? 在那种情况下我会有 3 个捕获组吗? 顺序是什么?

我不清楚如何引用上面 3 个捕获组中的每一个。

我引用捕获组的最后一个地方是在 proxy_pass 中。

每次 Nginx 计算另一个正则表达式时,数字捕获都会重置。

在评估proxy_pass语句时,从location表达式捕获的数字丢失了。 只定义$1$2 ,它们取自rewrite表达式。

要使捕获跨多个正则表达式持续存在,请使用(?<name>...)语法为其命名。

例如:

location ~ ^/(?<name>[\d]+)/ {
    rewrite ^/[\d]+/([\d]+)/(.*)$ ...;        
    proxy_pass https://httpbin.org/anything/$name/$1/$2;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM