search

Home  >  Q&A  >  body text

nginx conf setting problem

1

In the http {} section,

is set
map $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?

2

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


So, which classmate who has deep knowledge of merit and fame can help answer the question?

PHP中文网PHP中文网2738 days ago652

reply all(2)I'll reply

  • 阿神

    阿神2017-05-16 17:31:03

    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:

    map $http_header_1 $route {
    default 110;
    a       111;
    b       112;
    c       113;
    }
    map $http_header_2 $route {
    default 210;
    a       211;
    b       212;
    c       213;
    }
    map $http_header_3 $route {
    default 310;
    a       311;
    b       312;
    c       313;
    }
    

    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:

    1. curl -H "header_php:a" http://biz.domain.com/ 最终$route为 111, 并 fastcgi_pass localhost:111
    2. curl -H "header_python:b" http://biz.domain.com/ 最终$route为 212, 并 fastcgi_pass localhost:212
    3. curl -H "header_ruby:c" http://biz.domain.com/ 最终$route为 313, 并 fastcgi_pass localhost:313

    Then

    1. php-1 monitors 111, php-2 monitors 112, php-3 monitors 113
    2. python-1 monitors 211, python-2 monitors 212, python-3 monitors 213
    3. ruby-1 listens to 311, ruby-2 listens to 312, ruby-3 listens to 313

    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

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 17:31:03

    The host needs to understand a few things: 1. The statements in the map instruction must execute 1 of N by default.

    map $http_header_1 $route {default 10;}
    map $http_header_2 $route {default 20;}
    map $http_header_3 $route {default 30;}
    

    is equivalent to

    传入$http_header_1任意值(即使为null),返回$route=10;
    传入$http_header_2任意值(即使为null),返回$route=20;
    传入$http_header_3任意值(即使为null),返回$route=30;
    

    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

        map $http_user_agent $route{
           "Baiduspider" 1; #当$http_user_agent为Baiduspider,返回$route = 1
           "Googlebot" 2; #当$http_user_agent为Googlebot,返回$route = 2
           default 0; #否则,返回$route = 0
        }
    

    reply
    0
  • Cancelreply