Mastering Nginx

(Ron) #1

The NGINX HTTP Server


[ 116 ]

The NGINX master process is responsible for reading the configuration, handling


sockets, spawning workers, opening log files, and compiling embedded Perl scripts.
The master process is the one that responds to administrative requests via signals.


The NGINX worker process runs in a tight event loop to handle incoming connections.


Each NGINX module is built into the worker, so that any request processing, filtering,


handling of proxy connections, and much more is done within the worker process.
Due to this worker model, the operating system can handle each process separately


and schedule the processes to run optimally on each processor core. If there are any
processes that would block a worker, such as disk I/O, more workers than cores can


be configured to handle the load.


There are also a small number of helper processes that the NGINX master process


spawns to handle dedicated tasks. Among these are the cache loader and cache
manager processes. The cache loader is responsible for preparing the metadata


for worker processes to use the cache. The cache manager process is responsible


for checking cache items and expiring invalid ones.


NGINX is built in a modular fashion. The master process provides the foundation


upon which each module may perform its function. Each protocol and handler is
implemented as its own module. The individual modules are chained together into


a pipeline to handle connections and process requests. After a request is handled,
it is then passed on to a series of filters, in which the response is processed. One of


these filters is responsible for processing subrequests, one of NGINX's most


powerful features.


Subrequests are how NGINX can return the results of a request that differs from
the URI that the client sent. Depending on the configuration, they may be multiply


nested and call other subrequests. Filters can collect the responses from multiple


subrequests and combine them into one response to the client. The response is then
finalized and sent to the client. Along the way, multiple modules come into play.


See http://www.aosabook.org/en/nginx.html for a detailed explanation of
NGINX internals.


We will be exploring the http module and a few helper modules in the remainder
of this chapter.

Free download pdf