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



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

* 判断值是否为空

```bash
if ( $arg_code = '' )
```

* 设置变量

```bash
set $spp http://127.0.0.1:38002;
```

* 获取参数

```bash
$host:$server_port #host和port参数

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

* 实例：

```bash
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";
    }
```
