Socket Io in AWS ElasticBeanStalk Node

2020-05-04 12:42发布

问题:

I have a node js application in Elastic Bean stalk.We are considering using socket io for a feature .

I read in some places that socket io support has to be manually enabled in AWS elasticbeanstalk. Specifically when it uses the default NGINX proxy.

I read By default an elastic beanstalk instance has an nginx proxy in front of it that is not configured to allow webSockets.

Is this correct information? If so , how to enable socket io support in AWS EB?

回答1:

This is correct information. You'll need to do some additional configuration for your Elastic Beanstalk deployment to get WebSockets(Socket.io or otherwise) to work.

Once you create your Elastic Beanstalk Environment, you'll need to configure your load balancer to accept TCP connections, and add a configuration file to your node project's root directory:

Configure Load Balancer:

  • Head over to your EC2 console and select the Load Balancers tab
  • Select the load balancer that belongs to your ELB environment from the list
  • Select the Listeners tab
  • Change the default entries' Instance Protocol to TCP

Add the Configuration File:

  • In the root directory of your node project, create a folder called .ebextensions

Create a file called enable-websockets.config in your new .ebextensions folder with the following contents:

container_commands:
  enable_websockets:
    command: |
      sed -i '/\s*proxy_set_header\s*Connection/c \
              proxy_set_header Upgrade $http_upgrade;\
              proxy_set_header Connection "upgrade";\
              proxy_pass_request_headers on;\
              ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf

This file tells the NGINX reverse proxy how to handle the HTTP 101 upgrade status code that WebSockets need to communicate with your application server.