Nginx source code analysis-listener socket creation socket listening HTTP request creation connection

Source: Internet
Author: User

As a webserver, there must be a listening socket. This listening socket is used to receive HTTP requests. The creation of this listening socket is based on the content of the configuration file, in nginx. the number of listening sockets must be created for each address in the conf file. Let's not talk about the structure of each struct here. It's just about the general situation!

1) The ngx_init_cycle () function is called in the main function, and the ngx_open_listening_sockets function is called at the end of the function, this function is responsible for setting SOCKET options for the created listening socket (for example, non-blocking, sending buffer, binding, and listening processing)

2) Where can I create a socket? When parsing the HTTP {] configuration, that is, in the ngx_http_block () function, the ngx_http_optimize_servers () function is called at the end of the function.

Ngx_http_init_listening () function, which calls the ngx_http_add_listening function. In this function, the ngx_create_listening () function is called. This function creates a listening socket based on the configuration of each IP Address: port, another very important task of this function is to set the callback function of the listening socket to the ngx_http_init_connection function. Remember that this is the callback function of the listening socket rather than the callback function of the corresponding readable event of the listening socket.

3) When will the connection established by the HTTP request be accepted? In the ngx_event_process_init () function, this function is an init_process function created as the ngx_event_core_module module. This function is called during worker process initialization, the ngx_event_process_init function creates a relationship between each listening socket and a connection (ngx_connection_t. Create a connection pool in cycle, create a read event pool, create a write event pool, and create a for loop to traverse all ngx_listening_t struct in cycle. For each ngx_listening_t struct, that is, each listener socket obtains a connection from the connection pool, connects the listener socket, and sets the read event to ngx_event_accept, in this case, accept accepts the new connection on the corresponding listening socket!

4) after the connection is complete, call the handler on the listening socket, that is, the ngx_http_init_connection function, starting with the interaction of HTTP requests!


(Conclusion: For an initialized ngx_listening_t, there is only one socket and there is no readable and writable event. It needs to be hosted by ngx_connection_t, And the readable and writable event in ngx_connection_t is in the ngx_event_t structure, data corresponds to an ngx_connection_t struct)

Nginx source code analysis-listener socket creation socket listening HTTP request creation connection

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.