使用websocket推送数据报错: websocket: the client is not using the websocket protocol: ‘upgrade’ token not found in ‘Connection’ header

159 次浏览次阅读
没有评论

使用 websocket 推送数据报错: websocket: the client is not using the websocket protocol: ‘upgrade’ token not found in ‘Connection’ header

错误原因:因为使用了 nginx 配置了代理,需要在配置文件中加上 Upgrade

配置 nginx

打开项目的 nginx 配置文件,账号找到 server 或 location 块,然后添加或修改 Access-Control-Allow-Headers 设置,确保它允许 Upgrade 头。

eg:

location /api_socket {
        proxy_set_header  X-Real-IP  $remote_addr;
        client_max_body_size 100M;
        proxy_pass http://your_backend_server; # 代理地址

        # 要加的 upgrade
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        add_header 'Access-Control-Allow-Origin' '*';
        add_header Access-Control-Allow-Credentials 'true';
        add_header Access-Control-Allow-Methods 'POST, GET, PUT, OPTIONS, DELETE, PATCH';
        add_header Access-Control-Allow-Headers 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, Upgrade;
        if ($request_method = 'OPTIONS') {return 200;}
   }
正文完
 0
评论(没有评论)