使用 nginx 连接到使用与后端其余部分相同端口的 wss

问题描述 投票:0回答:1

我的服务器有域名和证书(这意味着我不能从客户端建立任何不安全的连接)。因此,我为后端使用相同的证书,为 webSocket 服务器/连接使用相同的端口和服务器。看来它不能有相同的服务器但不同的端口..所以我不知道如何让它与 nginx 一起工作....

客户端代码

useEffect(() => {
// here I try to use a different port to separate configs in nginx for nodejs and ws
        const ws = new WebSocket('wss://myip:8001');

        // // Connection opened
        ws.addEventListener('open', () => {
            ws.send('Connection established');
        });

        // Listen for messages
        ws.addEventListener('message', (event) => {
            setInfoMessages((prev) => {
                return prev?.length ? [...prev, event.data] : [event.data];
            });
        });

        return () => {
            if (ws.readyState === 1) {
                ws.close();
            }
        };
    }, []);

后端代码

const app = express();
app.options('*', cors());
app.use(cors());
app.use(express.json());

const privateKey = readFileSync('./privkey.pem', 'utf8');
const certificate = readFileSync('./fullchain.pem', 'utf8');

const credentials = { key: privateKey, cert: certificate };

const httpsServer = https.createServer(credentials, app);

const wss = new WebSocketServer({
    server: httpsServer,
});

wss.on('connection', function connection(ws) {
    console.log('WSS is connected\n');

    // ws.on('message', function message(data) {
    //  console.log('received: %s', data);
    // });

    // ws.send('something');
});

httpsServer.listen(8000, () => {
    console.log(`Server is running on port 8000`);
});

当前 nginx 配置

server {
    listen 80;
    server_name mydomain.com;

    # Redirect all HTTP traffic to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name mydomain.com;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;

    location / {
        root /root/media-buyer-ui/build;
        index index.html;
    }

    location ~/(auth|getbalance|getprofit|fetchprice) {
        proxy_pass https://myip:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

    }

}
upstream websocket {
   server localhost:8001;
}

我应该如何配置 nginx?谢谢

node.js nginx websocket https backend
1个回答
0
投票

我想我明白了,我只需要在客户端上像这样为 websocket 添加一个端点

useEffect(() => {
        const ws = new WebSocket('wss://mydomain.com/websocket');
...

在 nginxconf 中像这样

 location ~/(auth|getbalance|getprofit|fetchprice|websocket) { // HERE
        proxy_pass https://myip:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

    }
© www.soinside.com 2019 - 2024. All rights reserved.