Home Operation and Maintenance Nginx How to use NGINX and PM2 to achieve service expansion and load balancing on VPS servers

How to use NGINX and PM2 to achieve service expansion and load balancing on VPS servers

Sep 28, 2023 pm 10:25 PM
nginx Service extension pm

How to use NGINX and PM2 to achieve service expansion and load balancing on VPS servers

How to use NGINX and PM2 to achieve service expansion and load balancing on the VPS server?

In response to the demand for high concurrent access and improved server performance, service expansion and load balancing have become an important solution. This article will introduce how to use NGINX and PM2 to achieve service expansion and load balancing on VPS servers.

1. What are NGINX and PM2?

  1. NGINX is an open source, high-performance web server with reverse proxy, load balancing, caching and other functions. It is widely used in the Internet field for its excellent performance and reliability.
  2. PM2 is a modern Node.js process manager, which can help us manage the deployment, monitoring and automatic restart of Node.js applications.

2. Use NGINX for service expansion and load balancing

  1. Install NGINX

First, we need to install NGINX on the VPS server. Specific installation methods can be searched and found according to different operating systems.

  1. Configure NGINX reverse proxy

Before using NGINX for load balancing, we need to configure the reverse proxy to forward requests to the backend server. Edit the NGINX configuration file and add the following content in the http block:

upstream backend {
    server backend1;
    server backend2;
    server backend3;
}

server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
Copy after login

The backend1, backend2 and backend3 here are the addresses of the backend servers and can be configured according to the actual situation.

  1. Start the NGINX service

After saving the configuration file, use the following command to start the NGINX service:

sudo service nginx start
Copy after login

At this point, NGINX has been configured and you can Test whether the reverse proxy is working by visiting example.com.

3. Use PM2 to expand and load balance Node.js applications

  1. Install PM2

Install Node.js and PM2 on the VPS server . Specific installation methods can be searched and found according to different operating systems.

  1. Deploying Node.js applications

Through PM2, we can deploy Node.js applications to the server. In the root directory of the project, execute the following command:

pm2 start app.js
Copy after login

The app.js here is the entry file of your Node.js application.

  1. Configure the number of processes and load balancing of PM2

By default, PM2 will only start one process. If you need to start multiple processes to achieve load balancing, you can use the following command:

pm2 scale app +3
Copy after login

The app here is the name of your Node.js application in PM2, and 3 means to start three processes. You can adjust the number of processes yourself based on the server's configuration and performance.

4. Combining NGINX and PM2 to achieve service expansion and load balancing

  1. Modify the configuration of NGINX

In the previously configured NGINX configuration file, Change the forwarding address of the reverse proxy to the address and port monitored by the Node.js application, for example:

upstream backend {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
}
Copy after login
  1. Restart NGINX and PM2

After saving the configuration file, Use the following command to restart the NGINX and PM2 services:

sudo service nginx restart
pm2 restart all
Copy after login

At this point, NGINX and PM2 have completed the configuration of service expansion and load balancing. By accessing example.com, the request will be forwarded by NGINX to multiple Node.js processes on the backend for processing.

This article briefly introduces how to use NGINX and PM2 to achieve service expansion and load balancing on the VPS server. Through NGINX's reverse proxy and load balancing configuration, combined with PM2's multi-process deployment, the performance and stability of the server can be improved. In actual applications, NGINX and PM2 can be configured and adjusted in more detail according to specific needs.

The above is the detailed content of How to use NGINX and PM2 to achieve service expansion and load balancing on VPS servers. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles