位置中的nginx RegEx-获取单个字母

位置中的nginx RegEx-获取单个字母,nginx,Nginx,我想捕获一个位置,并将前5个字母作为位置块中的变量,以便构建一条新路径 例如: location /direct/(.)(.)(.)(.)(.)(.*) { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_http_version 1.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_s

我想捕获一个位置,并将前5个字母作为位置块中的变量,以便构建一条新路径

例如:

location /direct/(.)(.)(.)(.)(.)(.*) {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:1234/$1$2$3$4$5$6; }
我该怎么做呢?

使用类似

location ~ "/direct/(?<myvar>.{5})(?<other_var>.*)" { use $myvar here }
location~“/direct/(?。{5})(?.*”{use$myvar here}

您可以匹配前5个字符并将其作为
$1
传递:

location ~ "^/direct/([\w]{5})(.*)" {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass http://localhost:1234/$1;
}

这将只传递前5个字符,因此
http://example.com/12345abcdefg
将被传递到
http://example.com:1234/12345
,从而删除原始URI中剩余的“abcdefg”

无效:nginx:[emerg]pcre_compile()失败:缺少“/direct/(?”中的)欢迎使用StackOverflow!请为您的代码添加一些解释:为什么它可以工作?它应该做什么?是的,但我需要它作为单个字符,而不是一个字符。