使用端口80的AWS Elastic Beanstalk上的WebSocket问题

我正在将Heroku中的node.js应用程序迁移到在端口80上使用WebSockets的AWS Elastic Beanstalk.WebSockets在AWS Elastic Beanstalk上返回301错误,但在Heroku上不会。

要初始化WebSocket连接,请单击“ Create a new note

AWS Beanstalk http://default-environment.psgekxtsbd.us-west-2.elasticbeanstalk.com/

Heroku https://murmuring-shelf-40601.herokuapp.com/

这就是我如何设置服务器上的WebSockets。

 const express = require('express'); const app = express(); require("express-ws")(app); app.post("/service/create-account/", (req, res)=> {/*code in here*/}); app.ws('/:noteId/', function (ws, req) {/*code in here*/}); const port = process.env.PORT || 3000; app.listen(port); 

我曾尝试在.ebextensions文件夹中添加不同的configuration,

 files: "/etc/nginx/conf.d/01_websockets.conf" : mode: "000644" owner: root group: root content : | upstream nodejs { server 127.0.0.1:80; keepalive 256; } server { listen 80; large_client_header_buffers 8 32k; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; # prevents 502 bad gateway error proxy_buffers 8 32k; proxy_buffer_size 64k; proxy_pass http://nodejs; proxy_redirect off; # enables WS support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } 

 files: "/etc/nginx/conf.d/websocketupgrade.conf" : mode: "000755" owner: root group: root content: | proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 

当我添加这些configuration时,HTML无法加载,错误为ERR_NAME_NOT_RESOLVED 。 这是那个URL http://websocketsissue.us-west-2.elasticbeanstalk.com/

我使用Node.js的预定义configuration将我的弹性beanstalk环境设置为Web服务器环境。 我没有添加任何额外的资源。

无论如何,WebSockets使用AWS Elastic Beanstalk在端口80上工作吗?

更新

我能够获得单实例环境的工作,但不是负载平衡的自动扩展环境。

为了让单实例工作,我将下面的文件添加到.ebextensions

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

我在这里find了部分答案, https://scopestar.com/blog/aws/elasticbeanstalk/websockets/2016/10/21/enable-websockets-on-elasticbeanstalk-nginx-proxy.html

我在以下内容的.ebextensions文件夹中添加了一个文件,并将其修复为单实例环境。

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

为了让WebSocket在负载均衡,自动扩展环境下工作,我需要另外做下面的事情

  • 转到EC2仪表板
  • findElastic Beanstalk项目创build的负载平衡器
  • 右键单击Load Balancer并单击编辑侦听器
  • 将负载均衡器协议从HTTP更改为TCP端口80,并保存更改