websocket 遇上nginx负载均衡时 error 400 1C

项目中有一个用websocket实现的即时聊天服务,然后使用nginx做负载均衡服务器之后,websocket便再也无法建立连接,连接报400 bad request ,websocket 和nginx都只是入门,不太懂,求大萝卜指导!!!

2个回答

nginx负载均衡时 error 400 1C
nginx负载均衡bad requestwebsocket
项目中有一个用websocket实现的即时聊天服务,然后使用nginx做负载均衡服务器之后,websocket便再也无法建立连接,连接报400 bad request ,websocket 和nginx都只是入门,不太懂,求大萝卜指导!!!

加配置试试
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
加到 location 里面

    location / {
      proxy_pass http://websocket;
      proxy_pass_header Date;
      proxy_pass_header Server;
      proxy_set_header   Host             $host;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Error 400 Bad Request
我常去的一个论坛,今天进时却出现Error 400 Bad Request的提示,咋回事?
jetty9.2.4 websocket error
java.lang.NullPointerExceptionrn at org.eclipse.jetty.websocket.server.WebSocketServerFactory.upgrade(WebSocketServerFactory.java:514)rn at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:185)rn at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:152)rn at org.eclipse.jetty.websocket.servlet.WebSocketServlet.service(WebSocketServlet.java:151)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)rn at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:594)rn at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:486)rn at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)rn at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)rn at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233)rn at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065)rn at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:413)rn at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)rn at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999)rn at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)rn at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)rn at org.eclipse.jetty.server.Server.handle(Server.java:351)rn at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454)rn at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890)rn at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944)rn at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634)rn at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230)rn at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77)rn at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609)rn at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:45)rn at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599)rn at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534)rn at java.lang.Thread.run(Thread.java:744)
通过websocket客户端向服务器发消息时,报400错误
客户端代码[code=javascript]var webSocket = new WebSocket(rn 'ws://127.0.0.1:8080/websocket');rnrn webSocket.onerror = function(event) rn onError(event)rn ;rnrn webSocket.onopen = function(event) rn onOpen(event)rn ;rnrn webSocket.onmessage = function(event) rn onMessage(event)rn ;rnrn function onMessage(event) rn document.getElementById('messages').innerHTML += ' 'rn + event.data;rn rnrn function onOpen(event) rn document.getElementById('messages').innerHTML = 'Connection established';rn rnrn function onError(event) rn alert(event.data);rn rnrn function start2() rn webSocket.send('hello');rn return false;rn [/code]rnrn服务端代码:rn[code=java]import java.io.IOException;rnrnimport javax.websocket.OnClose;rnimport javax.websocket.OnMessage;rnimport javax.websocket.OnOpen;rnimport javax.websocket.Session;rnimport javax.websocket.server.ServerEndpoint;rnrn@ServerEndpoint(value="/websocket")rnpublic class WebSocketTest rnrn @OnMessagern public void onMessage(String message, Session session) rn throws IOException, InterruptedException rn rn // Print the client message for testing purposesrn System.out.println("Received: " + message);rn rn // Send the first message to the clientrn session.getBasicRemote().sendText("This is the first server message");rn rn // Send 3 messages to the client every 5 secondsrn int sentMessages = 0;rn while(sentMessages < 3)rn Thread.sleep(5000);rn session.getBasicRemote().rn sendText("This is an intermediate server message. Count: " rn + sentMessages);rn sentMessages++;rn rn rn // Send a final message to the clientrn session.getBasicRemote().sendText("This is the last server message");rn rn rn @OnOpenrn public void onOpen () rn System.out.println("Client connected");rn rnrn @OnClosern public void onClose () rn System.out.println("Connection closed");rn rnrn[/code]rnrn启动后报这个错误:rnWebSocket connection to 'ws://127.0.0.1:8080/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400rn求解!!!!!!!!!!!!!!!!!!!!!!!!!!!
NGINX负载均衡
NGINX负载均衡分发请求的几种方式: 轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 2、weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 3、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 4、fair(第三方)
Nginx负载均衡
Nginx负载均衡在大型Web架构中分布式和负载均衡是必不可少的,一般企业常用的负载均衡方式有Nginx负载均衡、LVS负载均衡、硬件负载均衡器(贵)、cdn负载均衡(贵、大公司)等几种方式,LVS工作在网络4层架构中,而Nginx负载均衡工作在网络7层架构中,相对于性能方面来讲,LVS负载均衡方案要高出Nginx负载均衡方案不少,并且能够用在更多的场合,比如Mysql,Redis等的负载均衡上。N
nginx负载均衡
使用nginx替代硬件资源F5实现应用间的负载均衡,详解其配置过程
手机浏览器websocket,无法连接一直error
rn```rn var websocket = null;rn if ('WebSocket' in window) rn websocket = new WebSocket("ws://localhost:8080/springws/websocket/socketServer.do");rn else if ('MozWebSocket' in window) rn websocket = new MozWebSocket("ws://localhost:8080/springws/websocket/socketServer.do");rn else rn websocket = new SockJS("http://localhost:8080/springws/sockjs/socketServer.do");rn rn websocket.onopen = onOpen;rn websocket.onmessage = onMessage;rn websocket.onerror = onError;rn websocket.onclose = onClose;rnrn```rn一直是监听到error,请问有了解的吗。rn电脑端浏览器是正常的,手机浏览器检测有WebSocketrnrn
相关热词 c# 标准差 计算 c#siki第五季 c#入门推荐书 c# 解码海康数据流 c# xml的遍历循环 c# 取 查看源码没有的 c#解决高并发 委托 c#日期转化为字符串 c# 显示问号 c# 字典对象池