title: Socket.io and Express with nginx
body: <p>I'm struggling to configure nginx correctly to ensure that it can handle the proxy for both Express (port 8081) and Socket.io (port 3000). Here is my config that is currently yielding a 502 error for the whole request, not just Socket.io:</p>
  
  <pre><code>server {
  
          root /var/www/example.com/public/;
  
          index index.html index.htm index.nginx-debian.html;
  
          server_name example.com;
  
          location /socket.io/ {
                  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;
                  proxy_pass http://127.0.0.1:3000/;
                  proxy_redirect off;
                  proxy_http_version 1.1;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection "upgrade";
          }
  
          location / {
                  #try_files $uri $uri/ =404;
                  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;
                  proxy_pass http://127.0.0.1:8081/;
                  proxy_redirect off;
                  proxy_http_version 1.1;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection "upgrade";
                  proxy_redirect off;
                  proxy_set_header X-Forwarded-Proto $scheme;
          }
  
      listen [::]:443 ssl; # managed by Certbot
      listen 443 ssl; # managed by Certbot
      ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
      ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
      include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  
  }
  
  server {
      if ($host = example.com) {
          return 301 https://$host$request_uri;
  } # managed by Certbot
  
  
          listen 80;
          listen [::]:80;
  
          server_name example.com;
      return 404; # managed by Certbot
  
  
  }
  
  </code></pre>
  
  <p>As far as I understand it, I need to ensure the Websocket used by Socket.io is upgraded to HTTP but this is where I'm struggling to grasp what I need to be doing. Presumably both Socket.io and Express need to be running on different ports and then need to proxy to nginx as per my configuration above.</p>
  
  <p>If I disable the Express proxy and just use nginx to serve up files then my assets are served but obviously I need this to work with both Express and Socket.io.</p>
  
  <p>Edit: I'm running nginx 1.14 but looking at a nginx blog post suggests I need at least 1.3...yet it looks a fair few years old already so not sure why the Ubuntu package manager is so outdated. I'm getting no configuration errors in but connection refused in error logs due to upstream.</p>
  
  <p>Any help much appreciated!</p>