Home  >  Article  >  Operation and Maintenance  >  Nginx reverse proxy server connection limit and request queue tuning method

Nginx reverse proxy server connection limit and request queue tuning method

PHPz
PHPzOriginal
2023-08-08 10:37:452580browse

Nginx reverse proxy server connection limit and request queue tuning method

Nginx reverse proxy server connection limit and request queue tuning method

When running high-concurrency network applications, the Nginx reverse proxy server is a A very common and reliable option. However, if connection limits and request queues are not properly configured, the server may experience performance bottlenecks and denial of service issues. This article will introduce how to use Nginx to limit the number of connections and optimize the request queue.

Limit the number of connections

Nginx can limit the number of connections by setting the worker_connections parameter. This parameter specifies the maximum number of connections that each worker process can handle simultaneously. When the number of connections reaches this limit, new connections will be rejected.

Open the Nginx configuration file, find the http block and add or modify the following lines:

http {
  ...
  worker_processes  auto;
  worker_connections  1024;
  ...
}

In the above example, worker_connections is set to 1024, which means each worker process can handle 1024 at the same time connections. Depending on the server's hardware performance and application requirements, you can make adjustments based on actual conditions. Please note that the value of worker_processes should be set to a multiple of the number of CPU cores to fully utilize server resources.

Request Queue Tuning

When the number of concurrent connections exceeds the worker_connections limit, Nginx will put the request in the queue waiting for processing. You can adjust the request queue length and timeout to minimize the possibility of denial of service.

Continue editing the Nginx configuration file and add or modify the following lines:

http {
  ...
  events {
    accept_mutex off;
    worker_connections  1024;
    worker_processes  auto;
    multi_accept on;
    use epoll;
    ...
  }
  ...
  server {
    ...
    location / {
      proxy_pass http://backend;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_http_version  1.1;
      proxy_set_header        Upgrade $http_upgrade;
      proxy_set_header        Connection "upgrade";
      proxy_read_timeout      600s;
      proxy_connect_timeout   600s;
      proxy_send_timeout      600s;
      proxy_buffer_size       128k;
      proxy_buffers           4 256k;
      proxy_busy_buffers_size 256k;
      proxy_buffering         on;
      ...
    }
    ...
  }
}

In the above example, we made several adjustments to the request queue. First, by setting accept_mutex to off, we disable the mutex so that multiple worker processes can accept new connections at the same time. Second, set multi_accept to on so that Nginx processes all requests in the queue as quickly as possible. Finally, we set the timeout and buffer size according to actual needs.

Code Example

The following is an example of a simple Node.js server, simulating a backend application.

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!
');
});

server.listen(3000, 'localhost', () => {
  console.log('Server running at http://localhost:3000/');
});

In the above example, we created a simple HTTP server listening on the local port 3000. Since this server is for demonstration purposes only, it will only return a simple "Hello, World!" string.

To use Nginx as a reverse proxy server, you need to save the above sample code as a file named server.js and execute the following command to start the server:

node server.js

Next, save the following Nginx configuration to a file named nginx.conf:

http {
  ...
  server {
    listen 80;
    location / {
      proxy_pass http://localhost:3000;
    }
  }
}

Start Nginx by executing the following command:

nginx -c /path/to/nginx.conf

Now , you can access your application by visiting http://localhost. All requests will be proxied and load balanced through the Nginx server.

Summary

By limiting the number of connections and tuning the request queue, you can better manage high-concurrency network applications. The above describes how to use Nginx to limit the number of connections and optimize the request queue, and provides a simple Node.js server code example. Please make appropriate configuration adjustments based on actual needs and the hardware performance of the server.

The above is the detailed content of Nginx reverse proxy server connection limit and request queue tuning method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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