server {
server_name example.com;
location / {
proxy_pass http://localhost:3000;
}
location /peerjs {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /socket.io/ {
proxy_set_header Host $host;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
listen 443 ssl;
ssl_certificate ...; # managed by Certbot
ssl_certificate_key ...; # managed by Certbot
}
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server, {
allowEIO3: true,
cors: {
origin: "*"
}
});
server.listen(3000);
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
</script>
const io = new Server(server, {
transports: ["polling", "websocket"],
cors: {
origin: "*"
}
});
const io = new Server(server, {
transports: ["polling", "websocket"],
cors: {
origin: process.env.CLIENT_URL,
methods: ["GET", "POST"],
credentials: false,
}
});
io(process.env.NEXT_PUBLIC_SOCKET_URL, {
withCredentials: false,
reconnectionAttempts: 5,
})
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://localhost:3000;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ^~ /socket {
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;
}
0{"sid":"Lbo5JLzTotvW3g2LAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}
возвращается в том слуае если ошибка cors, попробуй заменить за место "*", просто сделать trueconst io = new Server(server, {
transports: ["polling", "websocket"],
cors: {
origin: true
}
});
WebSocket connection to … failed: Invalid frame headerто есть на стеке пишут что просто твой сокет на клиенте - пытается юзать сервер ссокет, а в документации сказано что по умолчанию он подключит клиентский
<script src="/socket.io/socket.io.js"></script>
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
server {
server_name example.com;
location / {
proxy_pass http://localhost:3000;
}
location /peerjs {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://localhost:3000;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
listen 443 ssl;
ssl_certificate ...; # managed by Certbot
ssl_certificate_key ...; # managed by Certbot
}
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server, {
transports: ["polling", "websocket"],
cors: {
origin: true,
methods: ["GET", "POST"],
credentials: false,
}
});
server.listen(3000);
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
<script>
const socket = io("wss://example.com", {
withCredentials: false,
reconnectionAttempts: 5,
})
</script>
const io = ...;
io.listen(3099);
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://localhost:3099;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
const socket = io("wss://example.com:3099");
server {
server_name example.com;
listen 3099 ssl;
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://localhost:3099;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
ssl_certificate ...; # managed by Certbot
ssl_certificate_key ...; # managed by Certbot
}
server {
server_name example.com;
location / {
proxy_pass http://localhost:3000;
}
location /peerjs {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://localhost:3099;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
listen 443 ssl;
ssl_certificate ...; # managed by Certbot
ssl_certificate_key ...; # managed by Certbot
}