0

I'm deploying a node.js app with PM2 behind an nginx reverse proxy. I want to run the PM2-managed app using a local socket, but there are a couple of obstacles.

Firstly the app needs to be told where to write its socket so that nginx can point at it. With vanilla node this is done via the PORT environment variable, however given that PM2 can run multiple instances in cluster mode, this can't be a static value and the app itself cannot know what to set it to (because it's not the app that decides). So I need to know how to get PM2 to tell the app the location dynamically. As far as I can see PM2 should be able to deal with this since it mentions that clstering works with sockets in the docs - it just doesn't say any more than that.

Secondly, the app itself needs to write the socket, so it needs to be in a location that's writable to the app. Typically sockets are put in subdirectories of /var/run, but that is not usually writable to an unprivileged app, and since it's usually on volatile storage, per-app directories disappear after a reboot and need recreating with appropriate ownership. This obviously requires privileged access to /var/run, so the app can't do this. PM2 can run with appropriate privileges so it would seem correct for PM2 to do this, however, I can't see any mechanism for it to do so. In upstart this would be done in a pre-start script.

How can I solve these two issues?

Synchro
  • 3,148
  • 6
  • 27
  • 38
  • With your [`Net.Server`](https://nodejs.org/api/net.html#net_server_listen_path_callback) listening on a socket path it'll work as expected. You could run pm2 as root, just by using it with the root user, or write your socket in another directory. – soyuka Nov 06 '15 at 14:36
  • I have no trouble getting node to run on a socket, it's getting pm2 to tell it to that's the problem. Running pm2 as root isn't the problem - it's node that writes the socket file, and that's not running as root, but it has to be told where to write it by pm2 because there may be multiple instances in a cluster, so the filename cannot be fixed. Does pm2 provide an instance id env var? – Synchro Nov 06 '15 at 14:42
  • You don't need to provide anything to pm2, it just runs your node server in the [cluster](https://nodejs.org/api/cluster.html). You can also implement a `cluster` and run it with a `fork` `exec_mode`. Or maybe I misunderstood the question. – soyuka Nov 06 '15 at 15:08
  • Yes, I'm talking about a pm2 forked cluster rather than a node one but if I do that each node instance pm2 creates will try to write its socket file in the same place. Or is there some kind of built-in proxy to balance/distribute requests? – Synchro Nov 06 '15 at 15:12
  • Wait, I don't get it. What's your `exec_mode`? – soyuka Nov 06 '15 at 15:43
  • `fork`, so pm2 will start multiple node instances (or at least I assume it will!) – Synchro Nov 06 '15 at 15:44
  • Fork will start only one instance. Cluster will spawn `x` instances. See http://pm2.keymetrics.io/docs/usage/cluster-mode/. – soyuka Nov 07 '15 at 11:54

0 Answers0