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 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:
$ apt-get update $ apt-get install nginx
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:
http { upstream backend { server backend1.example.com; server backend2.example.com; } server { listen 80; location / { proxy_pass http://backend; } } }
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:
$ npm install pm2 -g
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:
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'); });
Save the above code as app.js and run the following command on the backend server to start the application:
$ node app.js
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:
$ pm2 start app.js
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:
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; } } }
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:
$ service nginx restart $ pm2 restart all
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!