You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure NGINXaaS配置A/B测试遇split_clients指令*符号报错求助

Azure NGINXaaS A/B测试配置报错解决方案

问题

在Azure NGINXaaS中配置A/B测试网关时,上传配置后触发错误:unknown directive "*" in /etc/nginx/nginx.conf:21。尝试将*替换为100%、调整配置位置均未解决问题,原配置如下:

http {
    # Set up a key‑value store to specify the percentage to send to each 
    # upstream group based on the 'Host' header.

    keyval_zone zone=split:64k state=/etc/nginx/state_files/split.json;
    keyval $host $split_level zone=split;

    upstream appversion1 {
        zone appversion1 64k;
        server 192.168.50.100;
        server 192.168.50.101;
    }

    upstream appversion2 {
        zone appversion2 64k;
        server 192.168.50.102;
        server 192.168.50.103;
    }

    split_clients $client_ip $split0 {
        *   appversion1;
    }
    split_clients $client_ip $split5 {
        5%  appversion2;
        *   appversion1;
    }
    split_clients $client_ip $split10 {
        10% appversion2;
        *   appversion1;
    }
    split_clients $client_ip $split25 {
        25% appversion2;
        *   appversion1;
    }
    split_clients $client_ip $split50 {
        50% appversion2;
        *   appversion1;
    }
    split_clients $client_ip $split100 {
        *   appversion2;
    }

    map $split_level $upstream {
        0        $split0;
        5        $split5;
        10       $split10;
        25       $split25;
        50       $split50;
        100      $split100;
        default  $split0;
    }

    server {
        listen 80;

        # In each 'split_clients' block above, '$client_ip' controls which 
        # application receives each request. For a production application, we set it
        # to '$remote_addr' (the client IP address). But when testing from just one 
        # client, '$remote_addr' is always the same; to get some randomness, we set 
        # it to '$request_id' instead.

        #set $client_ip $remote_addr; # Production
        set $client_ip $request_id; # Testing only

        location / {
            proxy_pass http://$upstream;
        }
    }
}

错误原因

Azure NGINXaaS基于NGINX Plus,但split_clients指令中不支持*作为剩余流量的通配符语法,必须明确指定百分比数值,且每个split_clients块内的百分比总和需为100%。

修复方案

将所有split_clients块中的*替换为对应剩余流量的百分比数值:

  • 仅匹配全部流量的块,使用100%
  • 已有指定百分比的块,剩余流量用100%减去已指定百分比的数值

修改后的完整配置

http {
    # 基于Host头配置键值存储,指定分流到各上游组的比例
    keyval_zone zone=split:64k state=/etc/nginx/state_files/split.json;
    keyval $host $split_level zone=split;

    upstream appversion1 {
        zone appversion1 64k;
        server 192.168.50.100;
        server 192.168.50.101;
    }

    upstream appversion2 {
        zone appversion2 64k;
        server 192.168.50.102;
        server 192.168.50.103;
    }

    split_clients $client_ip $split0 {
        100%   appversion1;
    }
    split_clients $client_ip $split5 {
        5%  appversion2;
        95%   appversion1;
    }
    split_clients $client_ip $split10 {
        10% appversion2;
        90%   appversion1;
    }
    split_clients $client_ip $split25 {
        25% appversion2;
        75%   appversion1;
    }
    split_clients $client_ip $split50 {
        50% appversion2;
        50%   appversion1;
    }
    split_clients $client_ip $split100 {
        100%   appversion2;
    }

    map $split_level $upstream {
        0        $split0;
        5        $split5;
        10       $split10;
        25       $split25;
        50       $split50;
        100      $split100;
        default  $split0;
    }

    server {
        listen 80;

        # 生产环境使用$remote_addr(客户端IP)作为分流依据,单客户端测试时用$request_id保证随机性
        #set $client_ip $remote_addr; # Production
        set $client_ip $request_id; # Testing only

        location / {
            proxy_pass http://$upstream;
        }
    }
}

内容的提问来源于stack exchange,提问作者Itamar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 13:04:54