使用端口80在AWS Elastic Beanstalk上发布WebSocket问题

时间:2022-08-25 09:23:55

I am migrating a node.js app from Heroku to AWS Elastic Beanstalk that uses WebSockets on port 80. The WebSockets are returning a 301 error on AWS Elastic Beanstalk, but not on Heroku.

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

To initialize a WebSocket connection click Create a new note.

要初始化WebSocket连接,请单击“创建新注释”。

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

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

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

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

This is how I am setting up the WebSockets on the server.

这就是我在服务器上设置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);

I have tried adding different configurations in the .ebextensions folder like,

我试过在.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";

When I add these configurations the HTML fails to load with the error ERR_NAME_NOT_RESOLVED. Here's that URL http://websocketsissue.us-west-2.elasticbeanstalk.com/

添加这些配置时,HTML无法加载错误ERR_NAME_NOT_RESOLVED。这是URL http://websocketsissue.us-west-2.elasticbeanstalk.com/

I setup my elastic beanstalk environment as a Web Server Environment using the predefined configuration for Node.js. I did not add any additional resources.

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

Is there anyway to get WebSockets to work on port 80 using AWS Elastic Beanstalk?

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

Update

更新

I was able to get the single instance environment to work, but not the load balanced, auto scaling environment.

我能够让单实例环境工作,但不能实现负载平衡的自动缩放环境。

To get the single instance to work I added the following file to .ebextensions

为了让单个实例工作,我将以下文件添加到.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

1 个解决方案

#1


4  

I found part of the answer here, https://scopestar.com/blog/aws/elasticbeanstalk/websockets/2016/10/21/enable-websockets-on-elasticbeanstalk-nginx-proxy.html

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

I added a file in the .ebextensions folder with the following content and that fixed it for single instance environments.

我在.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

To get the WebSockets to work on the load balanced, auto scaling environment I needed to do the following in addition

要使WebSockets在负载平衡,自动缩放环境中工作,我还需要执行以下操作

  • Go to the EC2 Dashboard
  • 转到EC2仪表板
  • Find the Load Balancer that the Elastic Beanstalk project created
  • 找到Elastic Beanstalk项目创建的Load Balancer
  • Right click on the Load Balancer and click Edit listener
  • 右键单击Load Balancer,然后单击Edit listener
  • Change the Load Balancer Protocol from HTTP to TCP for port 80 and save changes
  • 将端口80的负载均衡器协议从HTTP更改为TCP并保存更改

#1


4  

I found part of the answer here, https://scopestar.com/blog/aws/elasticbeanstalk/websockets/2016/10/21/enable-websockets-on-elasticbeanstalk-nginx-proxy.html

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

I added a file in the .ebextensions folder with the following content and that fixed it for single instance environments.

我在.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

To get the WebSockets to work on the load balanced, auto scaling environment I needed to do the following in addition

要使WebSockets在负载平衡,自动缩放环境中工作,我还需要执行以下操作

  • Go to the EC2 Dashboard
  • 转到EC2仪表板
  • Find the Load Balancer that the Elastic Beanstalk project created
  • 找到Elastic Beanstalk项目创建的Load Balancer
  • Right click on the Load Balancer and click Edit listener
  • 右键单击Load Balancer,然后单击Edit listener
  • Change the Load Balancer Protocol from HTTP to TCP for port 80 and save changes
  • 将端口80的负载均衡器协议从HTTP更改为TCP并保存更改