Detail the nginx front end according to the $remote_addr distribution method

  • 2020-05-17 07:51:34
  • OfStack

The requirements are as follows:

There are several servers under the domain name. Now we are doing tests for a certain region. Let ip users in a certain region only visit a certain server. If there is a problem, the impact is small, timely find the problem to solve the problem;

Solutions:

The module of nginx is used to configure the matching rules on the front-end load balancing and forwarding machine.

nginx configuration vhost inside, under the domain name location segment, add 1 piece of code

If $remote_addr matches ip, forward to abc_test_server;


server {
  listen    80;
  server_name abc.com.cn;
  access_log /dev/null;
  error_log /data/logs/error.log;
  
  location / {

  proxy_set_header  Host       $host;
  proxy_set_header  X-Real-IP    $remote_addr;
  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      if ($remote_addr ~ "202.96.134.100") 
       {
           proxy_pass http://abc_test_server;
            break;
        }
  proxy_pass http://abc_server;
  }
}

The load balancing configuration also needs to be increased by 1 segment


#abc_test only
upstream abc_test_server {
  server  192.168.20.10:80;
  
}

#abc.com.cn
upstream abc_server {
  server  192.168.20.11:80;
  server  192.168.20.12:80;
  server  192.168.20.13:80;
}

The set ip will be directly distributed to the one back-end server 192.168.20.10 for testing.


Related articles: