user3331344
user3331344

Reputation: 991

Cannot connect to peerjs server in production nginx webserver

I am trying to set up a custom peerjs server but I can't connect in production. I am assuming that ssl is the issue but not sure. I am using nginx on ubuntu server. I get the following error:

490.1fe86540fec474fb.js:12 WebSocket connection to 'wss://ps.mydomain.com/peerjs?key=peerjs&id=648463cc-0253-4472-9909-f57736…OjE3MTUzNjEzODZ9.KS9C_klV3zbDq1kbhzYFPyP_lvvUOXoy7bBcVmNvOXU&version=1.5.2' failed: WebSocket is closed before the connection is established.
490.1fe86540fec474fb.js:12 WebSocket connection to 'wss://ps.mydomain.com/peerjs?key=peerjs&id=1bbe4517-81bb-43fd-b42b-5f58ef…OjE3MTUzNjEzODZ9.dhCur0nOiUOaDJXXb7BIUk6yXiA1ZfwN7wbGZK_rN2w&version=1.5.2' failed: 

Here is how I am connecting in the client:

this.peer = new Peer(this.manager.user.uuid, {
  token: accessToken,
  host: 'ps.mydomain.com',
  port: 443
})

Here is peerServer.mjs

import 'dotenv/config'
import { PeerServer } from 'peer'
import { jwtVerify } from 'jose'

const serverConfig = {
  port: Number(process.env.PEER_PORT || 3001),
  path: '/peerjs',
  host: 'localhost',
  allow_discovery: true,
}

async function verifyToken(token) {
  const secret = new TextEncoder().encode(process.env.JWT_SECRET)

  if (!token) return null
  try {
    const { payload } = await jwtVerify(token, secret)
    return payload
  } catch (err) {
    throw err
  }
}

const peerServer = PeerServer(serverConfig)

peerServer.on('connection', async (client) => {
  try {
    const verified = await verifyToken(client.getToken())

    if (!verified?.uuid || verified?.uuid !== client.getId()) {
      client?.getSocket()?.close()
    }
  } catch (err) {
    client?.getSocket()?.close()
  }
})

peerServer.on('error', (err) => {
  console.error(err)
})

Here is my nginx config:

map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
}

server {
    server_name ps.mydomain.com;

    location /peerjs {
        proxy_pass http://localhost:5005/peerjs;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
    }

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

    listen 80;
    listen [::]:80;

    server_name ps.mydomain.com;
    return 404; # managed by Certbot
}

Everything seems to be working fine. If I go to ps.mydomain.com/peerjs the peer server is running. I get the following output.

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

Not really sure what the deal is.I've tried adding proxied: true but that doesn't work.

I've also tried adding ssl certs to the serverConfig but then I get a bad request in the browser for ps.mydomain.com. I added the following to the peerServer.mjs.

if(process.env.NODE_ENV === 'production') {
  serverConfig.ssl = {
    key: fs.readFileSync("/etc/letsencrypt/live/mydomain.com/privkey.pem"),
    cert: fs.readFileSync("/etc/letsencrypt/live/mydomain.com/fullchain.pem"),
  }
}

I also still can't connect. I have to assume if its required to pass in the key and cert for https then this is the actual problem. But I don't know why I am getting bad request when navigating through the browser and still can't connect. Things seem to work if I don't use ssl and start the server just listening on port 80 and then locally try to connect without https. So I can ony assume ssl is the issue. But I don't know what I am doing wrong.

Here is the error I am getting in the nginx error logs:

2024/05/10 21:02:52 [error] 17768#17768: *1 upstream prematurely closed connection while reading response header from upstream, client: (ip address here), server: ps.mydomain.com, request: "GET /peerjs HTTP/1.1", upstream: "http://[::1]:5005/peerjs", host: "ps.mydomain.com"
2024/05/10 21:02:52 [error] 17768#17768: *1 connect() failed (111: Unknown error) while connecting to upstream, client: (ip address here), server: ps.mydomain.com, request: "GET /peerjs HTTP/1.1", upstream: "http://127.0.0.1:5005/peerjs", host: "ps.mydomain.com"

At this point I am out of ideas. Can someone please help.

Thanks.

Upvotes: 1

Views: 109

Answers (0)

Related Questions