使用端口80的AWS Elastic Beanstalk上的WebSocket问题 [英] WebSocket Issue on AWS Elastic Beanstalk Using Port 80

查看:173
本文介绍了使用端口80的AWS Elastic Beanstalk上的WebSocket问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

要初始化WebSocket连接,请点击创建新笔记



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



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



这就是我在服务器上设置了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 /',函数(ws,req){/ *代码在这里* /});
const port = process.env.PORT || 3000;
app.listen(port);

我曾尝试在 .ebextensions 文件夹就像

 文件:
/etc/nginx/conf.d/01_websockets.conf:
模式:000644
所有者:root
组:root
内容:
上游nodejs {
服务器127.0.0.1:80;
keepalive 256;
}

服务器{
听80;

large_client_header_buffers 8 32k;

位置/ {
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
proxy_set_header主机$ http_host;
proxy_set_header X-NginX-Proxy true;

#防止502错误的网关错误
proxy_buffers 8 32k;
proxy_buffer_size 64k;

proxy_pass http:// nodejs;
proxy_redirect off;

#启用WS支持
proxy_http_version 1.1;
proxy_set_header升级$ http_upgrade;
proxy_set_header连接升级;


code




 文件:
/etc/nginx/conf.d/websocketupgrade.conf:
模式:000755
所有者: root
组:root
内容:|
proxy_set_header升级$ http_upgrade;
proxy_set_header连接升级;

当我添加这些配置时,HTML无法加载错误 ERR_NAME_NOT_RESOLVED 。以下是该网址 http://websocketsissue.us-west-2.elasticbeanstalk.com/



我使用Node.js的预定义配置将我的弹性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升级$ http_upgrade; \
proxy_set_header连接升级; \
'/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf


解决方案

我在这里找到答案的一部分, 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升级$ http_upgrade; \
proxy_set_header连接升级; \
'/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf

到让WebSocket在负载均衡的自动扩展环境中工作,我还需要执行以下操作:


  • 转至EC2控制面板 li>
  • 找到Elastic Beanstalk项目创建的负载均衡器

  • 右键单击负载均衡器并单击编辑侦听器

  • >将负载均衡器协议从HTTP更改为TCP端口80并保存更改


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.

To initialize a WebSocket connection click Create a new note.

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

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

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

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,

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/

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.

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

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

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

解决方案

I found part of the answer here, 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.

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

  • Go to the EC2 Dashboard
  • Find the Load Balancer that the Elastic Beanstalk project created
  • Right click on the Load Balancer and click Edit listener
  • Change the Load Balancer Protocol from HTTP to TCP for port 80 and save changes

这篇关于使用端口80的AWS Elastic Beanstalk上的WebSocket问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆