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

Nginx负载均衡Selenium Grid遇/ui路径浏览器连接故障排查

问题分析与解决

你的Nginx配置缺少WebSocket协议转发支持,这是导致浏览器连接故障的核心原因。Selenium Grid在创建WebDriver会话后,浏览器与Grid节点之间会通过WebSocket进行实时通信(比如页面操作、日志传输),默认的代理配置没有处理WebSocket的升级请求,导致连接中断。

修正后的Nginx配置

upstream selenium_grids {
    server <grid1_ip>:<grid1_port>;
    server <grid2_ip>:<grid2_port>;
    # 可选:如果需要会话粘性(同一测试会话固定到同一Grid),添加ip_hash
    # ip_hash;
}

server {
    listen 80;
    server_name <nginxIP>;

    location / {
        proxy_pass http://selenium_grids;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # 新增WebSocket支持配置
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        # 延长超时时间,避免长连接被Nginx主动断开
        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
    }
}

关键补充说明

  • proxy_http_version 1.1:WebSocket依赖HTTP/1.1协议的Upgrade机制,必须显式指定。
  • Upgrade和Connection头:告诉Nginx将WebSocket升级请求转发到后端Grid,而不是当作普通HTTP请求处理。
  • 超长超时时间:Selenium测试会话可能持续较长时间,默认Nginx超时(一般几分钟)会主动断开连接,设置7天超时可以适配大多数测试场景。
  • 可选ip_hash:如果你的测试需要同一个客户端的会话固定到同一台Grid Hub(避免跨Hub的会话状态不一致),可以在upstream块添加该指令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 01:27:08