In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the execution process of nginx startup initialization code". In daily operation, I believe that many people have doubts about what is the execution process of nginx startup initialization code. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the execution process of nginx startup initialization code?" Next, please follow the editor to study!
Code execution process
Ngx_event_process_init: traverses connections, initializes the callback function of listenfd, and adds event listener
Static ngx_int_t
Ngx_event_process_init (ngx_cycle_t * cycle)
{
...
Cycle- > connections =
Ngx_alloc (sizeof (ngx_connection_t) * cycle- > connection_n, cycle- > log)
Cycle- > read_events = ngx_alloc (sizeof (ngx_event_t) * cycle- > connection_n
Cycle- > log)
...
Ls = cycle- > listening.elts
For (I = 0; I)
< cycle->Listening.nelts; iTunes +) {
C = ngx_get_connection (ls[ I] .fd, cycle- > log)
C-> listening = & LS [I]
Rev = c-> read
}
...
Rev- > handler = ngx_event_accept
Ngx_add_event (rev, NGX_READ_EVENT, 0)
...
}
Ngx_event_accept: call the callback function of ngx_listening_t
Void
Ngx_event_accept (ngx_event_t * ev)
{
...
Lc = ev- > data
Ls = lc- > listening
...
S = accept (lc- > fd, (struct sockaddr *) sa, & socklen)
C = ngx_get_connection (s, ev- > log)
Ngx_add_conn (c)
Ls- > handler (c)
...
}
Find the location of ls- > handler (c)
Static char *
Ngx_http_block (ngx_conf_t * cf, ngx_command_t * cmd, void * conf)
{
...
If (ngx_http_optimize_servers (cf, cmcf, cmcf- > ports)! = NGX_OK) {
Return NGX_CONF_ERROR
}
...
}
Static ngx_int_t
Ngx_http_optimize_servers (ngx_conf_t * cf, ngx_http_core_main_conf_t * cmcf
Ngx_array_t * ports)
{
...
If (ngx_http_init_listening (cf, & port [p])! = NGX_OK) {
Return NGX_ERROR
}
...
}
Static ngx_int_t
Ngx_http_init_listening (ngx_conf_t * cf, ngx_http_conf_port_t * port)
{
...
Ls = ngx_http_add_listening (cf, & addr [I])
If (ls = = NULL) {
Return NGX_ERROR
}
...
}
Static ngx_listening_t *
Ngx_http_add_listening (ngx_conf_t * cf, ngx_http_conf_addr_t * addr)
{
...
Ls- > handler = ngx_http_init_connection
...
}
Third-party module (output):
Server {
Listen 0.0.0.0:70
Location / {
Root / usr/local/nginx/html/bak/
Limit_req zone=req10k burst=5 nodelay
Shuchu "helloworld"
}
}
The three cores of a module
Static ngx_command_t ngx_http_shuchu_commands [] = {
{
Ngx_string ("shuchu")
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1
Ngx_http_shuchu
NGX_HTTP_LOC_CONF_OFFSET
Offsetof (ngx_http_shuchu_loc_conf_t, content)
NULL
}
Ngx_null_command
}
Static ngx_http_module_t ngx_http_shuchu_ctx = {
NULL
Ngx_http_shuchu_init
NULL
NULL
NULL
NULL
Ngx_http_shuchu_create_loc_conf
Ngx_http_shuchu_merge_loc_conf
}
Ngx_module_t ngx_http_shuchu_module = {
NGX_MODULE_V1
& ngx_http_shuchu_ctx, / * module context * /
Ngx_http_shuchu_commands, / * module directives * /
NGX_HTTP_MODULE, / * module type * /
NULL, / * init master * /
NULL, / * init module * /
NULL, / * init process * /
NULL, / * init thread * /
NULL, / * exit thread * /
NULL, / * exit process * /
NULL, / * exit master * /
NGX_MODULE_V1_PADDING
}
Ngx_conf_parse: parsing configuration
Char *
Ngx_conf_parse (ngx_conf_t * cf, ngx_str_t * filename)
{
...
Type = parse_file
Fd = ngx_open_file (filename- > data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0)
Rc = ngx_conf_read_token (cf)
Omit the determination of the combination of rc and type
Rc = ngx_conf_handler (cf, rc)
...
}
Ngx_conf_handler: processing instructions
Static ngx_int_t
Ngx_conf_handler (ngx_conf_t * cf, ngx_int_t last)
{
...
For (I = 0; ngx_ modules [I]; iTunes +) {
Cmd = ngx_ modules [I]-> commands
For (/ * void * /; cmd- > name.len; cmd++) {
If (cmd- > type & NGX_DIRECT_CONF) {
Conf = ((void * *) cf- > ctx) [ngx_ modules [I]-> index]
} else if (cmd- > type & NGX_MAIN_CONF) {
Conf = & ((void * *) cf- > ctx) [ngx_ modules [I]-> index])
} else if (cf- > ctx) {
Confp = * (void * *) ((char *) cf- > ctx + cmd- > conf)
If (confp) {
Conf = confp [NGX _ modules [I]-> ctx_index]
}
}
Rv = cmd- > set (cf, cmd, conf)
}
}
...
}
Ngx_http_block
Static char *
Ngx_http_block (ngx_conf_t * cf, ngx_command_t * cmd, void * conf)
{
...
Ctx- > main_conf = ngx_pcalloc (cf- > pool, sizeof (void *) * ngx_http_max_module)
Ctx- > srv_conf = ngx_pcalloc (cf- > pool, sizeof (void *) * ngx_http_max_module)
Ctx- > loc_conf = ngx_pcalloc (cf- > pool, sizeof (void *) * ngx_http_max_module)
For (m = 0; ngx_ modules [m]; masks +) {
If (ngx_ modules [m]-> type! = NGX_HTTP_MODULE) {
Continue
}
Module = ngx_ modules [m]-> ctx
Mi = ngx_ modules [m]-> ctx_index
If (module- > create_main_conf) {
Ctx- > main_ confession [mi] = module- > create_main_conf (cf)
If (ctx- > main_ confi [mi] = = NULL) {
Return NGX_CONF_ERROR
}
}
If (module- > create_srv_conf) {
Ctx- > srv_ confession [mi] = module- > create_srv_conf (cf)
If (ctx- > srv_ confi [mi] = = NULL) {
Return NGX_CONF_ERROR
}
}
If (module- > create_loc_conf) {
Ctx- > loc_ confession [mi] = module- > create_loc_conf (cf)
If (ctx- > loc_ confi [mi] = = NULL) {
Return NGX_CONF_ERROR
}
}
}
Pcf = * cf
Cf- > ctx = ctx
...
/ * parse inside the http {} block * /
Cf- > module_type = NGX_HTTP_MODULE
Cf- > cmd_type = NGX_HTTP_MAIN_CONF
Rv = ngx_conf_parse (cf, NULL)
...
}
Ngx_http_core_server
Static char *
Ngx_http_core_server (ngx_conf_t * cf, ngx_command_t * cmd, void * dummy)
{
...
Ctx = ngx_pcalloc (cf- > pool, sizeof (ngx_http_conf_ctx_t))
If (ctx = = NULL) {
Return NGX_CONF_ERROR
}
Http_ctx = cf- > ctx
Ctx- > main_conf = http_ctx- > main_conf
Ctx- > srv_conf = ngx_pcalloc (cf- > pool, sizeof (void *) * ngx_http_max_module)
Ctx- > loc_conf = ngx_pcalloc (cf- > pool, sizeof (void *) * ngx_http_max_module)
...
Cscf = ctx- > srv_ confession [NGX _ http_core_module.ctx_index]
Cscf- > ctx = ctx
Cmcf = ctx- > main_ confession [NGX _ http_core_module.ctx_index]
Cscfp = ngx_array_push (& cmcf- > servers)
If (cscfp = = NULL) {
Return NGX_CONF_ERROR
}
* cscfp = cscf
/ * parse inside server {} * /
Pcf = * cf
Cf- > ctx = ctx
Cf- > cmd_type = NGX_HTTP_SRV_CONF
Rv = ngx_conf_parse (cf, NULL)
...
}
Ngx_http_core_listen
Static char *
Ngx_http_core_listen (ngx_conf_t * cf, ngx_command_t * cmd, void * conf)
{
...
Cscf- > listen = 1
Value = cf- > args- > elts
Ngx_memzero (& u, sizeof (ngx_url_t))
U.url = value [1]
Ngx_parse_url (cf- > pool, & u)
If (ngx_http_add_listen (cf, cscf, & lsopt) = = NGX_OK) {
Return NGX_CONF_OK
}
...
}
Ngx_http_add_listen
Ngx_int_t
Ngx_http_add_listen (ngx_conf_t * cf, ngx_http_core_srv_conf_t * cscf
Ngx_http_listen_opt_t * lsopt)
{
...
Cmcf = ngx_http_conf_get_module_main_conf (cf, ngx_http_core_module)
If (cmcf- > ports = = NULL) {
Cmcf- > ports = ngx_array_create (cf- > temp_pool, 2
Sizeof (ngx_http_conf_port_t))
If (cmcf- > ports = = NULL) {
Return NGX_ERROR
}
}
Sin = & lsopt- > u.sockaddr_in
P = sin- > sin_port
Port = ngx_array_push (cmcf- > ports)
Port- > family = sa- > sa_family
Port- > port = p
Port- > addrs.elts = NULL
Ngx_http_add_address (cf, cscf, port, lsopt)
...
}
Ngx_http_add_address
Static ngx_int_t
Ngx_http_add_address (ngx_conf_t * cf, ngx_http_core_srv_conf_t * cscf
Ngx_http_conf_port_t * port, ngx_http_listen_opt_t * lsopt)
{
...
Addr = ngx_array_push (& port- > addrs)
Addr- > opt = * lsopt
Addr- > default_server = cscf
...}
Ngx_http_add_server
Static ngx_int_t
Ngx_http_add_server (ngx_conf_t * cf, ngx_http_core_srv_conf_t * cscf
Ngx_http_conf_addr_t * addr)
{
...
Server = ngx_array_push (& addr- > servers)
* server = cscf
...
}
So far, the basic architecture of the whole configuration has been set up. There are multiple listening addresses (ngx_http_conf_addr_t) hanging under the port (ngx_http_conf_port_t). The opt after each address is currently read in the configuration and contains the listener information, and the listening address (ngx_http_conf_addr_t) is followed by a server block (ngx_http_core_srv_conf_t) where the listening address exists. The next step is to finally do something in optmize, do something in opt, and then do hash:
Ngx_http_optimize_servers
Static ngx_int_t
Ngx_http_optimize_servers (ngx_conf_t * cf, ngx_http_core_main_conf_t * cmcf
Ngx_array_t * ports)
{
...
Port = ports- > elts
For (p = 0; p
< ports->Nelts; packs +) {
Addr = port [p] .addrs.elts
For (a = 0; a)
< port[p].addrs.nelts; a++) { if (ngx_http_server_names(cf, cmcf, &addr[a]) != NGX_OK) { return NGX_ERROR; } } if (ngx_http_init_listening(cf, &port[p]) != NGX_OK) { return NGX_ERROR; } } ... } ngx_http_server_names:整理addr中的servers,将sn和对应的srv_conf对应起来 static ngx_int_t ngx_http_optimize_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, ngx_array_t *ports) { ... ha.pool = cf->Pool
Cscfp = addr- > servers.elts
For (s = 0; s)
< addr->Servers.nelts; slots +) {
Name = cscfp [s]-> server_names.elts
For (n = 0; n)
< cscfp[s]->Server_names.nelts; nasty +) {
Rc = ngx_hash_add_key (& ha, & name [n] .name, name [n] .server
NGX_HASH_WILDCARD_KEY)
}
...
Hash.hash = & addr- > hash
Ngx_hash_init (& hash, ha.keys.elts, ha.keys.nelts)
}
...
}
Ngx_http_add_listening
Static ngx_listening_t *
Ngx_http_add_listening (ngx_conf_t * cf, ngx_http_conf_addr_t * addr)
{
...
Ls = ngx_create_listening (cf, & addr- > opt.u.sockaddr, addr- > opt.socklen)
Ls- > handler = ngx_http_init_connection
...
}
Instance
Every time get_connection reverses instance, s is the socket obtained by accept, which is an int, which is assigned to c-> fd
Ngx_connection_t *
Ngx_get_connection (ngx_socket_t s, ngx_log_t * log)
{
...
C = ngx_cycle- > free_connections
Ngx_cycle- > free_connections = c-> data
Ngx_cycle- > free_connection_n--
Rev = c-> read
Wev = c-> write
Ngx_memzero (c, sizeof (ngx_connection_t))
C-> read = rev
C-> write = wev
C-> fd = s
Rev- > instance =! instance
Wev- > instance =! instance
Rev- > data = c
Wev- > data = c
Return c
...
}
While in free, only move the linked list, empty the files, and make the current linked list available.
Void
Ngx_free_connection (ngx_connection_t * c)
{
...
C-> data = ngx_cycle- > free_connections
Ngx_cycle- > free_connections = c
Ngx_cycle- > free_connection_n++
If (ngx_cycle- > files) {
Ngx_cycle- > Files [c-> fd] = NULL
...
}
And both add_event and add_connection point the epoll directly to instance, and when process_events, take out the event to point to.
Static ngx_int_t
Ngx_epoll_add_event (ngx_event_t * ev, ngx_int_t event, ngx_uint_t flags)
{
...
Ee.data.ptr = (void *) ((uintptr_t) c | ev- > instance)
...
}
Static ngx_int_t
Ngx_epoll_process_events (ngx_cycle_t * cycle, ngx_msec_t timer, ngx_uint_t flags)
{
...
C = event_ list [I] .data.ptr
Instance = (uintptr_t) c & 1
If (c-> fd = =-1 | | rev- > instance! = instance) {
...
}
At this point, the study on "what is the execution process of nginx startup initialization code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.