user2538425
user2538425

Reputation: 65

How to debug and fix nginx wrong location redirection problem?

I have an Nginx config file list below. 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. 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.

I've tried to add "/" at the end of "be_demo", it doesn't work.

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

Thanks.

Upvotes: 2

Views: 1162

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

The numeric capture $1 is set by the last regular expression to be evaluated. In the second case, the regular expression in the map statement is evaluated after the regular expression in the location statement.

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;                                                                                                                                                                                                                                                                                                                             
    } 
}

Upvotes: 1

Related Questions