Sample code to implement IP access shunting through Nginx reverse proxy
- 2020-05-14 06:00:18
- OfStack
This article introduces the sample code to implement IP access shunting through Nginx reverse proxy and shares it with you. The details are as follows:
It is a common server deployment architecture to use Nginx as a reverse proxy to realize triage to reduce the load and pressure on the server. In this article, we will share a method of how to shunt according to the route IP.
Shunt according to specific IP
Forward the last bit 0 or 2 or 6 of the last segment 1 of the IP address to test-01.com for execution, otherwise forward to test-02.com for execution.
upstream test-01.com {
server 192.168.1.100:8080;
}
upstream test-02.com {
server 192.168.1.200:8080;
}
server {
listen 80;
server_name www.test.com;
location / {
if ( $remote_addr ~* ^(.*)\.(.*)\.(.*)\.*[026]$){
proxy_pass http://test-01.com;
break;
}
proxy_pass http://test-02.com;
}
}
Forward the first 3 paragraphs of the IP address 192.168.202.* to test-01.com for execution, otherwise forward to test-02.com for execution.
upstream test-01.com {
server 192.168.1.100:8080;
}
upstream test-02.com {
server 192.168.1.200:8080;
}
server {
listen 80;
server_name www.test.com;
location / {
if ( $remote_addr ~* ^(192)\.(168)\.(202)\.(.*)$) {
proxy_pass http://test-01.com;
break;
}
proxy_pass http://test-02.com;
}
}
Shunt according to the specified range IP
Forward the last paragraph of the IP address 1-100 to test-01.com for execution, otherwise forward it to test-02.com for execution.
upstream test-01.com {
server 192.168.1.100:8080;
}
upstream test-02.com {
server 192.168.1.200:8080;
}
server {
listen 80;
server_name www.test.com;
location / {
if ( $remote_addr ~* ^(.*)\.(.*)\.(.*)\.[1,100]$){
proxy_pass http://test-01.com;
break;
}
proxy_pass http://test-02.com;
}
}
Shunted according to forwarded address
Forward the access beginning with 212 in paragraph 1 of the IP address to test-01.com for execution, otherwise forward to test-02.com for execution.
upstream test-01.com {
server 192.168.1.100:8080;
}
upstream test-02.com {
server 192.168.1.200:8080;
}
server {
listen 80;
server_name www.test.com;
location / {
if ( $http_x_forwarded_for ~* ^(212)\.(.*)\.(.*)\.(.*)$){
proxy_pass http://test-01.com;
break;
}
proxy_pass http://test-02.com;
}
}
The role of the if directive
The if directive: determines whether the value of the expression is true (true), and if so, executes the following curly braces.
Here are some common ways to compare conditional expressions:
A full comparison of variables can be made using = or! = operator Partial matches can be represented using a regular expression of ~ or ~* ~ means case sensitive ~* means case insensitive (nginx and Nginx are identical) ! ~ with! ~* is the reverse operation, that is, does not match Check if the file exists using -f or! - f operators Check if the directory exists using -d or! - d operators Check if a file, directory, or symbolic connection exists using -e or! - e operators Check if the file is executable using -x or! - x operators The partial matching of the regular expression can be done with parentheses, and the matching part can be replaced with the $1~$9 variable