[Turn]nginx the start-up period of what has been done

Source: Internet
Author: User

Nginx is a multi-process Web container, different configuration of its starting mode is also different, here I only talk about the most typical way to start.

It has 1 master processes, and multiple worker processes (the number of optimal configurations is related to CPU cores). Well, first we'll find the main function, which is in the src/core/nginx.c file. When it comes to the source code, let's start by looking at the directory structure of the source code.

Nginx mainly has the following directories:

Src/core, this directory holds the underlying data structure like list, red-black tree, nginx string, through always some logical structures such as ngx_cycle_s, ngx_connection_s, etc., as well as the encapsulation of some underlying operations such as log, file operation, shared memory , memory pool, and finally a nginx.c this main startup function.

Src/event, this directory holds the structure and hook functions associated with abstract events. Nginx is an event-driven processing process, the event is naturally the core of the whole system, here defines the most core of the ngx_event_s structure.

The Src/event/modules directory contains specific event-driven methods, such as Epoll, Kqueue, poll, AIO, select, etc., which are hung in nginx by the hooks in the ngx_event_actions_t structure. Nginx starts with the configuration to decide which implementation to use.

UNIX implementations of many function calls under UNIX systems are stored in the Src/os/unix.

The Src/http directory is stored in the relevant implementation of the HTTP module, which handles HTTP requests, including parsing of the protocol and access to backend server code.

The Src/http/module directory holds some specific-purpose module for the HTTP module type, such as gzip processing encryption, image compression, and so on.

Once you have a preliminary understanding, go back to the main function and take a look at the things we are interested in. It executes the ngx_time_init first, why do you want to initialize the time? Nginx considered is very thoughtful, take the system time Gettimeofday is the system call, which means that the need to send interrupts to the Linux kernel, the kernel needs to do process switching to handle this call. This is a function that cannot ignore the cost. Nginx encapsulates the time function, so that every time we need to deal with times, not call Gettimeofday, but Nginx own cache time, so that a lot of reduced system calls, take the current time this matter but who love to do.

So, how does Nginx maintain its own clock? How does it make sense to ensure that the current time the user has taken? Nginx Designer's starting point is that Nginx is an event-driven mechanism, when a batch of events occur, that is, epoll_wait return, will take a gettimeofday to update their time, and then invoke the corresponding processing function of each event. These functions will ensure that they are non-blocking, that is, the processing power of the millisecond, so in any event handler, the time taken is the time before epoll_wait just returned, so that even if the time taken to slow down a few milliseconds does not matter. The key is that each function is non-blocking, we have to quickly return control to Nginx, which is the basic design principles of Kazakhstan.

After the initialization time of the main function, the core data structure Ngx_cycle is established, and then both the worker process and the master process revolve around it. Below, we want to pay great attention to ngx_init_cycle this function, the startup process a lot of work is done here, the code is not listed, this function has 800 lines, oversized, also visible its key. The first thing to do in Ngx_init_cycle is to invoke the Create_conf method in all Nginx module. Well, now we're going to look at what the Nginx module is about.

Nginx Abstracts a NGX_MODULE_S structure to describe each module, and each module handles the events it senses. Nginx in the total number of module is written dead in the code, but also can be flexibly configured, hehe, Nginx style of play. Recall, after downloading the Nginx source package, we also want to perform the Configure operation it provides, this command will generate makefile and ngx_modules files, Makefilel decide which module source files to compile, and the resulting ngx_ The MODULES.C file determines which module is used by the compiled execution file. The NGX_MODULES.C will generate an array ngx_modules, which is the global variable used by the entire Nginx project, in the following form:

ngx_module_t *ngx_modules[] = {    &ngx_core_module,    &Ngx_errlog_module,    &Ngx_conf_module,    ...}


This global variable generated through configure is critical, and only it knows what module a request might use for processing.

Then, Ngx_init_cycle is invoking all the module's create_conf methods through the Ngx_modules array (each module has the power to decide whether or not to implement this method, and if not, of course, it will not be called). Then, to start processing the configuration file, here we need to focus on the Ngx_conf_parse function, because it calls the Ngx_conf_handler method, the Ngx_conf_handler method will invoke each module in its own implementation of the set hook function, Let each module handle the configuration items that interest you. So, if you do not configure a module in the nginx.conf to want the east, this module, although compiled into, but will not be executed. Here we have to look at the structure of the module, can not always say ha.

structngx_module_s {ngx_uint_t ctx_index; ngx_uint_t index; ... ...    void*CTX; ngx_command_t*commands;    ngx_uint_t type; ngx_int_t (*init_master) (ngx_log_t *log); ngx_int_t (*init_module) (ngx_cycle_t *cycle); ngx_int_t (*init_process) (ngx_cycle_t *cycle); ngx_int_t (*init_thread) (ngx_cycle_t *cycle); void(*exit_thread) (ngx_cycle_t *cycle); void(*exit_process) (ngx_cycle_t *cycle); void(*exit_master) (ngx_cycle_t *cycle), ...};


Ctx_index is used to represent the ordinal number of a module we define in the context array, and index represents the ordinal number in the Ngx_modules array.

CTX this pointer points to the context of the module, type represents the module's types (module is categorical, each type can have multiple module), the following 8 hook functions, indicating that the corresponding event occurs when these methods are called (Of course, Module can also not be implemented). Commands points to the command structure to which the module belongs, for example, the HTTP module defines its own command:

static ngx_command_t  ngx_http_commands[] = {    {ngx_string ("http")  ),      ngx_main_conf| ngx_conf_block| Ngx_conf_noargs,      ngx_http_block,      0,      0,      NULL },      Ngx_null_command};


Let's look at the definition of ngx_command_s:

struct ngx_command_s {    ngx_str_t             name;    ngx_uint_t            type;     Char               * (*setvoid *conf);    ngx_uint_t            conf;    ngx_uint_t            offset;     void                 *post;};


So, as I said above, the Ngx_conf_handler method invokes the set hook function that is implemented in each module, and if we compile the HTTP module (which is available by default), the above ngx_ is called in the Ngx_conf_handler method. The Http_block function. This ngx_http_block function is worth detailing, as it reads the configuration file and decides which HTTP ports to listen to, and it plugs this information into the core variable of ngx_cycle by passing in the ngx_conf_t pointer.

Ngx_event_core_module is also a core module, said before in the end is by Epoll, select, poll or kqueue to achieve IO multiplexing, is the module to get it done.

Continue down. The Ngx_init_cycle function then calls all the init_conf hook functions implemented by the module. After that, execution to now Nginx process finally began to listen to the port. This is critical, just called the various module set hook method, such as the above HTTP module Ngx_http_block method, these methods have been to ngx_cycle listening array into the need to listen to the port. Why should we start listen now? Because there is no fork in the worker sub-process now ha. As you know, under the Linux system, the child processes that fork out will share the parent process's address space, so what needs to be done in all worker processes is put into ngx_init_cycle. The handle of the listener is shared with all nginx workers.

After listening for the specified port, start calling the Init_module hook function of all module implementations. Next, prepare for interprocess communication. A master, multiple workers, how do these processes communicate through each other? This is not the start, next time to elaborate. They also exchange data through shared memory, at which point the shared memory begins to initialize. Finally, Ngx_init_cycle is done, relieved?

Next, the main function initializes the semaphore, and the synchronization between processes is played through the semaphore. Then create the pidfile, this file is used to start the completion of the Nginx command line, the Nginx process to send the semaphore to control it. At the end of the main function, the Ngx_master_process_cycle function is executed, and this function does what the master process should do. It calls Ngx_start_worker_processes first to start the worker, to fork out many child processes according to the number of workers configured in the configuration file, to execute the ngx_worker_process_cycle function for each subprocess, This is a dead loop function that will begin processing real user requests.

ngx_master_process_cycle function again call ngx_start_cache_manager_processes start the cache management process, this block is limited to space, next time to talk about it. Finally, the ngx_master_process_cycle enters the dead loop, starts preparing to receive the signal from the command, and monitors the running status of each worker, and if a worker dies abnormally, it will be pulled back up.

In the final statement, I'm using the nginx-0.7.65 version as an example, the source code files listed above are for this version. Of course, I say these are the core functions, in fact, the 1.x version and the difference is very small.

[Turn]nginx the start-up period of what has been done

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.