Not free – Nginx source global awareness

I don’t know how many days later, I finally wrote this part of the content.

Originally during the National Day to write some content, but found me quite naive, basically lying dead in bed watching others travel…

To take it back, keep reading our nginx source code…

Personally, before reading the source code, to understand the entire nginx architecture, or the overall structure, and Nginx source directory structure is very clear, easy to learn or read want to see the module.

Note: this time read nginx source version is 0.5

First we type a terminal command: tree-l 1

↳ tree-L 1    22:08:02 . ├── Core ├─ Event ├─ HTTP ├─ Mail ├─ misc ├─ OS 6 Directories, 0 filesCopy the code

What does each module do

  • Core: nginx core source code, including common data structures and kernel implementation of the core code, such as red black tree, queue, etc
  • Event: NGINx event-driven model, and timer implementation related code;
  • HTTP: nginx implementation of HTTP server related code, after the heavy play!
  • Mail: Nginx to achieve mail proxy server related code;
  • Misc: auxiliary code that tests compatibility with C++ headers and support for Google_PerfTools;
  • OS: encapsulate system functions provided by different architectures, and provide a unified system call interface externally;

At the beginning, we mainly read core, Event, HTTP three core modules, in fact, these three modules are also important components of Nginx.

The overall architecture

Before reading the source code or debugging, first of all, to understand the global knowledge of nginx source code, to understand what components nginx contains what functions, so I drew a diagram to facilitate understanding.

  • Core module functions: provide some basic functions for other modules: string processing, time management, file reading and writing functions (no matter what the framework source code, will contain some basic tools or processing functions);
  • Configuration parsing: mainly includes file syntax check, configuration parameter parsing, parameter initialization and other functions. (Generally good frameworks provide the user with the appropriate usage configuration, so this part is a necessity.)
  • Memory management: Memory pool management, shared memory allocation, buffer management and other functions; (Memory management is a guarantee for any framework)
  • Event-driven: process creation and management, signal reception and processing, implementation of all event-driven models, advanced IO and other functions; (So does the core of Nginx, including the core of Redis.)
  • Log management: error log generation and management, task log generation and management and other functions; (The familiar log management, without it, how can we troubleshoot errors?)
  • HTTP service: Provides Web services, including client connection management, client request processing, virtual host management, server group management and other functions. (Isn’t that what Nginx does?)
  • Mail service: similar to HTTP service, but with the addition of Mail protocol implementation; (e.g. 25, 465, etc.)

core

The source code in the core directory defines the most basic data structure of the Nginx server and the most basic core module that provides basic functionality for common calls to other modules. First take a look at the source structure of the core module:

Note: part of xxx.h is omitted, just because c is present, some of H is paired, and the experience is not very friendly if the picture is too large…

As can be seen from this part of the figure, the nginx core directory mainly contains some efficient data structures and some configuration, log, connection, memory and other management files.

This section will be explained in detail later

event

The Event directory contains a module subdirectory and some files. In addition to the Module subdirectory, other files provide the definition, initialization, event receiving, passing, management, and event-driven model invocation functions. Module subdirectory in the source code implementation of Nginx support event-driven model: AIO, epoll, kqueue, select, /dev/poll, poll and other event-driven model;

Note: part xxx.h has been omitted. As you can see from the figure above, the Event directory mainly contains the implementation of the event-driven model and the encapsulation of event management, which will be explained in detail in the following sections.

http

HTTP directory and event directory, generally contains module implementation source module directory file and some structure definition, initialization, network connection establishment, management, closing, as well as datagram parsing, server group management and other functions of the source file. The module directory file implements the functions of the HTTP module.

├── Modules ├─ ngx_http_busy_lock.c ├─ ngx_http_busy_lock.h ├─ ngx_http_cache.h ├─ ngx_http_config.h School exercises ── School exercises for ngx_http_copy_filter_module.c School exercises ── School exercises ─ school exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ School Exercises School exercises ── School Exercises for http_request.h school exercises ── School Exercises for http_request.h School Exercises ── School exercises ── School exercises ── School exercises ─ School exercises ─ School exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ School exercises ── School exercises ─ school exercises ─ School exercises ─ School exercises ─ School exercises ─ School Exercises ─ School Exercises ─ School Exercises School exercises ── School exercises ─ school exercises ─ school exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ School Exercises ─ Ngx_http_variables. H └ ─ ─ ngx_http_write_filter_module. C 1 directory, 32 filesCopy the code

From the above listed files can come out, Nginx source code reflects the modularity, so easy to read, secondly can also see for HTTP some resolution, DNS load balancing and other packaging and implementation between this.

summary

The above is for nginx source code overall cognition, the next part will be a brief explanation of nginx configuration files.

Read the source code of excellent framework, don’t be impatient, settle down, like a cup of tea, slowly taste and appreciate…

See you next time…

reference

  • Tc. Dreamcat. Ink/archives / 20…
  • www.kancloud.cn/digest/unde…
  • Blog.aofall.com/archives/6….