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

1

16

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)?

Valentinavalentine answered 22/7, 2017 at 12:18 Comment(2)
NGINX for reverse proxy without a doubt, maybe you can only make the cluster with PM2 and let nginx do his magic (has no sense to load balance two times)Windhover
or also, use the load balancer in pm2 on a single server and use nginx if you want to balance multiple serversWindhover
C
32

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

Compendious answered 22/7, 2017 at 15:32 Comment(3)
The last url points to pm2 homepage with no article, please update itSoni
if you run sudo on pm2 you can get it to boot to port 80.Verena
PM2 uses node cluster for the proxy which is WAY slower than Nginx proxy. So what you suggesting is actually a bad solution, instead Nginx should do the proxy and PM2 should do no proxying.Core

© 2022 - 2024 — McMap. All rights reserved.