Search code examples
expressnginxwebrtcpeerjs

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


I am working on building videoapp using expressjs, peerjs. I am getting the below error while connecting to peerjs server. The app is running behind a proxy server (nginx). The code works fine in my local machine (I am not using proxy server while testing in my local).

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

I am getting the result when I do "https://<servername>/peerjs". I assume the I am able to connect to peerjs server. I do not understand why I am getting the above error.

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

I am combining peerjs along expressjs

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

Client side code

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

Below is the configuration for 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: I am new to nodejs and nginx. I feel I am missing something very basic that I am not able to figure it out.


Solution

  • Hi in your nginx config file add /peerjs in proxy_pass . So your location block should look something like

    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;
    

    }