-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple instance of an app (in fork mode) bound to different ports + load balancing using nginx #765
Comments
If you can live without any feature of pm2, such as monitoring, etc, I would launch each instance isolated from each other (without fork), and use supervisord instead of pm2. Just my two cents. |
If pm2 is incapable of meeting this requirement then I will be switching to a solution like supervisord or monit etc. The reason I asked this here is because this is a basic requirement when setting up a bunch of instances of a node app to be load balanced by nginx. I know that pm2 can manage load balancing using the cluster module but imho cluster needs a lot of work still and isn't production ready yet. So yes, if pm2 cannot deliver then supervisord or monit it shall be but I really hope there's a way to do this in pm2. |
Facing same issue, though I am using pm2 with cluster mode in production (my application is small). But the problem I face is IF my node servers fail nginx can show a custom page (pages like Sorry we ll be back). But if I use pm2 I can find no way to do this. |
I was able to launch multiple instances of the same script using json config: pm2 start pm2.json pm2.json: {
"apps": [
{
"exec_mode": "fork_mode",
"script": "./lib/index.js",
"name": "proj-0",
"node_args": [ "--harmony" ],
"env": {
"PORT": 4001,
"NODE_ENV": "production"
},
"error_file": "/var/www/logs/proj-0.err.log",
"out_file": "/var/www/logs/proj-0.out.log"
},
{
"exec_mode": "fork_mode",
"script": "./lib/index.js",
"name": "proj-1",
"node_args": [ "--harmony" ],
"env": {
"PORT": 4002,
"NODE_ENV": "production"
},
"error_file": "/var/www/logs/proj-1.err.log",
"out_file": "/var/www/logs/proj-1.out.log"
}
]
} |
Json is always a better way to declare how your apps should be configured! With PM2 0.12 you will be able to write json with json5 Closing |
@Unitech Can you please elaborate on json5 and how it affects this issue ? I can't find anything like repeating the same config block over and over again with slight variations, as this is what is usually needed in these cases. |
one workaround that surprisingly worked for me is creating symlinks |
I personally run multiple instances of my app in different ports with pm2, i name the instance after the port, like 'myapp:8080' and i have a js loadbalancer and the nginx on lets say por 1337, then the nginx forwards the traffic from the port 80 and serves the static files. This is the load balancer i created for that, it uses redis to register and unregister servers, feel free to use it. https://gist.github.com/Zaggen/bbec82153f45ec60a136dca8e9ed65e7 And the nginx config file looks something like this:
|
Here is a really useful tutorial on how to implement clustering in PM2 https://keymetrics.io/2015/03/26/pm2-clustering-made-easy/ 🔢 |
Optimised your answer:
|
So this isn't an issue per se but I haven't been able to find data on this and would appreciate the help.
The idea is to run multiple instance of the same app (in fork mode) bound to different ports using pm2, on a single machine. The requests to these apps will be load balanced using nginx as a reverse proxy.
I run the first instance using the following command:
This works fine. But when I do:
I get an error saying:
How do I start multiple instances of the same app bound to different ports on a the same machine using pm2?
Thanks in advance!
The text was updated successfully, but these errors were encountered: