How to deploy a Node.js WebSocket server to Amazon Elastic Beanstalk?

﹥>﹥吖頭↗ 提交于 2019-12-01 12:02:26

If you have set up your package.json file correctly (primarily by using --save when npm installing) then you should not have to include the node_modules directory.

Check that the process.env.PORT setting is not changing the listened port - AWS EB usually sets the port to 8081.

To find the port being reported, you can add console.log(process.env.PORT) in your code, then connect via ssh to the server and run tail -f var/log/nodejs/nodejs.log (this monitors the log output of node.js). Then hit your server again and see what port shows up in the log output on your ssh connection.

To investigate the error you are getting, add a ws.on('error',...) function and log what you want.

Alright, after quite a bit or searching, I found a solution that works without changing anything to the ELB and without setting the proxy to none (I use nginx).

All you need is this:

  • In the root of your project, create the .ebextensions directory (if it doesn't already exist)
  • Create a file in there, I called mine 01_nginx.config
  • In this file, put the following code: container_commands:

    01_nginx_websockets:
        command: |
            sed -i '/\s*proxy_set_header\s*Connection/c \
            proxy_read_timeout 36000s; \
            proxy_set_header Upgrade $http_upgrade; \
            proxy_set_header Connection "upgrade"; \
            ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
    

Deploy your code to elastic beanstalk and enjoy websockets connecting!

PS: You don't really need the proxy_read_timeout property set, I just use that for my own. Also, it doesn't seem to do much so, I'll keep looking.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!