Expressjs + Peerjs + nginx:无法连接到 Peerjs 服务器

Expressjs + Peerjs + nginx : Unable to connect to Peerjs server

我正在使用 expressjs、peerjs 构建视频应用程序。连接到 peerjs 服务器时出现以下错误。该应用程序 运行 位于代理服务器 (nginx) 后面。代码在我的本地机器上运行良好(我在本地测试时没有使用代理服务器)。

`GET https://<servername>/peerjs/id?ts=16228299262200.9619575641336071 404 (Not Found)`

我在 "https://<servername>/peerjs" 时得到了结果。我假设我能够连接到 peerjs 服务器。我不明白为什么会出现上述错误。

{"name":"PeerJS Server","description":"A server side element to broker connections between PeerJS clients.","website":"https://peerjs.com/"}

我正在结合 peerjs 和 expressjs

const ExpressPeerServer = require('peer').ExpressPeerServer;
const peerServer = ExpressPeerServer(server, {
  debug: true,
  proxied:true,
});
app.use('/peerjs', peerServer);

客户端代码

let myPeer = new Peer({host :'servername',secure:true})

下面是nginx的配置

server {

  server_name <server_name>;

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

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/<server_name>/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/<server_name>/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 {
    if ($host = <server_name>) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen 80;
  listen [::]:80;

  server_name <server_name>;
    return 404; # managed by Certbot


}

PS: 我是nodejs和nginx的新手。我觉得我遗漏了一些我无法弄清楚的非常基本的东西。

您好,在您的 nginx 配置文件中添加 /peerjs proxy_pass。所以你的位置块应该看起来像

location / {
proxy_pass http://localhost:3000/peerjs;
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;

}