[英]Socket.IO on Subdomain vhost and proxy
我正在嘗試將socket.example.com:4000
代理到 websocket 服務器mydomain.com:3000
但客戶端也可以通過*.example.com:4000
連接到服務器,就像vhost
沒有效果和代理配置是全局設置的。 我不想代理其他子域。
我使用vhost和http-proxy-middleware
const options = {
target: 'http://example.com:3000',
changeOrigin: true,
ws: true
};
const wsProxy = createProxyMiddleware(options);
app.use(vhost('socket.example.com', wsProxy));
我不需要 vhost 或 http-proxy-middleware。 我使用了http-proxy和一些 vanilla javascript。
import httpProxy from 'http-proxy';
const proxy = httpProxy.createProxyServer({
target: {
host: 'example.com',
port: 4001
}
});
server.on('upgrade', (req, socket, head) => {
const domain = req.headers.host;
const host = domain.split(':')[0];
if (host === 'socket.example.com') {
proxy.ws(req, socket, head);
} else {
socket.destroy();
}
});
##Update## 在沒有代理或專用 websocket 服務器的同一台服務器上運行。
import { Server } from "socket.io";
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
allowRequest: (req, fn) => {
const domain = req.headers.host;
const host = domain.split(':')[0];
fn(null, host === `socket.example.com`);
}
});
io.on('connection', (socket) => {
console.log('a user connected');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.