簡體   English   中英

使用 HTTPS 和 NGINX 在節點服務器上連接的 Socket.io

[英]Socket.io with connection on node server with HTTPS and NGINX

我正在嘗試使用套接字,但是在生產中連接系統時,我相信由於 SSL 證書,它最終會產生一些沖突,並且會彈出此錯誤:

混合內容:“ https://myapp.com ”頁面已通過 HTTPS 加載,但請求了不安全的 XMLHttpRequest 端點“ http://myapp.com:3001/socket.io/?EIO=3&transport=polling&t=N4Tk_Lq ” . 此請求已被阻止; 內容必須通過 HTTPS 提供。

我嘗試了在互聯網上找到的幾種解決方案,但都沒有奏效,所以我在這里問了這個問題。

后台配置:

    const clientServer = require("http").Server(client.app);
    global.io = require('./socketio.js').init(clientServer, {
        pingInterval: 10,
        pingTimeout: 5,
    });

    clientServer.listen(3001, () => {
        console.log("client ON");
    });

前端配置:

const config: SocketIoConfig = { url: 'http://myapp.com:3001', options: {} };

nginx:

 location /client/v1 {
    proxy_pass http://localhost:3001;
    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;
  }

如果 NGINX 充當反向代理,則永遠不會公開端口3001

嘗試在前端的配置中將/client/v1端點附加到您的域名:

const config: SocketIoConfig = { url: 'https://myapp.com/client/v1', options: {} };

或者,如果您需要包含.br ...

const config: SocketIoConfig = { url: 'https://myapp.com.br/client/v1', options: {} };

我設法將節點服務器置於 https,也在 nginx 中更改

在角度客戶端它看起來像這樣(前端)

const config: SocketIoConfig = { url: 'https://myapp.com:3001', options: {secure: true} };

出於某種原因,它不適用於https://myapp.com/client/v1

在帶有 nodejs 的服務器端,它看起來像這樣:

var privateKey = fs.readFileSync('/etc/letsencrypt/live/myapp.com/privkey.pem', 'utf8').toString();
var certificate = fs.readFileSync('/etc/letsencrypt/live/myapp.com/cert.pem', 'utf8').toString();
var chain = fs.readFileSync('/etc/letsencrypt/live/myapp.com/chain.pem').toString();
var options = { key: privateKey, cert: certificate, chain: chain }; 

像這樣啟動服務器:

const clientServer = require("https").Server(options,client.app);
    global.io = require('./socketio.js').init(clientServer, {
        pingInterval: 10,
        pingTimeout: 5,
    });

    clientServer.listen(3001, () => {
        console.log("client ON");
    });

在 nginx 中,我必須將“s”放在 proxy_pass 中:

location /client/v1 {
    proxy_pass http://localhost:3001;
    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;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM