title: WebSocket Issue on AWS Elastic Beanstalk Using Port 80
body: <p>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. </p>
  
  <p>To initialize a WebSocket connection click <code>Create a new note</code>.</p>
  
  <p>AWS Beanstalk
  <a href="http://default-environment.psgekxtsbd.us-west-2.elasticbeanstalk.com/" rel="nofollow noreferrer">http://default-environment.psgekxtsbd.us-west-2.elasticbeanstalk.com/</a></p>
  
  <p>Heroku 
  <a href="https://murmuring-shelf-40601.herokuapp.com/" rel="nofollow noreferrer">https://murmuring-shelf-40601.herokuapp.com/</a></p>
  
  <p>This is how I am setting up the WebSockets on the server.</p>
  
  <pre><code>const express = require('express');
  const app = express();
  require("express-ws")(app);
  app.post("/service/create-account/", (req, res)=&gt; {/*code in here*/});
  app.ws('/:noteId/', function (ws, req) {/*code in here*/});
  const port = process.env.PORT || 3000;
  app.listen(port);
  </code></pre>
  
  <p>I have tried adding different configurations in the <code>.ebextensions</code> folder like,</p>
  
  <pre><code>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";
                  }
              }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>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";
  </code></pre>
  
  <p>When I add these configurations the HTML fails to load with the error <code>ERR_NAME_NOT_RESOLVED</code>. Here's that URL <a href="http://websocketsissue.us-west-2.elasticbeanstalk.com/" rel="nofollow noreferrer">http://websocketsissue.us-west-2.elasticbeanstalk.com/</a> </p>
  
  <p>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.</p>
  
  <p>Is there anyway to get WebSockets to work on port 80 using AWS Elastic Beanstalk?</p>
  
  <p>Update </p>
  
  <p>I was able to get the single instance environment to work, but not the load balanced, auto scaling environment.</p>
  
  <p>To get the single instance to work I added the following file to <code>.ebextensions</code> </p>
  
  <pre><code>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
  </code></pre>