nodejitsu/node-http-proxy — Issue #128: Proxy server dies when started with Ubuntu Upstart or as background process
Author: geuis
Created: 2011-10-13 01:28:10+00:00
State: open

When I run the proxy directly, it seems to work fine as long as the session remains open.

I'm having a hard time getting my proxy server to stay alive when its being started with Upstart or as a background process. Basically I can see the process start and then after a few seconds it dies. Upstart then continues to restart the proxy but it continues to bounce.

The other way to background a process is:

sudo node app.js &

I run multiple node.js services on the same server and start them up using Upstart with no issues.

First, here is my proxy server:

var http = require('http'),
httpProxy = require('http-proxy'),
fs = require('fs');

fs.readFile(__dirname + '/routes.json', function (err, data) {

if (err) throw err;

var options = {
hostnameOnly: true,
router: JSON.parse(data.toString())
};
var proxyServer = httpProxy.createServer(options);

proxyServer.listen(80);

});

Next, my routes.json file (domain changed, but localhost and port are the same):

{
"mysite.com": "127.0.0.1:9999"
}

The routes.json file is loaded and parsed as json and this is used for the router options.

Finally, this is my Upstart conf which is identical except for the app.js paths to several other conf files I have that work as expected.

description "nodeproxy node.js server"

start on started
stop on shutdown

respawn

script
# We found $HOME is needed. Without it, we ran into problems
export HOME="/usr/local/apps/nodeproxy/"

exec sudo -u www-data /usr/local/bin/node /usr/local/apps/nodeproxy/app.js

end script

I have done extensive checking including:
Error logging
File permissions on app.js and nodeproxy.conf
File owners
Running app.js with Upstart and as a background process using &

I'm really at a loss as to why this is running fine when I have the session open but refuses to be backgrounded.

Thanks!