I am runnning into an SSL error when setting up Nginx as a reverse proxy for a Node.js app. This app uses Express to serve static content and Socket.io to manage Websocket connections.

The error occurs on the client side when attempting to connect to the socket.io server:

GET https://mywebsite.com:3000/socket.io/?EIO=3&transport=polling&t=NHqAzi7 net::ERR_SSL_PROTOCOL_ERROR

The steps I have taken to set this up are as follows:

  • followed this tutorial to set up Nginx to use certificates generated by certbot,
  • add a path to /etc/nginx/sites-available/mywebsite.com for the default /socket.io/ socket path

My Nginx configuration file looks like this:

server {

    root /var/www/mywebsite.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name mywebsite.com www.mywebsite.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /socket.io/ {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.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 = mywebsite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name mywebsite.com www.mywebsite.com;
    return 404; # managed by Certbot
}

And my server setup follows these instructions and looks like this:

var express = require('express'),
    http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);

app.use(express.static(__dirname + "/public"));

server.listen(3000, ()=>{
  console.log('server listening on port 3000');
});

Finally, my client connects to the server like this:

socket = io('https://mywebsite.com:3000');

Any idea what else I could try to get this working?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @bicycle,

As you have already setup a reverse proxy for yourdomain.com/socket.io/ to proxy to port 3000, what you could do is update your client connects to the server like this:

socket = io('https://yourdomain.com/socket.io/');

That way as per your Nginx configuration the connection would get proxied to port 3000.

Hope that this helps!
Regards,
Bobby

Submit an Answer