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

王林
Release: 2023-05-12 22:01:12
forward
1499 people have browsed it

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;
  }
}
Copy after login

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;
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template