Node.js 使用端口80在AWS Elastic Beanstalk上发布WebSocket

Node.js 使用端口80在AWS Elastic Beanstalk上发布WebSocket,node.js,amazon-web-services,heroku,websocket,amazon-elastic-beanstalk,Node.js,Amazon Web Services,Heroku,Websocket,Amazon Elastic Beanstalk,我正在将node.js应用程序从Heroku迁移到AWS Elastic Beanstalk,该应用程序在端口80上使用WebSocket。WebSocket在AWS Elastic Beanstalk上返回301错误,但在Heroku上没有返回 要初始化WebSocket连接,请单击创建新便笺 AWS豆茎 赫罗库 这就是我在服务器上设置WebSocket的方式 const express = require('express'); const app = express(); requir

我正在将node.js应用程序从Heroku迁移到AWS Elastic Beanstalk,该应用程序在端口80上使用WebSocket。WebSocket在AWS Elastic Beanstalk上返回301错误,但在Heroku上没有返回

要初始化WebSocket连接,请单击创建新便笺

AWS豆茎

赫罗库

这就是我在服务器上设置WebSocket的方式

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
文件夹中添加不同的配置,例如

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";
                }
            }

添加这些配置时,HTML无法加载,错误为
ERR\u NAME\u NOT\u RESOLVED
。这是网址

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

使用AWS Elastic Beanstalk是否可以让WebSocket在端口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

我在这里找到了部分答案

我在
.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仪表板
  • 查找Elastic Beanstalk项目创建的负载平衡器
  • 右键单击负载平衡器,然后单击编辑侦听器
  • 将端口80的负载平衡器协议从HTTP更改为TCP并保存更改

它与其他端口一起工作吗?我知道8080是默认配置,您是否尝试过,或者是否需要8080我只是尝试将ws-services移动到端口8080,并将文件服务器和REST服务保留在端口80上。它在EB上给了我一个502错误,但它在我的LocalHost上起作用。您如何确定哪个负载平衡器属于EB项目?@Toriang当您为要部署到的EB创建环境时,将记录创建的负载平衡器的名称。您可以查看EB页面上的事件日志并找到名称。它应该从awseb开始。我尝试了完全相同的文件,但eb部署失败,出现以下错误:
[emerg]327\0:文件意外结束,应为“;/etc/nginx/conf.d/01\u nginx\u setup.conf:4
”中的“}”或“}”-你能理解这一点吗?
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