Error during WebSocket handshake Unexpected response code 404
  eVOfnv2OroeT 2023年11月02日 62 0

问题描述:之前部署项目websocket好好的,移植到政务云SLB指定的域名下面就出错了:
Error during WebSocket handshake Unexpected response code 404

解决方案

1. 配置nginx

无论如何都要配置nginx:
​​​proxy_http_version 1.1;​​​​proxy_set_header Connection "upgrade";​​​​proxy_set_header Upgrade $http_upgrade;​

location /xxx{
proxy_pass http://127.0.0.1:7071/xxx;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

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

proxy_connect_timeout 60s;
proxy_read_timeout 7200s;
proxy_send_timeout 600s;

# 再不行的话就把下面的设置试一下
#proxy_set_header Upgrade websocket;
#proxy_pass_request_headers on;
#access_log off;
#proxy_buffering off;

}

2. WebSocket配置

我们用的是SpringBoot,所有配置不能少

import com.fh.websocket.session.MySpringConfigurator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
* 开启WebSocket支持
*/
@Configuration
public class WebSocketConfig {

@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}

@Bean
public MySpringConfigurator mySpringConfigurator() {
return new MySpringConfigurator();
}
}

3. WebSocketServer要添加无参构造器

这个很坑,部署到另一个是正常的。
移植过去就不行了。

Error during WebSocket handshake Unexpected response code 404_websocket


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
eVOfnv2OroeT