同域名下SocketIO+React在Windows IIS部署异常求助
Socket.IO + React 项目部署到 Windows IIS 服务器指导
项目现状
我正在搭建 Socket.IO + React 项目,本地环境下功能正常,但部署到 Windows IIS 服务器时需要后续操作指导。项目结构为根目录events,下含client和server两个文件夹:
Server 端代码(server/index.js)
const express = require("express"); const app = express(); const PORT = 4000; app.use(express.urlencoded({ extended: true })); app.use(express.json()); //New imports const http = require("http").Server(app); const cors = require("cors"); app.use(cors()); const socketIO = require('socket.io')(http, { cors: { origin: "domainname" } }); app.post("/api", (req, res) => { socketIO.emit("sendPlatform", req.body); res.status(200).json({ message: "Success" }); }); // Socket.IO connection handler socketIO.on('connection', (socket) => { console.log('A client connected'); // Socket.IO event to receive data from the client socket.on('sendData', (data) => { console.log('Received data:', data); // Handle the received data here // Emit a response event to the specific client socket.emit('responseData', { message: 'Data received' }); }); // Socket.IO event for client disconnection socket.on('disconnect', () => { console.log('A client disconnected'); }); }); http.listen(PORT, () => { console.log(`Server listening on ${PORT}`); });
本地执行node index.js可正常启动服务,日志显示Server listening on 4000。
Client 端代码(client/src/App.js)
import { useState, useEffect } from 'react'; import {io} from 'socket.io-client' const App= ()=> { const [platform, setPlatform] = useState('test'); useEffect(()=> { const socket = io('http://localhost:4000') socket.on('connect', ()=> console.log('connected maat')) socket.on("sendPlatform", (platform) => { console.log(platform); setPlatform(platform.name); }); },[]) return ( <div className="App"> {platform} </div> ) } export default App;
本地执行npm start功能正常。
已完成的操作:创建域名domainname,上传server和client文件夹并安装依赖,在client目录执行npm run build后将其配置为IIS站点路径,修改了index.js中的origin从http://localhost:3000改为domainname。
后续部署步骤
1. 配置 Node.js 服务在 IIS 上运行
首先需要安装 IIS 扩展:
- 安装 iisnode(用于在 IIS 中托管 Node.js 应用)
- 安装 URL Rewrite(用于 URL 重写和反向代理)
然后给server目录创建 IIS 应用程序:
- 在 IIS 管理器中,找到已配置的
client站点,右键添加应用程序 - 别名设为
api(或任意名称),物理路径指向events/server目录 - 在
server目录下创建web.config文件,内容如下:
<configuration> <system.webServer> <handlers> <add name="iisnode" path="index.js" verb="*" modules="iisnode" /> </handlers> <rewrite> <rules> <rule name="SocketIO" patternSyntax="ECMAScript"> <match url="socket.io/*" /> <action type="Rewrite" url="index.js/{R:0}" /> </rule> <rule name="API" patternSyntax="ECMAScript"> <match url="*" /> <action type="Rewrite" url="index.js" /> </rule> </rules> </rewrite> <iisnode node_env="production" /> </system.webServer> </configuration>
2. 修改客户端 Socket 连接地址
将client/src/App.js中的连接地址改为相对路径或域名地址(同域名下推荐相对路径):
// 替换原有的连接代码 const socket = io('/api'); // 或者直接用域名:io('https://domainname/api')
修改后重新执行npm run build,并更新 IIS 站点的静态文件。
3. 调整 Server 端配置
- 更新
server/index.js中的 CORS 配置,确保允许主域名访问:
const socketIO = require('socket.io')(http, { cors: { origin: "https://domainname", // 替换为你的实际域名,带http/https methods: ["GET", "POST"] } });
- 如果使用 URL 重写映射到
/api路径,无需修改端口监听,但要确保服务器防火墙开放4000端口(或者通过 URL Rewrite 隐藏端口)
4. 启动与测试
- 重启 IIS 站点和应用程序池
- 访问
https://domainname,打开浏览器控制台查看是否有connected maat日志 - 测试 API 接口:发送 POST 请求到
https://domainname/api,检查客户端是否接收到socketPlatform事件
注意事项
- 确保服务器安装了与本地一致的 Node.js 版本,避免依赖兼容问题
- 给
server目录赋予 IIS_IUSRS 用户的读写权限,确保 Node.js 能正常读取文件和写入日志 - 生产环境建议使用 PM2 或 iisnode 的进程管理功能,确保 Node 服务意外退出后自动重启
- 若使用 HTTPS,需在 IIS 配置 SSL 证书,同时 Socket.IO 连接要使用
wss://协议(相对路径会自动适配)
内容的提问来源于stack exchange,提问作者Gery
相关产品推荐
相关产品推荐

