Analysis of nginx code (1) -- Preliminary Exploration

Source: Internet
Author: User
I was curious to find that nginx accidentally saw a Sina error page "nginx..." in the browser. Google found that this is a reverse proxy server that supports Server Load balancer. Developed by Russians, although it does not use the GNU or BSD license, it is also an open source software.

Confirm with the tool, Sina Blog should be used nginx is correct, the following is the result of the execution of curl-I http://blog.sina.com.cn/
HTTP/1.1 200 OK
Via: 1.1 isaserver
Connection: keep-alive
Proxy-connection: keep-alive
Content-Length: 365631
Expires: Wed, 07 Nov 2007 09:31:17 GMT
Date: Wed, 07 Nov 2007 09:26:17 GMT
Content-Type: text/html
Server: nginx/0.5.32
~~~~~~~~~~~~~~~~ This line
Last-modified: Wed, 07 Nov 2007 09:18:01 GMT
Cache-control: Max-age = 300
X-via: Tj-206
Accept-ranges: bytes
X-via: proxy by Tj-197

Squid and Apache are the most familiar open-source proxy servers, but both of them are common positive and reverse proxies. As reverse proxies, they are actually quite different from forward proxies. I think since Sina also uses it, it naturally has its unique characteristics. After checking, the Chinese webpage says that its HTTP performance can reach more than TPS, but it does not indicate the source of the data. The corresponding data cannot be found on foreign websites for the time being, but many people compare it with Lighttpd.

Soon I downloaded the nginx 0.5.32 version of Code. There are not many lines of code, only over 80 thousand lines, and HTTPS is supported based on OpenSSL. Compared with Apache's more than 0.3 million lines, it is much simpler,

As a web server or reverse proxy, it requires a fast speed. In addition to simplified code, a more critical point is the concurrency model.

Apache's weakness lies in that its concurrency model is a common process/thread pool, and the number of connections and the number of processes/threads are. Therefore, whether it is in prefork or worker mode, each connection is mapped to an independent process/thread.

This concurrency model is acceptable when the number of connections is not too large (less than 1000), but the total number of processes/threads will be very large during large-scale concurrency. Because Apache itself also eats a lot of memory, when more than 1000 of the concurrency is reached, the server's memory will basically be eaten, and the operating system is also switching the process/thread frequently, very difficult.

In contrast, more advanced large-scale network service systems (such as telecom Intelligent Network Systems) generally adopt the process/thread pool + state machine model-that is, the number of connections and the number of processes/Threads is m: n. In this way, the total number of processes/threads will not increase due to the increase in connections, avoiding the overhead of memory and scheduling switching. However, this method has higher requirements on program logic, A connection needs to be split into multiple logical states (creation, read, write, and close, which can be further refined based on the actual business). After each process/thread finishes processing a certain state, the status value of the connection needs to be changed, and the subsequent status is processed by the next idle process/thread.

Nginx adopts this concurrency model. For the storage of connection status, nginx mainly adopts this complex structure.
Struct ngx_connection_s {
Void * data;
Ngx_event_t * read;
Ngx_event_t * write;
...
};

The structure ngx_event_t stores detailed information about the connection Io status, and all ngx_event_t forms two global linked lists for access operations.

Based on the two data structures, nginx uses the following two functions to complete the loop of each process/thread.
1. ngx_locked_post_event
This function updates the status of a connection and is called after the connection I/O status changes (such as through select) are checked.

Nginx provides multiple implementations of select semantics in module mode:
Poll
Devpoll
Epoll
Eventport
Kqueue
Rtsig
The last four types are the asynchronous Io models provided by BSD/Linux to accelerate Io operations.

2. ngx_event_thread_process_posted
This function checks the event table and calls the handler function corresponding to the event to process one event at a time.

These two functions are combined to implement the most basic M: N concurrency model.

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.