简体   繁体   中英

Issue using websocket with elastic beanstalk, nginx and node.js

I'm trying to deploy node.js application with sock.js on EB. Websockets work fine without proxy. When using nginx proxy ws connection issue occured. I have apended .ebextensions/.config file:

files:
  "/etc/nginx/conf.d/wssproxy.conf":
    mode: "000644"
    owner: root
    content: |
      map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
      }
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;

Websockets don't work, but nginx error log says:

upstream prematurely closed connection while reading upstream, client: 83.219.138.215, server: , request: "POST x/XXXXX/432/2l4e2j6v/xhr_streaming HTTP/1.1", upstream: " http://xxx.0.0.1:8081/XXXXX/432/2l4e2j6v/xhr_streaming ", host: "XXXXXX.elasticbeanstalk.com", referrer: " http://XXXXXX.elasticbeanstalk.com/ "

This is likely due to the fact that your load balancer is not deterministically mapping requests to upstream servers on both layer 7 (HTTP) and layer 4 (websockets). You'll notice that your provided error message shows you're using xhr-streaming, which works on layer 7. I'm guessing that if you force just the websocket protocol or just HTTP polling these errors will become more consistent, but they wont go away.

Here's a more detailed explanation with a few possible solutions. Unfortunately, they're all pretty annoying to implement. The following question refers to socket.io, but since you're using sockjs the ideal solution is quite a bit easier to implement given sockjs' use of server-specific IDs in HTTP polling request paths .

Node socket.io on load balanced Amazon EC2

All of these problems would go away if it were possible to use a deterministic load balancing method on the ELB, such as IP or HTTP path hashing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM