weixin_40183203
weixin_40183203
采纳率0%
2020-03-19 18:40 阅读 340

Nginx转发websocket报错 400 bad request 是什么问题,请各位大佬帮忙看看?

端口号8088
1.服务器端代码:

@Component
@ServerEndpoint("/websocket")
public class WebSocket {

 private Session session;
 private String name;
 private static ConcurrentHashMap<String,WebSocket> LinkDeviceMap = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String,WebSocket> LinkPhoneMap= new ConcurrentHashMap<>();
private static CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>();

    @OnOpen
 public void OnOpen(Session session){

     this.session = session;
     webSockets.add(this);
     System.out.println("[WebSocket] 连接成功");

 }

 @OnClose   
 public void OnClose(){
     webSockets.remove(this);
     System.out.println("[WebSocket] 退出成功");
 }
 @OnMessage
    public void OnMessage(String message) throws IOException {

          System.out.println("[WebSocket] 收到的消息"+message);

}
}

}
2.Nginx配置:
server {
listen 8087;
server_name localhost;
client_header_buffer_size 16k;
large_client_header_buffers 4 256k;
# 允许跨域
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass http://xxxxxxxxx:8088/websocket;
        # proxy_pass http://websocket/websocket;    #转发         
       proxy_set_header Host $host;       
         proxy_set_header X-Real-IP $remote_addr;    
    proxy_set_header X-Forwarded-For $remote_addr; 
       proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;       
         proxy_set_header Connection "upgrade";
    }

手机和h5测试ws://xxxxxxxx:8087可以连接websokcet通讯,没有问题,nginx转发websocket没有问题 但是硬件设备和sokit测试工具连接报错400 bad request

HTTP/1.1 400 Bad Request..Server: nginx/1.10.3..Date: Thu, 19 Mar 2020 10:37:31 GMT..Content-Type: text/html..Content-Length: 173..Connection: close......

400 Bad Request....

400 Bad Request

..
nginx/1.10.3......
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

相关推荐