Home Operation and Maintenance Nginx How to use NGINX and PM2 to optimize load balancing of VPS servers

How to use NGINX and PM2 to optimize load balancing of VPS servers

Sep 26, 2023 pm 06:17 PM
nginx load balancing pm

How to use NGINX and PM2 to optimize load balancing of VPS servers

How to use NGINX and PM2 to optimize the load balancing of VPS servers

Introduction:
In modern web applications, load balancing is a very critical item technology. It can improve application scalability and reliability by spreading traffic across multiple servers. In this article, we will introduce how to use NGINX and PM2 to optimize the load balancing of VPS servers. We'll explain each step in detail with specific code examples.

Step 1: Install NGINX
First, we need to install NGINX, which is a high-performance web server and reverse proxy server. The following are the specific commands to install NGINX on Ubuntu:

1

2

$ apt-get update

$ apt-get install nginx

Copy after login

Step 2: Configure NGINX
Once the installation is complete, we need to do some configuration of NGINX. The following is the content of a sample configuration file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

http {

    upstream backend {

        server backend1.example.com;

        server backend2.example.com;

    }

 

    server {

        listen 80;

 

        location / {

            proxy_pass http://backend;

        }

    }

}

Copy after login

In the above example, we set up a server cluster named backend, which contains two backend servers backend1.example.com and backend2.example.com. We will then listen on port 80 and proxy all traffic to the backend server cluster.

Step 3: Install PM2
Next, we need to install the PM2 process manager. PM2 can help us launch and manage Node.js applications on the server. Here are the specific commands to install PM2 on Ubuntu:

1

$ npm install pm2 -g

Copy after login

Step Four: Deploy the application on the backend server
Before we continue, we need to deploy a simple Node on the backend server. js application. The following is sample code for a simple Express application:

1

2

3

4

5

6

7

8

9

10

const express = require('express');

const app = express();

 

app.get('/', (req, res) => {

    res.send('Hello, World!');

});

 

app.listen(3000, () => {

    console.log('App is running on port 3000');

});

Copy after login

Save the above code as app.js and run the following command on the backend server to start the application:

1

$ node app.js

Copy after login

Now , our application should be accessible by visiting http://backend1.example.com:3000.

Step 5: Use PM2 to start the application on the back-end server
In order to use PM2 for process management, we need to enter the directory where the application is located and use the following command to start the application:

1

$ pm2 start app.js

Copy after login

The above command will run the application as a daemon process in the background, and automatically handle matters such as application restart and log management.

Step 6: Configure Load Balancing in NGINX
Now, we have configured NGINX and started the Node.js application on the backend server. We need to modify the NGINX configuration file to achieve load balancing. The following is the content of the modified sample configuration file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

http {

    upstream backend {

        server backend1.example.com:3000;

        server backend2.example.com:3000;

    }

 

    server {

        listen 80;

 

        location / {

            proxy_pass http://backend;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

        }

    }

}

Copy after login

In the above configuration file, we changed the port of the backend server from 80 to 3000, and added the proxy_set_header directive to pass some key HTTP header information.

Step 7: Restart NGINX and PM2 services
Before we perform load balancing testing, we need to restart NGINX and PM2 services for the changes to take effect. The following is the restart command:

1

2

$ service nginx restart

$ pm2 restart all

Copy after login

Step 8: Test load balancing
Now, we can use a web browser or the curl command to test whether the load balancing is working properly. We should be able to get a "Hello, World!" response by visiting http://yourdomain.com. Every time we refresh the page, we should see different server names for the backend servers, which means load balancing is in effect.

Conclusion:
By using NGINX and PM2, we can easily achieve load balancing on the VPS server. Load balancing can improve the scalability and reliability of applications to better serve users. We hope that the specific code examples and steps provided in this article can help readers better practice server optimization and load balancing.

The above is the detailed content of How to use NGINX and PM2 to optimize load balancing of 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)

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 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.

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 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 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 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 start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

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]

See all articles