WebSocket issue on AWAS Elastic Beanstalk using port 80

I am migrating a node.js application from Heroku to AWS Elastic Beanstalk, which uses WebSockets on port 80. WebSockets returns error 301 to AWS Elastic Beanstalk, but not to Heroku.

To initiate a WebSocket connection, click Create a new note.

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

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

This is how I configure 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 tried to add various configurations to a folder .ebextensions, for example,

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 does not load with an error ERR_NAME_NOT_RESOLVED. Here is this URL http://websocketsissue.us-west-2.elasticbeanstalk.com/

beanstalk -, node.js. .

WebSockets 80 AWS Elastic Beanstalk?

, .

, .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
+4
1

, 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.
  • Load Balancer Edit listener
  • HTTP TCP 80 .
+4

Source: https://habr.com/ru/post/1676416/


All Articles