How to use WS with Node.js + Cloudflare + Nginx

Viewed 98

I would like to create a websocket using ws.

I use Nginx.

In my Nginx configuration that redirects "gateway.example.com" to "http://127.0.0.1:3006". In my Cloudflare configuration (DNS), there is gateway to SERVER_IP.

// For "generate" the websocket. There are the Cloudflare key & cert.
const server = require("https").createServer({
    cert: require("fs").readFileSync('./src/router/api/v1/cert.pem'),
    key: require("fs").readFileSync('./src/router/api/v1/key.pem')
});
server.listen(3006)

const wss = new WebSocket.Server({
  server
});
1 Answers

It was a problem with nginx. We need a special configuration for ws (upgrade).

Related