In the http {} section,
is setmap $http_header_1 $route {default 10;}
map $http_header_2 $route {default 20;}
map $http_header_3 $route {default 30;}
The requested header is set to
curl -H "header_1:0" http://xxx.com/xxx
Then in the server {} section,
echo "$route";
The printed result is always 30, because the third map instruction overwrites the previous value;
The question is: How to get the desired value based on different headers in the map part?
To solve problem 1, the way I think is
map $http_header_1 $route1 {default 10;}
map $http_header_2 $route2 {default 20;}
map $http_header_3 $route3 {default 30;}
Then in the location {} section, use
if ($http_header_1) {
echo "$route1";
}
To get the desired value; Shamefully, this if seems to have failed, and nginx does not recommend using if
Okay, I’ve done it with if, but nginx doesn’t recommend using if. Does anyone have a better solution?
@冰风 that person, it seems that the text to reply to you cannot use md format, I will reply to you here:
Thank you. However, your answer did not answer my question.
My business needs are:
That is, the request has and only one of $httpheader1, $httpheader2, $httpheader3, and has a corresponding value of one of a, b, and c.
nginx needs to determine the key of the header and return a confirmed $route value based on the value corresponding to the key.
Business Example:
最终$route为 111, 并 fastcgi_pass localhost:111
最终$route为 212, 并 fastcgi_pass localhost:212
最终$route为 313, 并 fastcgi_pass localhost:313
Then
In the end, there are 9 business processes in total, each listening to a port.
Don’t ask me why I have such a strange demand, it’s my hidden merit and fame~~~
So, your answer cannot satisfy my animal desires
The host needs to understand a few things: 1. The statements in the map instruction must execute 1 of N by default.
is equivalent to
So, the final outgoing $route is 30;
map is an instruction to establish the mapping relationship between multiple incoming values and outgoing values. It is best that the incoming value has been defined and assigned before being passed to the map, otherwise default will be executed directly.
The original poster can give it a try