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

WBOY
Release: 2023-05-18 10:10:34
forward
1151 people have browsed it

Mainly the configuration jump of the if judgment in the location parameter. Offloading can reduce the load and pressure of the server. This is a very common server deployment architecture.
Jump based on the range of the last IP segment

#域名,ip,端口等信息请自行修改 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; } }
Copy after login

The above is to forward the request source with the last IP segment 1-125 to huaji-01.com, and other requests to huaji-02.com, the same can be done Modify the regular rules, such as

rules: $remote_addr ~* ^(.*)\.(.*)\.(.*)\.*[0268]$ The end is an even number ip like 0268, jump Go to huaji-01.com, others jump to the second domain name;

Rule: $remote_addr ~* ^(112)\.(.*)\.(.*)\.(.*) $ IP starting with 112 jumps to the specified domain name;

Rule: $http_x_forwarded_for ~* ^(112)\.(.*)\.(.*)\.(.*)$ According to forward Address segment to divert traffic, jump to the specified domain name starting with 112

The meaning of if instruction condition judgment:

Regular expression matching, among which:

~ is case-sensitive matching

~* is case-insensitive matching

!~ and !~* are case-sensitive mismatching and case-insensitive mismatching respectively

File and directory matching, where:

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

-d and !-d are used to determine Whether the directory exists

-e and !-e are used to determine whether the 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. The flag tag is:

last, which is equivalent to the [l] tag in apache, indicating rewrite.

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

redirect returns 302 temporary redirection, and the browser address will display the url address after the jump.

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


The above is the detailed content of How to configure Nginx to divert traffic based on the last segment of the request IP. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!