Concurrent processing mechanism

In general, there are three concurrent processing mechanisms: multi-process, multi-thread and asynchronous. Nginx uses all three mechanisms for concurrent processing. Of course, its asynchronous mechanism uses an asynchronous, non-blocking approach.

As we know, Nginx processes are divided into two types: master process and worker process. Each master process can generate multiple worker processes, so it is multi-process. Each worker process can process multiple user requests at the same time, and each user request will be handled by a thread, so it is multi-threaded.

So what explains asynchrony? The worker process uses the epoll multiplexing mechanism to process backend servers. When the background server returns the result, the backend server will call back the Epoll multiplexer, which will notify the corresponding worker. At this time, the worker process will suspend the transaction currently being processed, take the IO return result to the corresponding client request, and continue to execute the suspended transaction after the response is completed. This process is asynchronous and non-blocking. IO multiplexing can refer to the IO multiplexing article, very detailed.

Global module tuning

worker_processes

Open the nginx.conf configuration file and you can see that the default value for worker_processes is 1, as shown in the figure below

When we change it to 2 and run it, see the process running Nginx as shown below

Worker_processes specifies the number of Nginx worker processes, usually set to the number of CPU cores or an integer multiple of the number of cores. Note, however, that this value depends not only on the number of CPU cores, but also on the number of hard disks and the load balancing mode. You can specify the value auto when in doubt.

worker_cpu_affinity

The specific kernel to bind worker processes to, however, worker_CPU_affinity cannot be set if worker_PROCESSES is specified as auto.

This is done in binary, with each kernel represented by one binary bit, 0 for kernel off and 1 for kernel on. That is, how many bits are required for each kernel.

worker_rlimit_nofile

Worker_rlimit_nofile sets the maximum number of files that a worker process can open. The default value is the same as the maximum number of file descriptors that can be opened on current Linux systems.

You can view the maximum number of file descriptors that can be opened on your system by using the ulimit -n command, as shown in the figure below

Can use ulimit -n 65535 described can open file number changed to 65535 (this is just a temporary change, after restart the server failure), want to permanent changes to open the biggest number of the file, modify the/etc/security/limits file, Modify soft nofile 65535 hard nofile 65535.

Tuning the Events module

worker_connection 1024

Set the maximum number of connections that each worker process can process concurrently. This value cannot exceed the value of worker_Rlimit_nofile.

accept_mutex

  • On: The default value, indicating that when a new connection arrives, workers that are not in a working (blocked) state will process the connection in a serial manner. If accept_mutex is set to on, the active resource that changes the worker process from blocked to ready when the connection request comes in is a mutex. Only one worker process can acquire the mutex to enter the ready state (usually blocking queue header elements), and it will enter the running state when it is divided into time slices. Other worker processes that have not acquired the mutex are still blocked.

  • Off: indicates that when a new connection arrives, all workers will be woken up, but only one worker can get the new connection, and other workers will re-enter the blocking state, which is the panic situation. When the request is not coming, the activated resource will be a new connection. When the request is coming, all workers will become ready and compete for connection resources. Worker processes that do not get connection resources will become blocked again.

Nginx for jing group explanation: the author of the apache server process is typically hundreds of thousands of, in the event of jing group, will be particularly big impact on the system, but most progress in nginx is dozens, after a new connection to the state of all processes from the blocking state to the ready state, the impact on the system is not big, in the actual work,

For example, if accept_mutex is set to “accept_mutex”, this is equivalent to removing the worker blocking queue header element every time. This is slower than removing a chicken from the queue. If accept_mutex is set to on, all blocking workers will wake up, so all chickens can be eaten.

Therefore, in the case of high concurrency, it is generally set tooff, for a system with a small amount of concurrencyon.

accept_mutex_delay

When accept_mutex is set to on, it indicates the interval between the blocking queue leader worker’s attempts to acquire the mutex. The default value is 500 milliseconds. That is, the worker at the head of the blocking queue checks for new connections every 500 milliseconds and tries to acquire the mutex if there are any.

multi_accept on

  • off: The system selects connections one by one and allocates them to those with the least number of connections based on the load balancing policyworker.
  • on: The system will be real-time statistics of eachworkerThe number of connections currently being processed, and then thevacancyOne of the mostworker, so many new connections are assigned to this at onceworker.

The number of concurrent connection requests that a worker can process is usually 1024. If the number of connections processed by the current worker process is smaller, that is, 1024- number of connections processed by the current process = number of connections that a worker can process. If set to ON, requests will be sent to the worker process with the largest number of connections, which is faster. If set to off, requests will be sent to the worker process with the smallest number of connections, which is slower but not necessarily better than off. Because on requires mater to calculate all worker’s missing files in real time, it also consumes a lot of system resources

use epoll

If you set the connection processing mode between worker and client, Nginx will automatically select the most efficient mode suitable for the current system. Of course, you can also specify the connection processing mode to be used by using the USE instruction. The use value of this several: select | poll | epoll | rtsig | kqueue | / dev/poll

  • select | poll | epoll: These are the three multiplexing mechanisms,selectwithpollIt works almost the same way, andepollIt is the most efficient multiplexing mechanism in use today.
  • rtsig:realtime signal, real-time signal,Linux2.2.19 +In an efficient and clean way, but inthereThe version does not support this mode.
  • kqueueApplication in:BSDOn the systemepoll.
  • /dev/poll:UnixFor use on the systempoll.

HTTP module tuning

Introduction to non-tuning properties

  • include mime.type: said it would/usr/local/nginx/conf/mime.typeFile is included,mime.typeFile specifies the file type, which you can open to see as shown below

  • Default_type Application /octet-stream: indicates that a file with no extension is of application/octet-stream type by default, that is, Nginx will treat it as an octet-stream file.

  • Charset UTF-8: Sets the character encoding of the request and response.

sendfile on

If this parameter is set to on, the zero-copy mechanism of Linux is enabled. Otherwise, the zero-copy function is disabled. Whether the zero-copy function takes effect depends on the operating system version. For more information about zero copy, see: Understanding zero copy in more detail.

tcp_nopush on

  • on: Sends packets as separate packetsNginxThe actual response body data is sent in the form of a packet, which no longer contains the response header.
  • off: Default value. The response header information is included in each response body packet.

tcp_nodelay on

  • on: Does not set the data sending cache, that is, does not delay the transmission, suitable for small data transmission, no cache.
  • off: Enables the send cache. If the data to be transferred is large-volume files, such as images, you are advised to set this parameteroff.

keepalive_timeout 65

Set the timeout period for a long connection between the client and Nginx. When the timeout period expires, the connection will be closed automatically. The unit is second.

keepalive_requests 10000

Sets the maximum number of requests that a long connection can send. This value needs to be tested in a real-world environment.

client_body_timeout 10

Set the timeout period for the client to obtain Nginx response, that is, the maximum time between a request from the client and the response received by Nginx. If the request times out, the request is considered to have failed.