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

Nginx reverse proxy server connection limit and request queue tuning method

PHPz
Release: 2023-08-08 10:37:45
Original
2622 people have browsed it

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;
  ...
}
Copy after login

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;
      ...
    }
    ...
  }
}
Copy after login

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/');
});
Copy after login

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
Copy after login

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

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

Start Nginx by executing the following command:

nginx -c /path/to/nginx.conf
Copy after login

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!

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