Node.js 为什么我的Socket.io客户端未使用NGINX反向代理连接到AWS EC2节点应用程序
我开发了一个使用socket.io的实时应用程序,它可以在开发环境中工作,但一旦我将其部署到生产环境中,它就会停止工作 My production Node.js应用程序部署在AWS EC2、NGINX上,用于反向代理 对于服务器上的socket.io,我创建了另一个侦听端口6444的服务器 这是我的服务器端代码:Node.js 为什么我的Socket.io客户端未使用NGINX反向代理连接到AWS EC2节点应用程序,node.js,sockets,nginx,mern,Node.js,Sockets,Nginx,Mern,我开发了一个使用socket.io的实时应用程序,它可以在开发环境中工作,但一旦我将其部署到生产环境中,它就会停止工作 My production Node.js应用程序部署在AWS EC2、NGINX上,用于反向代理 对于服务器上的socket.io,我创建了另一个侦听端口6444的服务器 这是我的服务器端代码: const app = require("express")(); const http = require("http"); const s
const app = require("express")();
const http = require("http");
const server = http.createServer(app);
const Server = require("socket.io");
const io = new Server(server);
const firebase = require("firebase/app");
const firebaseConfig = require("./firebaseConfig");
const firebaseApp = firebase.initializeApp(firebaseConfig);
const firebaseDB = require("firebase/database");
let hostname = "";
function RasberryPiSocket() {
io.on("connection", function (socket) {
socket.on("Server", (data) => {
hostname = data.device_id;
firebase
.database()
.ref(`wifi/${data.device_id}`)
.update({ Status: data.status })
.catch((error) => console.log(error));
});
socket.on("disconnect", function (device_id) {
firebase
.database()
.ref(`wifi/${hostname}`)
.update({ Status: "offline" })
.catch((error) => console.log(error));
});
});
app.get("/", (req, res) => res.send("Hello"));
server.listen(6444, function () {
console.log("listening on *6444");
});
}
module.exports = RasberryPiSocket;
以下是我的客户端代码:
const io = require("socket.io-client");
const socket = io.connect("https://<IP of EC2 instance>");
function RealTimeSocket(hostname) {
socket.on("connect", () => {
//console.log(socket, "Starting Socket Connection");
console.log(socket.id); // false
});
socket.on("CHI", (data) => {
console.log(data, hostname);
});
setInterval(
() =>
socket.emit("Server", {
device_id: hostname,
socket_id: socket.id,
status: "online",
}),
1000
);
}
module.exports = RealTimeSocket;
我可以访问端口4000上的所有API,socket.io端口6444使我超时
请帮助我已经4天了,我正在尝试寻找解决方案,但没有任何效果请尝试在ec2实例中的端口6444上添加入站规则。您将在ec2实例的安全组选项卡上找到入站/出站规则。
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_pass http://localhost:4000;
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;
}
location /chat/ {
proxy_pass http://localhost:6444;
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;
}