title: AWS Load Balanced socket.io server, running behind a Proxy on NGINX
body: <p>I'll be candid - I know very little about websockets, my config file was setup by a developer we had years ago, and even then I dont think his configuration was correct. All the HTTP(S) REST API responses work, but the websockets features are having issues.</p>
  <p>I am getting an error from the web console in production, everything works on local:</p>
  <pre class="lang-sh prettyprint-override"><code>WebSocket connection to 'wss://my.website.com/socket.io/?EIO=3&amp;transport=websocket&amp;sid=bE54hfVzKjRVT5H1AR1i' failed:
  </code></pre>
  <ul>
  <li>I have a little bit of a difficult environment where my socket.io server is running</li>
  <li>In our AWS environment I have a Linux server running NGINX, because this sits behind a public facing AWS load balancer (public facing listens via 443 with SSL) sending traffic down through port 80 to the actual box, all the certificates are handled at the AWS load balancer rather than on box.</li>
  <li>as a secondary issue, is there a reason my old developer would add <code>keepalive 64</code> on a websocket server? the verbiage seems to be odd when websockets are supposed to stay up for long periods of time?</li>
  </ul>
  <p>I know my issue has to be in the NGINX config, I dont suspect my issue is in my load balancer but I dont know where to start</p>
  <p>from my local machine, I have tried to change the url in my config to ws:// and wss:// with no success</p>
  <pre><code>upstream my_nodejs_upstream {
      server 127.0.0.1:8080;
      keepalive 64;
  }
  
  server {
      listen 80;
      server_name my_nodejs_server;
      root /home/www/application;
  
      location / {
          server_tokens off;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection &quot;upgrade&quot;;
          proxy_max_temp_file_size 0;
          proxy_pass http://my_nodejs_upstream/;
          proxy_redirect off;
          proxy_read_timeout 240s;
      }
  }
  </code></pre>
  <p>Nodejs</p>
  <pre class="lang-js prettyprint-override"><code>const express = require('express')
  const app = express()
  const server = require('http').Server(app)
  const io = require('socket.io')(server) // v2.0.4
  
  io.on('connection', (socket) =&gt; {
    // ...
  })
  
  // ...
  
  app.use((req, res, next) =&gt; {
    res.io = io
    next()
  })
  
  app.use('/', require('./routes'))
  
  const port = process.env.PORT
  server.listen(port, () =&gt; {
    // ...
  })
  </code></pre>
  <p>client</p>
  <pre class="lang-js prettyprint-override"><code>const socket = io.connect(process.env.SOCKET_SERVER)
  socket.on('connect', () =&gt; {
    socket.emit('...', ...)
  })
  
  socket.on('...', data =&gt; {
    // ...
  })
  </code></pre>
  <p>access.log -&gt; filtered for 400 errors</p>
  <pre class="lang-sh prettyprint-override"><code>10.3.0.225 - - [29/Jul/2021:18:43:26 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=QNqTyFYHniVsHwRVATA- HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:26 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=eL9XCFhOpUc1x8v5ATA_ HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:26 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=sUeUwuQ0WZZ6srppATBB HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:26 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=RFmviPReFWc6jj0JATBA HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:26 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=h_-JSjypuJkd1VB9ATBC HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:26 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=aGtQf1Ei2YFt-mfZATBD HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:27 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=B3uR9cdYoZS28pJ2ATAh HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&quot;
  10.3.0.45 - - [29/Jul/2021:18:43:27 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb0Yf&amp;sid=ok8WIn_zywc-5LhOAS_a HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:27 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb0Yf.0&amp;sid=EJYczgk3g6MkfXWRAS_b HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:29 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=jFE3Zhm59giR0_tYATBE HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:30 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=MK_Cxs3O2miJqc8EATBF HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=NhpayDE&amp;sid=dweMe6kGyqK-Yt0qATAZ HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=polling&amp;t=NhpayDE.0&amp;sid=dweMe6kGyqK-Yt0qATAZ HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb2XP&amp;sid=375bccUv_aKZr9saATAW HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb2XV&amp;sid=cnTj1b2cB9G_SldMATAY HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb2XW&amp;sid=8MEo84boodpm5OPyAS_n HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb2Xx&amp;sid=7d0LBCT9jvM-Q9PoATAU HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb2Y3&amp;sid=DpMuhNMh9jFvu7neATAV HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb2YF&amp;sid=_Ndcj-_BAej_YUjNATAX HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;POST /socket.io/?EIO=3&amp;transport=polling&amp;t=Nhpb3Y7&amp;sid=dweMe6kGyqK-Yt0qATAZ HTTP/1.1&quot; 400 52 &quot;https://my.ui-server.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=ULibUHaBWhNSu7hdATBK HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=KXUistz6s0Emu06kATBM HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=fuOE0YD01avGKg_gATBN HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=8T994wWah-1azLgoATBO HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:35 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=dReJzIK5lgkHblh4ATBP HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  10.3.0.225 - - [29/Jul/2021:18:43:36 +0000] &quot;GET /socket.io/?EIO=3&amp;transport=websocket&amp;sid=eO_a9FJ0HWH_LSuDATBQ HTTP/1.1&quot; 400 45 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36&quot;
  </code></pre>
  <p>I did not find anything meaningful inside the error.log</p>
  <pre class="lang-sh prettyprint-override"><code>$ cat error.log
  2021/07/29 06:08:01 [notice] 10145#10145: using inherited sockets from &quot;6;&quot;
  </code></pre>