Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 782
Ravi Teja Kumar Isetty Avatar asked Jul 22 '17 12:07

Ravi Teja Kumar Isetty


People also ask

Does PM2 use nginx?

This is a common method to use NGINX as a HTTP proxy front of PM2. NGINX will allow to serve static files rapidly, manage the SSL protocol and redirect the traffic to your Node.

What is PM2 and nginx?

This is a basic guide for deploying a LoopBack 4 (LB4) app using pm2 behind nginx reverse proxy. pm2 is a Production Runtime and Process Manager for Node. js applications with a built-in Load Balancer. It allows you to keep applications alive forever, to reload them without downtime and facilitate common Devops tasks.

Can you use nginx with node js?

Configuring Nginx For Nginx to route to the Node. js application listening on port 3000, we'll need to first unlink the default configuration of Nginx and then create a new configuration to be used for by our Node. js application. The Nginx configuration is kept in the /etc/nginx/sites-available directory.


1 Answers

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

like image 99
AbhinavD Avatar answered Oct 13 '22 17:10

AbhinavD