Search code examples
node.jsexpressnginxpm2

Can we use both NGINX and PM2 for node.js production deployment?


I am new to Node.js. I have built my first Node.js server. I am doing some research to improve performance of node js server in production. So I learned about NGINX and Process Manager(PM2).

NGINX:

  1. It can load balance the incoming requests.
  2. It can act as reverse proxy for our application.

PM2:

  1. It can divide our application as clusters though it has in built load balancer.
  2. We can monitor and restart application when crashed.

Can we use both for production?

Though load balancer is there in PM2 can I use only PM2?

What is the advantage of using NGINX over PM2?

If I use Load balancer using NGINX and clustering using PM2, will it give better performance than using only one (NGINX or PM2)?


Solution

  • This is a huge topic but let me help and give you some pointers.

    Nginx is much more than just a reverse proxy. It can serve static content, can compress the response content, can run multiple apps on different port on the same VM and much more.

    PM2 essentially helps you to scale throughput of your service by running it in cluster mode and utilizing all the cores of the box. Read this stackoverflow answer to understand more on this.

    Now to answer your question

    Can we use both for production?

    Yes and you should. Nginx can run on port 80. PM2 can run on port 3000 (or whatever port) which can then manage traffic within the instances of the app.

    gzip alone will make a huge difference in the app end user performance.

    Here is a good article in case you need code help on how to set it up