Nginx

生產中的 WSS 連接問題

  • February 1, 2021

我使用 Angular8 作為前端,使用 Nodejs 作為後端

我已在生產中配置 WSS,但與客戶端的連接無法正常工作,在一個頁面連接正常但在另一頁面連接不工作。

websocket 和伺服器在同一個埠上執行

Ws(localhost) 一切正常

我們在後端使用的包

https://www.npmjs.com/package/ws

後端程式碼:

express = require('express');
app = express();
const http = require('http');
const port = 8080;
const fs = require('fs');
const certificate = {
cert: fs.readFileSync(''),
key: fs.readFileSync('')
}
const httpServer = http.createServer(app,certificate);

var server = require('ws');

var s =  new server.Server({ server:httpServer },{
 rejectUnauthorized: false
});
s.on('connection', function (ws) {
   ws.on('message', function (message) {
        var obj = JSON.parse(message);
         if (obj.messagetype == "test") {
             //send data
           }     
   }
}

httpServer.listen(port);

前端程式碼

export class WebsocketService {
constructor() { }
private subject: Rx.Subject<MessageEvent>;

public connect(url): Rx.Subject<MessageEvent> {
 if (!this.subject) {
   this.subject = this.create(url);
 }
return this.subject;
}

private create(url): Rx.Subject<MessageEvent> {
 let ws = new WebSocket(url);

 let observable = Rx.Observable.create((obs: Rx.Observer<MessageEvent>) => {
  ws.onmessage = obs.next.bind(obs);
  ws.onerror = obs.error.bind(obs);
  ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
});
 let observer = {
 next: (data: Object) => {
  if (ws.readyState === WebSocket.OPEN) {
     ws.send(JSON.stringify(data));
   }
 }
};
 return Rx.Subject.create(observer, observable);
 }
}

const CHAT_URL = "wss://cen.abcuae.com/";

export class SockoneService {

public messages: Subject<Message>;
public messages2: Subject<Message>;

constructor(wsService: WebsocketService) {
this.messages = <Subject<Message>>wsService.connect(CHAT_URL).pipe(map(
  (response: MessageEvent): Message => {
    let data = JSON.parse(response.data);
    return data;
  }));
  this.messages2 =  this.messages;
}

這是 /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";
}
}

快遞=要求(“快遞”);應用 = 快遞();

//這裡使用 https const http = require(‘https’);

常量埠 = 8080;常量 fs = 要求(‘fs’); const 證書 = { 證書:fs.readFileSync(’’),密鑰:fs.readFileSync(’’) } const httpServer = http.createServer(app,certificate);

var server = require(‘ws’);

var s = new server.Server({ server:httpServer },{ rejectUnauthorized: false }); s.on(‘connection’, function (ws) { ws.on(‘message’, function (message) { var obj = JSON.parse(message); if (obj.messagetype == “test”) { //發送數據 }

} }

引用自:https://serverfault.com/questions/1050643