How to configure Nginx to split traffic based on the last segment of the request IP

How to configure Nginx to split traffic based on the last segment of the request IP

It is mainly the configuration jump of the if judgment in the location parameter. Diversion can reduce the load and pressure of the server. This is a very common server deployment architecture.
Jump according to the range of the last segment of the IP

#Domain name, IP, port and other information please modify it yourself upstream huaji-01.com {
 server 192.168.1.100:8080;
}

upstream huaji-02.com {
 server 192.168.1.200:8080;
}

server {

 listen 80;
 server_name www.huaji.com;

 location /
 {
  if ( $remote_addr ~* ^(.*)\.(.*)\.(.*)\.[1,125]$) {
    proxy_pass http://huaji-01.com;
    break;
  }

  proxy_pass http://huaji-02.com;
 }

}

The above example forwards requests with IP addresses ending in 1-125 to huaji-01.com, and others to huaji-02.com. You can also modify the regular expression, for example

Rule: $remote_addr ~* ^(.*)\.(.*)\.(.*)\.*[0268]$ Even-numbered IPs ending with 0268 will jump to huaji-01.com, and others will jump to the second domain name;

Rule: $remote_addr ~* ^(112)\.(.*)\.(.*)\.(.*)$ IPs starting with 112 will be redirected to the specified domain name;

Rule: $http_x_forwarded_for ~* ^(112)\.(.*)\.(.*)\.(.*)$ Divert traffic based on the forward address segment, and redirect traffic starting with 112 to the specified domain name

The meaning of if instruction conditional judgment:

Regular expression matching where:

~ for case-sensitive matching

~* is case-insensitive matching

!~ and !~* are case-sensitive and case-insensitive matches respectively.

File and directory matching, where:

-f and !-f are used to determine whether a file exists

-d and !-d are used to determine whether a directory exists

-e and !-e are used to determine whether a file or directory exists

-x and !-x are used to determine whether the file is executable

The last parameter of the rewrite directive is the flag tag, which includes:

last is equivalent to the [L] flag in Apache, which means rewrite.

break After this rule is matched, the matching is terminated and the following rules will no longer be matched.

redirect returns a 302 temporary redirect, and the browser address will display the URL address after the jump.

permanent returns 301 permanent redirection, and the browser address will display the URL address after the jump.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Sample code for implementing IP access diversion through Nginx reverse proxy

<<:  Vue improves page response speed through lazy loading

>>:  MySQL Packet for query is too large problem and solution

Recommend

Problems encountered when uploading images using axios in Vue

Table of contents What is FormData? A practical e...

Singleton design pattern in JavaScript

Table of contents 1. What is a design pattern? 2....

Jmeter connects to the database process diagram

1. Download the MySQL jdbc driver (mysql-connecto...

MySQL full-text search usage examples

Table of contents 1. Environmental Preparation 2....

Let's talk about the size and length limits of various objects in MySQL

Table of contents Identifier length limit Length ...

Analysis of the operating principle and implementation process of Docker Hub

Similar to the code hosting service provided by G...

Windows Server 2019 Install (Graphical Tutorial)

Windows Server 2019 is the latest server operatin...

Method of using MySQL system database for performance load diagnosis

A master once said that you should know the datab...

MySQL 5.6.33 installation and configuration tutorial under Linux

This tutorial shares the installation and configu...

Docker installs Redis and introduces the visual client for operation

1 Introduction Redis is a high-performance NoSQL ...

MySQL Optimization Solution Reference

Problems that may arise from optimization Optimiz...

Solution to VMware virtual machine no network

Table of contents 1. Problem Description 2. Probl...

SQL implementation of LeetCode (197. Rising temperature)

[LeetCode] 197.Rising Temperature Given a Weather...