Nginx 判断值是否为空 设置变量 获取参数


Nginx 判断值是否为空 设置变量 获取参数

判断值是否为空

1
if ( $arg_code = '' )

设置变量

1
set $spp http://127.0.0.1:38002;

获取参数

1
2
3
$host:$server_port #host和port参数

$arg_code # url里?后面code=xxxx里面的xxxx的值

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
server {
listen 80;
server_name xxx.com;

location / {
set $spp http://wsbackend;
if ( $arg_code ~* ^[0-3][0-9][0-9][0-9] ){
set $spp http://127.0.0.1:38001;
}
if ( $arg_code ~* ^[4-6][0-9][0-9][0-9] ){
set $spp http://127.0.0.1:38002;
}
if ( $arg_code ~* ^[7-9][0-9][0-9][0-9] ){
set $spp http://127.0.0.1:38003;
}
if ( $arg_code ~* ^1[0-3][0-9][0-9][0-9] ){
set $spp http://127.0.0.1:38001;
}
if ( $arg_code ~* ^1[4-6][0-9][0-9][0-9] ){
set $spp http://127.0.0.1:38002;
}
if ( $arg_code ~* ^1[7-9][0-9][0-9][0-9] ){
set $spp http://127.0.0.1:38003;
}
if ( $arg_code = '' ){ # 判断参数是否为空
set $spp http://127.0.0.1:38001;
}
proxy_pass $spp;
proxy_set_header Host $host:$server_port;
proxy_http_version 1.1;
proxy_connect_timeout 60s; #配置点1
proxy_read_timeout 3000s; #配置点2,如果没效,可以考虑这个时间配置长一点
proxy_send_timeout 120s; #配置点3
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}