天天看點

Error during WebSocket handshake Unexpected response code 404

問題描述:之前部署項目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要添加無參構造器

繼續閱讀