简体   繁体   English

如何调试和修复nginx错误位置重定向问题?

[英]How to debug and fix nginx wrong location redirection problem?

I have an Nginx config file list below.我在下面有一个 Nginx 配置文件列表。 I want to send the request to different server base on Refer.我想根据引用将请求发送到不同的服务器。

When I send a request with URL "doamin.com/capi/a/b" and refer "a.com/a/1/test", everything is good, server "be" will get "be/a/b" request.当我发送带有 URL“doamin.com/capi/a/b”的请求并引用“a.com/a/1/test”时,一切都很好,服务器“be”将收到“be/a/b”请求. But if I send a request with URL "doamin.com/capi/a/b" and refer "a.com/a/0/test", server "be_demo" will get "be_demo/" request, the path "a/b" is missing.但是如果我发送一个带有 URL“doamin.com/capi/a/b”的请求并引用“a.com/a/0/test”,服务器“be_demo”将得到“be_demo/”请求,路径“a/ b”不见了。

I've tried to add "/" at the end of "be_demo", it doesn't work.我试图在“be_demo”的末尾添加“/”,它不起作用。

map $http_referer $be_pool {                                                                                                                                                                                                                                                                                                                                       
        default                  be;                                                                                                                                                                                                                                                                                                                                   
        "~a\.com\/.*\/0\/.*"       be_demo;                                                                                                                                                                                                                                                                                                                
    } 
    server {
        ...
        location ~ ^/capi/(.*)$ {                                                                                                                                                                                                                                                                                                                                               
            proxy_pass http://$be_pool/$1;                                                                                                                                                                                                                                                                                                                             
        } 
    }

Thanks.谢谢。

The numeric capture $1 is set by the last regular expression to be evaluated.数字捕获$1由要评估的最后一个正则表达式设置。 In the second case, the regular expression in the map statement is evaluated after the regular expression in the location statement.在第二种情况下, map语句中的正则表达式在location语句中的正则表达式之后计算。

The solution is to use a named capture instead.解决方案是改用命名捕获。

For example:例如:

map $http_referer $be_pool {                                                                                                                                                                                                                                                                                                                                       
    default                  be;                                                                                                                                                                                                                                                                                                                                   
    "~a\.com\/.*\/0\/.*"     be_demo;                                                                                                                                                                                                                                                                                                                
} 
server {
    ...
    location ~ ^/capi/(?<myuri>.*)$ {                                                                                                                                                                                                                                                                                                                                               
        proxy_pass http://$be_pool/$myuri;                                                                                                                                                                                                                                                                                                                             
    } 
}

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

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