我有一个在IIS上用PHP构建的Web应用程序作为后端和Node.JS socket.io用于实时通信,但是我需要在同一个端口(443)上运行app(IIS和Node.JS)来保护两者连接,所以即时创建反向代理:
var httpProxy = require('http-proxy');
proxy = httpProxy.createProxyServer({}),
url = require('url');
http = require('http');
http.createServer(function(req, res) {
var hostname = req.headers.host.split(":")[0];
var pathname = url.parse(req.url).pathname;
if(pathname == '/realtime_script')
proxy.web(req, res, { target: 'http://localhost:8000/socket.io/socket.io.js' });
else if(pathname.indexOf("/socket.io") > -1)
proxy.web(req, res, { target: 'http://localhost:8000'+req.url });
else
proxy.web(req, res, { target: 'http://localhost:8080' });
}).listen(80, function() {
console.log('proxy listening on port 9000');
});
我设置socket.io来监听端口8000并且IIS绑定到端口8080,我设法使用http://localhost访问主页,所有内容都加载并完美连接(包括socket.io),但每次重定向发生了(例如提交表单或点击链接),节点js崩溃了这条消息:
C:\inetpub\wwwroot\node\node_modules\http-proxy\lib\http-proxy\index.js:119
throw err;
^Error: socket hang up
at createHangUpError (http.js:1472:15)
at Socket.socketCloseListener (http.js:1522:23)
at Socket.emit (events.js:95:17)
at TCP.close (net.js:465:12)
任何人都可以告诉我这里有什么问题?
谢谢