websocket 服务器上连接404 ,failed: Error during WebSocket handshake: Unexpected response code: 404

本文介绍了解决WebSocket连接404问题的方法。通过在nginx配置文件中添加特定指令,如proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; 实现了服务器上的WebSocket正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 问题

websocket 连接404
本地可以访问websocket,但是部署到服务器上就不行,不过本地用的配置是vue.config.js,没走nginx代理。

服务器上是nginx代理

2 解决

 upstream abc-api {
        server 192.168.xx.xx:80;
    }

location /abc/socket/ {
            proxy_pass http://abc-api/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
                
        }

nginx里面必须写

  proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

以上就可以在外网访问服务器上面的websocket了;

http://www.wetools.com/websocket/
这个网站测试服务器上面的websocket能不能使用

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一写代码就开心

你的打赏将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值