This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

This article has been synchronized to the GitHub open source project: The Way of the Supergods in Java

The master and the worker

  • When Linux starts, there are two processes associated with Nginx, one for master and one for worker.

How The Master works

  • When the client sends a request to Nginx, the master will receive the request and notify all worker processes. At this time, the worker will compete for the request. After a worker grabs the request, it will forward the request according to the set steps.

  • Benefits of a master and multiple workers

    • You can usenginx -s reloadHot deployment. When a hot deployment is performed, it is normalworkerIt will restart, but the request is being processedworkerNo, we won’t restart until the request is processed.
    • For each individualworkerFor concurrency, there is no need to consider locking. And eachworkerThey don’t affect each other. Reduces the possibility of instantaneous service failure.

The setting of the worker

The number of worker

  • Like Redis, Nginx adopts IO multiplexing mechanism, and each worker is an independent process. eachworkerIs used to maximize CPU performance.
  • Therefore, it is most appropriate for the number of workers to be equal to the number of cpus on the server. (Set several workers for a few cores)
  • Less Settings waste CPU performance, resulting in slower processing of service requests.
  • Setting too many Settings will cause CPU loss due to frequent context switching.

This article has been synchronized to the GitHub open source project: The Path of The Java Supergods for more Java knowledge, please visit!