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?