3

I'm trying to create a secure node.js server without a websocket server on the same port. The port is 8080.

I can access the url in the browser and I can connect to websockets when I specify the port.

https://ws.site.com // Works
wss://ws.site.com // Don't work
wss://ws.site.com:8080 // Works

Why is this? What am I doing wrong? This is the nginx config

upstream ws.site.com {
    server 127.0.0.1:8080;
}

server {
    listen 443;
    server_name ws.site.com;

    ssl on;
    ssl_certificate /path/ssl-bundle.crt;
    ssl_certificate_key /path/myserver.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    location / {
            access_log off;

            proxy_pass https://ws.site.com;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_read_timeout 86400;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    }
}
8

The wss:// URL handler uses the default port of WebSockets, which is 443 if you don't add a port suffix by your own. If you run it on a non-default one (8080) you need the port suffix.

5
  • So I could add support for http (80) and redirect to https (443)?
    – tbleckert
    May 19 '15 at 13:05
  • Hm I think I misread your anwser. You mean that wss://ws.site.com secretly adds 443 as the port? How can I fix this? It works here websocket.org/echo.html, how do they do it?
    – tbleckert
    May 19 '15 at 13:25
  • @tbleckert No secret here. The default port for wss is 443, like it is for https. You can specify another port if you want both cases.
    – sebix
    May 19 '15 at 15:49
  • You mean I need to run node directly on port 443?
    – tbleckert
    May 19 '15 at 17:58
  • what should be changed in this .conf file?
    – Esqarrouth
    Jun 6 '20 at 21:42
0

The problem is in your settings.

Your upstream is running on port 8080:

upstream ws.site.com {
    server 127.0.0.1:8080;
}

And you are telling to look on https (443) port:

proxy_pass https://ws.site.com;
proxy_redirect off;

Fix it with http://ws.site.com

Simple port table:

  • port 80: http, ws
  • port 443: https, wss

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.