6

I'm looking for a way to reroute all requests that have set an account-id in the HTTP header Account-ID with the last two digits (of a ten digit number) 00 to 05 (to touch only 5% of the total traffic). In addition, if a request has set the HTTP header Server-A, that request should be forwarded to that server regardless of the set account-id. Otherwise and by default, all traffic should be handled by server-b. My current location-rule looks like this:

location /rest/accounts/ {
  if ( $http_account_id ~ '([\d]{8})(0[0-4])') {
    proxy_pass http://server-a.company.com;
  }

  if ( $http_server_a = true) {
    proxy_pass http://server-a.company.com;
  }

  proxy_pass http://server-b.company.com;
}

As I read in the official documents here, if is considered evil.

Is there a better solution for my approach which isn't considered evil?

1 Answer 1

6

You can actually chain map directives a which would make it cleaner. For example:

map $http_server_a $server_a_check {
    default                 "http://server-b.company.com";
    ""                      "http://server-a.company.com";
}

map $http_account $account_check{
    default                 $server_a_check;
    "~([\d]{8})(0[0-4])"    "http://server-a.company.com";
}

server {
    ....
    location / {
        proxy_pass          $account_check;
    }
}

So proxy_pass is getting its value from $account_check which does a check against Account header with a regex check. Then if none of the variants pass their checks, the default gets its value from the result of $server_a_check which looks for Server-A header with no data for the value as the question didn't state what the accepted values would be.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, will give it a try and accept as soon as possible.
I accept the answer although I get a "502 Bad Gateway" if I enter https://server-a instead of http://server-a...
What if I need to set two variables at once?

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.