【发布时间】:2013-12-17 21:39:37
【问题描述】:
我想用 nginx 在 phusion 乘客上执行一个简单的 node-http-proxy 示例。 您可以在 https://github.com/nodejitsu/node-http-proxy 上找到此示例代码。
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create your proxy server
//
httpProxy.createServer(9000, 'localhost').listen(8000);
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
如果我执行此代码,我会收到以下错误。
App 28096 stderr: /usr/share/passenger/helper-scripts/node-loader.js:157
App 28096 stderr: throw new Error("http.Server.listen() was called more than once, which " +
App 28096 stderr: ^
App 28096 stderr: Error: http.Server.listen() was called more than once, which is not allowed because Phusion Passenger is in auto-install mode. This means that the first http.Server object for which listen() is called, is automatically installed as the Phusion Passenger request handler. If you want to create and listen on multiple http.Server object then you should disable auto-install mode.
比我在 google 群组https://groups.google.com/forum/#!topic/phusion-passenger/sZ4SjU8ypwc 上找到一个帖子并添加
PhusionPassenger.configure({ autoInstall: false });
在我第一次调用“createServer(...).listen(port)”之前。
下一步是将监听方法的端口参数的值替换为值“passenger”。但我没有成功。有没有人设法让它运行?
--- 我的解决方案---
感谢Hongli 的建议“接下来,您需要修改一个(并且只有一个)调用......”。这解决了我的问题。
var http = require('http'),
httpProxy = require('http-proxy');
if (typeof(PhusionPassenger) != 'undefined') {
PhusionPassenger.configure({ autoInstall: false });
}
httpProxy.createServer(9000, 'localhost').listen('passenger');
var target_server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
【问题讨论】:
标签: node.js nginx passenger http-proxy