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

VMware virtual machine installation Apple Mac OS super detailed tutorial

Table of contents Summarize Sometimes we need to ...

Example of using docker compose to build a consul cluster environment

Basic concepts of consul Server mode and client m...

Summary of Vue3 combined with TypeScript project development practice

Table of contents Overview 1. Compositon API 1. W...

After reading the introduction of CSS box model, you will not be confused

The property names often heard in web design: con...

Linux ssh server configuration code example

Use the following terminal command to install the...

Summary of common commands for Linux user and group management

This article summarizes the common commands for L...

JS realizes the calculation of the total price of goods in the shopping cart

JS calculates the total price of goods in the sho...

Detailed method of using goaccess to analyze nginx logs

Recently I want to use goaccess to analyze nginx ...

Causes and solutions for cross-domain issues in Ajax requests

Table of contents 1. How is cross-domain formed? ...

HTML mouse css control

Generally speaking, the mouse is displayed as an u...

The difference between docker run and start

The difference between run and start in docker Do...