I am running into an SSL error when setting up Nginx as a reverse proxy for a Node.js app. This app uses Angular to serve dynamic content and for WebSocket, we use ws. WS working properly but when attempting for wss I receive below error
WebSocket connection to 'wss://cen.abcuae.com:5000/' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR.
here is configuration under /etc/nginx/sites-available/default
server {
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/cen.abcuae.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/cen.abcuae.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_name cen.abcuae.com;
location / {
proxy_pass http://0.0.0.0:8080; #whatever port your app runs on
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;
proxy_read_timeout 3600;
}
}
server {
listen 443;
listen [::]:443 ssl;
server_name cen.abcuae.com;
ssl_certificate /etc/letsencrypt/live/cen.abcuae.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/cen.abcuae.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 C
location /websocket {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass https://cen.abcuae.com:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
The error occurs on the client-side when attempting to connect to the WebSocket server
WebSocket connection to 'wss://cen.abcuae.com::5000/' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
here is my frontend code
import { WebsocketService } from "./websocket.service";
const CHAT_URL = "wss://cen.abcuae.com:5000";
https.Server
instance with configured TLS key and certificate toWebSocketServer
constructor before calling newwss
service.wss
is a TLS-secured protocol, so you need to provide a certificate, a private key, and a properly configured server to do that. See WebSocket-Node documentation about this.