Home >Operation and Maintenance >Nginx >How to implement nginx front-end distribution based on $remote_addr

How to implement nginx front-end distribution based on $remote_addr

王林
王林forward
2023-05-12 22:01:121534browse

The requirements are as follows:

There are multiple servers under the domain name. Now we are testing for a certain region, so that IP users in a certain region can only access a certain server and do the testing separately. , if there is no problem, update all; if there is a problem, the impact will be small, find the problem in time and solve the problem;

Solution:

Use the nginx module and load it on the front end On the balanced forwarding machine, configure the matching rules;

In the nginx configuration vhost, add a piece of code in the location section under the domain name

If $remote_addr matches the ip, forward it 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 add a section

#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 back-end server 192.168.20.10 for testing.

The above is the detailed content of How to implement nginx front-end distribution based on $remote_addr. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete