How to optimize VPS server response time and throughput using NGINX and PM2

PHPz
Release: 2023-09-27 08:45:15
Original
1305 people have browsed it

How to optimize VPS server response time and throughput using NGINX and PM2

The following is an article on how to use NGINX and PM2 to optimize the response time and throughput of a VPS server:

Title: How to use NGINX and PM2 to optimize Response time and throughput of VPS server

Overview:
In modern Internet application scenarios, how to improve the response time and throughput of the server is a challenge that every developer needs to face. NGINX and PM2 are two powerful tools that can help us optimize server performance easily. This article will introduce in detail how to use NGINX and PM2 to optimize the response time and throughput of the VPS server, and give specific code examples.

1. Installation and configuration of NGINX:

  1. Installation of NGINX:
    It is very simple to install NGINX on the VPS server. You can use package management tools (such as apt, yum, etc.) to install. Please refer to NGINX official documentation for specific installation steps.
  2. Configuring NGINX:
    The NGINX configuration file is located in /etc/nginx/nginx.conf. In this file, the performance of the server can be tuned. The following are some commonly used configuration items:

    • worker_processes: Specify the number of worker processes used by NGINX. It is generally recommended to set it to the number of CPU cores of the server.
    • worker_connections: Specifies the number of concurrent connections that each worker process can handle. This value can be adjusted according to the server configuration. It is generally recommended to set it to the maximum supported number of connections.
    • sendfile: Turning on this option can improve the efficiency of file transfer.
    • keepalive_timeout: Specify a timeout for a long connection, which can reduce the cost of establishing and closing the connection between the client and the server.

    The sample configuration file is as follows:

     worker_processes  4;
     
     events {
         worker_connections  1024;
     }
     
     http {
         ...
         sendfile            on;
         keepalive_timeout   65;
         ...
     }
    Copy after login

2. Installation and configuration of PM2:

  1. Installation PM2:
    PM2 is a tool for managing Node.js applications, which can help us achieve functions such as load balancing and automatic restart. Use the following command to install PM2:

     npm install pm2 -g
    Copy after login
  2. Configure PM2:
    The configuration file of PM2 is economy.config.js. In this file, you can configure the Node.js application that needs to be started. parameters. The following is a simple configuration example:

     module.exports = {
       apps : [{
         name: 'app',
         script: 'app.js',
         instances: 'max',
         exec_mode: 'cluster',
         autorestart: true,
         watch: false,
         max_memory_restart: '1G',
         env: {
           NODE_ENV: 'production'
         }
       }]
     };
    Copy after login
    • name: Application name
    • script: Application entry file path
    • instances: Number of instances to start, set to max can be automatically allocated according to the number of CPU cores of the machine
    • exec_mode: execution mode, set to cluster to achieve load balancing
    • autorestart: set to true to enable automatic restart
    • watch : Set to false to turn off the monitoring of file changes
    • max_memory_restart: Set the maximum memory usage of each instance. When this value is exceeded, the instance will be automatically restarted
    • env: Set the Node.js application Environment variables

3. Combination use of NGINX and PM2:

  1. Configure NGINX reverse proxy:
    Configure NGINX as Reverse proxy, forward the request to the Node.js application started by PM2. The following is an example configuration:

     server {
         listen   80;
         server_name  example.com;
     
         location / {
             proxy_pass http://localhost:3000;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection 'upgrade';
             proxy_set_header Host $host;
             proxy_cache_bypass $http_upgrade;
         }
     }
    Copy after login
    • listen: Specify the port number that the server listens on
    • server_name: Specify the bound domain name or IP address
    • location /: Specify Requested matching rules and proxy configuration
  2. Start the Node.js application:
    Use PM2 to start the Node.js application. The following is an example of a startup command:

     pm2 start ecosystem.config.js
    Copy after login

    After the command is executed, PM2 will automatically complete the startup of the application, and perform load balancing and automatic restart based on the parameters in the configuration file.

Conclusion:
This article introduces how to use NGINX and PM2 to optimize the response time and throughput of the VPS server. By appropriately adjusting the configuration parameters of NGINX and using PM2 for load balancing and automatic restart, the performance and stability of the server can be significantly improved. I hope this article can be helpful to developers who encounter difficulties in server optimization.

The above is the detailed content of How to optimize VPS server response time and throughput using NGINX and PM2. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template