Phusion乘客错误:http.Server.listen()被多次调用 [英] Phusion Passenger Error: http.Server.listen() was called more than once

查看:121
本文介绍了Phusion乘客错误:http.Server.listen()被多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用nginx在phusion乘客上执行一个简单的node-http-proxy示例. 您可以在 https://github.com/nodejitsu/node-http-proxy .

I want to execute a simple node-http-proxy example on phusion passenger with nginx. You can find this example code on 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);

如果执行此代码,则会出现以下错误.

If I execute this code, I get the following error.

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 并添加

Than I find a post on google groups https://groups.google.com/forum/#!topic/phusion-passenger/sZ4SjU8ypwc and add

PhusionPassenger.configure({ autoInstall: false });

在我第一次调用"createServer(...).listen(port)"之前.

before I call the first time "createServer(...).listen(port)".

下一步是将侦听方法的端口参数值替换为值"passenger".但是我没有成功.有人设法使它运行吗?

The next step was to replace the value of the port parameter of the listen-method with the value 'passenger'. But I have no success. Has anybody managed to get this to run?

---我的解决方案---

--- My solution ---

感谢宏力的建议下一步,您需要修改一个(并且只有一个)调用...".这解决了我的问题.

Thank you Hongli for the advice "Next, you need to modify one (and only one) invocation ...". This solved my problem.

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);

推荐答案

2015年7月28日更新::此答案现已纳入官方的乘客文档中.请参考 https://www.phusionpassenger.com/library/indepth/nodejs/reverse_port_binding.html 获取最新答案.

Update July 28 2015: this answer has now been incorporated into the official Passenger documentation. Please refer to https://www.phusionpassenger.com/library/indepth/nodejs/reverse_port_binding.html for the latest answer.

Phusion乘客作者在这里.首先,让我解释一下为什么会发生此错误.

Phusion Passenger author here. First, let me explain what why this error occurs.

Node.js教程,Phusion Passenger 反转端口绑定钩入http.Server.listen().如果有多个listen(),那么Passenger不知道要使用哪个http.Server对象来接收请求,因此会引发此错误.

As explained in the Node.js tutorial, Phusion Passenger inverses port binding by hooking into http.Server.listen(). If there are multiple listen() then Passenger doesn't know which http.Server object to use for receiving requests, so it throws this error.

要解决此问题,您需要告诉乘客我想明确指定用于接收请求的http.Server".这分两个步骤完成.首先,您必须尽早在程序中调用以下代码:

To solve this problem, you need to tell Passenger "I want to specify explicitly which http.Server to use for receiving requests". This is done in two steps. First, you must call the following code as early as possible in your program:

if (typeof(PhusionPassenger) != 'undefined') {
    PhusionPassenger.configure({ autoInstall: false });
}

接下来,您需要修改http.Server.listen()的一个(并且只有一个)调用,并使其传递'passenger'的参数:

Next, you need to modify one (and only one) invocation of http.Server.listen(), and make it pass 'passenger' s argument:

server.listen('passenger');

如果乘客注意到您有多个listen('passenger')呼叫,它将投诉.你只能有一个.

If Passenger notices that you have multiple listen('passenger') calls, it will complain. You can only have one.

从Phusion Passenger 4.0.52开始,您还可以将'/passenger'作为参数传递.这旨在用于Hapi.js支持(请参见下文).

Since Phusion Passenger 4.0.52 you can also pass '/passenger' as argument. This is intended for Hapi.js support (see below).

这里是使用Express框架的完整示例.您必须将'passenger'传递给Express的listen()呼叫.

Here is a full example utilizing the Express framework. You must pass 'passenger' to Express's listen() call.

if (typeof(PhusionPassenger) != 'undefined') {
    PhusionPassenger.configure({ autoInstall: false });
}

var express = require('express');
var app = express();
app.get('/', function(req, res){
    var body = 'Hello World';
    res.setHeader('Content-Type', 'text/plain');
    res.setHeader('Content-Length', body.length);
    res.end(body);
});

if (typeof(PhusionPassenger) != 'undefined') {
    app.listen('passenger');
} else {
    app.listen(3000);
}

Hapi.js示例

使用Hapi.js时,必须将'/passenger'(而不是'passenger')传递给Hapi的服务器构造函数.这将导致Hapi尝试在Unix域套接字上侦听,从而允许Phusion Passenger的替代插入.如果您不传递该初始斜杠,则Hapi将尝试将参数视为TCP/IP域名,并且Hapi将失败并显示错误.

Hapi.js example

When using Hapi.js, you must pass '/passenger' (not 'passenger') to Hapi's server constructor. This will cause Hapi try to listen on a Unix domain socket, which allows Phusion Passenger's overrides to kick in. If you do not pass that initial slash, then Hapi will try to treat the argument as a TCP/IP domain name, and Hapi will fail with an error.

if (typeof(PhusionPassenger) != 'undefined') {
    PhusionPassenger.configure({ autoInstall: false });
}

var Hapi = require('hapi');
var server;

if (typeof(PhusionPassenger) != 'undefined') {
    // Requires Phusion Passenger >= 4.0.52!
    server = new Hapi.Server('/passenger');
} else {
    server = new Hapi.Server('localhost', 3000);
}

server.route({
    method: 'GET',
    path: '/hello',
    handler: function (request, reply) {
        reply('hello world');
    }
});

server.start();

这篇关于Phusion乘客错误:http.Server.listen()被多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆