Skip to content

Possible to run multiple instances of same app on different ports #322

Closed

Description

@waygee

Hi,

I wanted to inquire whether it's possible to run my app.js multiple clusters like so:

app.js Cluster1 port 80
….
app.js Cluster2 port 81
….
app.js Cluster 3 port 82
….
app.js Cluster 4 port 83

My hosting provider, Rackspace, has advised me to try to set up this way.

Is this possible to do this under pm2 and have my app.js be running multiple clusters under different ports?

Thanks in advance for your help.

Activity

  1. soyuka commented on Jun 13, 2014

    @soyuka
    Contributor

    You could clone your app 4 times... I think there are better solutions as load-balancing for those kind of things but pm2 can't handle this without some heavy mods.

  2. rlidwka commented on Jun 22, 2014

    @rlidwka
    Contributor
    NODE_PORT=80 pm2 start app.js -f
    NODE_PORT=81 pm2 start app.js -f
    NODE_PORT=82 pm2 start app.js -f
    NODE_PORT=83 pm2 start app.js -f
    

    ... where NODE_PORT is an arbitrary name that you add as .listen(process.env.NODE_PORT) into your program.

    (-f flag is necessary to force re-execution, since pm2 will complain otherwise)

  3. added a commit that references this issue on Jun 22, 2014
  4. aagam29 commented on Jun 14, 2019

    @aagam29

    What is the benefit of doing so ? @waygee
    Deploying the same application on multiple ports and then attaching a proxy say nginx will help you serve requests better ?

  5. alikarimii commented on Feb 14, 2020

    @alikarimii

    https://pm2.keymetrics.io/docs/usage/environment/

    module.exports = { apps : [ { name: "myapp", script: "./app.js", instances: 2, exec_mode: "cluster", watch: true, increment_var : 'PORT', env: { "PORT": 3000, "NODE_ENV": "development" } } ] }
    Use increment_var as in example

  6. jfoclpf commented on May 22, 2022

    @jfoclpf

    What is the benefit of doing so ? @waygee Deploying the same application on multiple ports and then attaching a proxy say nginx will help you serve requests better ?

    Load balancing with NGINX, an example

    upstream my_nodejs_upstream {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
        server 127.0.0.1:3003;
        keepalive 64;
    }
    
    server {
        listen 443 ssl;
        
        server_name www.my-website.com;
        ssl_certificate_key /etc/ssl/main.key;
        ssl_certificate     /etc/ssl/main.crt;
       
        location / {
        	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
        	proxy_set_header Host $http_host;
            
        	proxy_http_version 1.1;
        	proxy_set_header Upgrade $http_upgrade;
        	proxy_set_header Connection "upgrade";
            
        	proxy_pass http://my_nodejs_upstream/;
        	proxy_redirect off;
        	proxy_read_timeout 240s;
        }
    }
    

    Then in PM2

    module.exports = {
      apps : [
          {
            name: "myapp",
            script: "./app.js",
            instances: 4,
            exec_mode: "cluster",
            watch: true,
            increment_var : 'PORT',
            env: {
                "PORT": 3000,
                "NODE_ENV": "development"
            }
          }
      ]
    }

    Although PM2 with instances already does load balancing, so I don't know if it's worth the effort.

    The nice thing here is that you're not limited by the number of cores

    Check this article

  7. joneldiablo commented on Nov 24, 2022

    @joneldiablo

    hiii!!! @jfoclpf if I use 4 instances with exec_mode: 'cluster' (having 2 cores) I will get 8 instances running?
    instance 1 - port 3000 (2 in cluster)
    instance 2 - port 3001 (2 in cluster)
    ....

  8. mfahadshah commented on Jan 13, 2023

    @mfahadshah

    What is the benefit of doing so ? @waygee Deploying the same application on multiple ports and then attaching a proxy say nginx will help you serve requests better ?

    In my case i need to do it because pm2 doesn't work well with socket io long polling so i used this approach to let nginx load balance my app with ip_hash.
    I do have some concerns if this approach will take away perks from pm2 like reload with zero down time. please answer if anyone knows. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions