2

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.

1 Answer 1

1

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;                                                                                                                                                                                                                                                                                                                             
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.