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

4
我正在将一个使用WebSockets在端口80上的node.js应用程序从Heroku迁移到AWS Elastic Beanstalk。在AWS Elastic Beanstalk上,WebSockets返回301错误,但在Heroku上没有出现此问题。
要初始化WebSocket连接,请单击“创建新笔记”。
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 文件夹中添加不同的配置,例如:

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

And

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

当我添加这些配置后,HTML加载失败,并出现错误ERR_NAME_NOT_RESOLVED。以下是URL:http://websocketsissue.us-west-2.elasticbeanstalk.com/ 我将我的Elastic Beanstalk环境设置为Web服务器环境,并使用Node.js的预定义配置。我没有添加任何其他资源。
有没有办法在AWS Elastic Beanstalk上使用端口80使WebSockets工作?
更新
我已经能够让单个实例环境运行,但无法让负载平衡、自动扩展环境运行。
要让单个实例运行,我将以下文件添加到.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

它能与其他端口一起使用吗?我知道8080是默认配置,但你尝试过了吗?或者你需要它是80端口吗? - Lyon
我刚试着将ws服务移动到8080端口,并将文件服务器和REST服务保留在80端口。这在我的本地主机上可以运行,但在EB上会出现502错误。 - sissonb
1个回答

6
我在这里找到了部分答案: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

为了让WebSockets在负载均衡、自动扩展环境下正常工作,我需要执行以下步骤:

  • 进入EC2控制台
  • 找到Elastic Beanstalk项目创建的负载均衡器
  • 右键单击负载均衡器并选择编辑监听器
  • 将端口80的负载均衡协议从HTTP更改为TCP并保存更改

你如何确定哪个负载均衡器属于 eb 项目? - Tori Huang
@ToriHuang 当您创建用于部署EB的环境时,它将记录所创建的负载均衡器的名称。您可以查看EB页面上的事件日志并找到该名称。它应该以awseb开头。 - sissonb
我尝试了同样的文件,但是EB部署失败,并出现以下错误: [emerg] 327#0: unexpected end of file, expecting ";" or "}" in /etc/nginx/conf.d/01_nginx_setup.conf:4 - 你能理解这是什么意思吗? - contool

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接