Nginx (9): The principle of nginx

1. master 和 worker

Insert picture description here
Insert picture description here

2. How does the worker work?

Insert picture description here

3. The advantages of one master and multiple workers

  • nginx -s reloadHot deployment can be used ;
  • Each worker is an independent process. If one of the workers has a problem, the other workers continue to compete to implement the request process without causing service interruption.

4. Set the number of workers

It is most appropriate that the number of workers is equal to the number of CPUs of the server.

Both nginx and redis use an io multiplexing mechanism. Each worker is an independent process, but there is only one main process in each process, which processes requests in an asynchronous and non-blocking manner, even for tens of millions of requests. It's not a problem. Each worker thread can maximize the performance of a cpu. Therefore, it is most appropriate that the number of workers and the number of CPUs of the server are equal. Setting less will waste the cpu, setting too much will cause the loss caused by frequent context switching of the cpu.

#设置 worker 数量
worker process 4

#work绑定cpu(4 work 绑定 4cpu)
worker_cpu_affinity 0001 0010 0100 1000

##work绑定cpu(4 work 绑定 8cpu中的4个)
worker_cpu_affinity 0000001 00000010 00000100 00001000

5. Number of connections worker_connection

  • Send a request, occupying 2 or 4 requests of the worker
  • Nginx has a master and four workers. Each worker supports a maximum number of connections of 1024. The maximum number of concurrent connections supported is:
    • The maximum concurrent number of ordinary static access:worker_connection * worker_processes / 2
    • http as a reverse proxy, the maximum number of concurrency: worker_connection * worker_processes / 4(as a reverse proxy server, each concurrency will establish a connection with the client, a connection with the back-end server, so it will occupy two more connections)

Guess you like

Origin blog.csdn.net/houwanle/article/details/112132881