websocket 线上环境报错:Handshake failed due to invalid Upgrade header: null

一、问题描述:
公司有个项目用到了websocket,在本地环境测试没有问题,因为公司后台websocket是微服务搭建,我们需要nginx进行一层代理,结果出现如下错误

Handshake failed due to invalid Upgrade header: null

二、问题解决
1、首先找到自己nginx的配置文件—>配置下面代码 ——> nginx重启

location /consultation-websocket/ {
      proxy_pass http://127.0.0.1:8098/consultation-websocket/;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
 
      # For WebSocket upgrade header
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
}


2、其中最重要的是下面这三行

proxy_http_version 1.1;(告诉nginx使用HTTP/1.1通信协议,这是websoket必须要使用的协议)

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";(告诉nginx,当它想要使用WebSocket时,响应http升级请求)

三、配置展示

server {
        listen       8018 ssl ; 
        server_name  _;
	
        location /{
             root  /app/html/aligxdp/live/;
             index  index.html index.html;
             try_files $uri $uri/ /index.html;
             add_header Access-Control-Allow-Origin *;
          }

        location /byYtjEnv/ {
          rewrite ^/byYtjEnv/(.*) /$1 break;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header X-Forwarded-Host $server_name;
          proxy_set_header X-Real-IP $remote_addr;
           # For WebSocket upgrade header
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";

          proxy_pass http://app-service/;
        }
  
        error_page 404 /404.html;
                location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
                location = /50x.html {
        }
}   


 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个误通常表示WebSocket连接失败,原因是无效的帧头。根据引用和引用,该误可能由几个原因引起。一种可能性是WebSocket服务端无法连接到指定的URL。这可能是由于网络问题、服务器故障或URL设置误引起的。另一种可能性是代理服务器配置不正确,导致反向代理WebSocket连接失败。根据引用,使用Nginx来反向代理WebSocket服务时,需要正确配置Nginx以支持WebSocket连接。 要解决这个问题,你可以尝试以下几个步骤: 1. 确保WebSocket服务端的URL地址是正确的。检查URL是否包含正确的主机名、端口号和路径。确保WebSocket服务正在运行,并且可以通过指定的URL进行访问。 2. 检查网络连接是否正常。确保你的客户端能够与WebSocket服务端建立网络连接。你可以尝试使用其他网络设备或连接尝试重新连接。 3. 检查代理服务器配置是否正确。如果你正在使用代理服务器来反向代理WebSocket连接,确保你已正确配置Nginx或其他代理服务器以支持WebSocket连接。根据引用,你可能需要使用特定的配置选项来启用WebSocket支持,并确保代理服务器能够正确转发WebSocket请求。 4. 检查浏览器是否支持WebSocket。确保你正在使用支持WebSocket的浏览器版本。可以尝试在其他支持WebSocket的浏览器上测试连接,以确定是否是浏览器兼容性问题。 总结来说,当出现"WebSocket connection to 'ws://localhost:8080/ws' failed: Invalid frame header"误时,可能是由于无法连接到指定的URL、代理服务器配置问题、网络问题或浏览器不支持WebSocket等原因导致的。你可以尝试根据上述步骤进行排查和解决。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [控制台反复输出WebSocket connection to ‘ws://10.133.212.203:8080/ws‘ failed:](https://blog.csdn.net/weixin_46466212/article/details/126762870)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Nginx反向代理WebSocket服务连接WebSocket connection to “wss://xxx/xxx“ failed](https://blog.csdn.net/tiven_/article/details/126126442)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值