Nginx源碼分析之--auto/sources

微信公衆號:鄭爾多斯
關注可瞭解更多的Nginx知識。任何問題或建議,請公衆號留言;
關注公衆號,有趣有內涵的文章第一時間送達!linux

回顧

上一篇文章咱們詳細的講解了auto/init文件,該文件主要是初始化一些文件目錄,便於後面的編譯過程。
configure執行auto/init以後就會執行auto/sources文件,因此本文分析一下auto/sources文件,這個文件雖然內容不少,可是結構很是簡單,所有是初始化操做,爲後面的Makefile文件生成各類依賴。好比編譯Core模塊用到的依賴,編譯pcre模塊用到的依賴等等。nginx

auto/sources

該文件主要初始化了一下幾部份內容:
web

auto-source一覽
auto-source一覽

下面咱們分析一下各個模塊的內容。

一、核心模塊CORE

模塊名

首先定義了一個CORE_MODULES變量,這個變量表示Nginx的核心模塊,從定義中能夠看出來,它包括了ngx_core_module, ngx_errlog_module,以及ngx_conf_module這三個模塊。正則表達式

1CORE_MODULES="ngx_core_module ngx_errlog_module ngx_conf_module"
複製代碼
頭文件

定義了CORE_INCS表示核心模塊的頭文件所在目錄。windows

1CORE_INCS="src/core"
複製代碼
依賴文件

定義了一個CORE_DEPS(depandencies:依賴)變量表示核心模塊的依賴文件。編譯核心模塊的時候要使用到這些依賴文件。bash

 1CORE_DEPS="src/core/nginx.h \
2           src/core/ngx_config.h \
3           src/core/ngx_core.h \
4           src/core/ngx_log.h \
5           src/core/ngx_palloc.h \
6           src/core/ngx_array.h \
7           src/core/ngx_list.h \
8           src/core/ngx_hash.h \
9           src/core/ngx_buf.h \
10           src/core/ngx_queue.h \
11           src/core/ngx_string.h \
12           src/core/ngx_parse.h \
13           src/core/ngx_inet.h \
14           src/core/ngx_file.h \
15           src/core/ngx_crc.h \
16           src/core/ngx_crc32.h \
17           src/core/ngx_murmurhash.h \
18           src/core/ngx_md5.h \
19           src/core/ngx_sha1.h \
20           src/core/ngx_rbtree.h \
21           src/core/ngx_radix_tree.h \
22           src/core/ngx_slab.h \
23           src/core/ngx_times.h \
24           src/core/ngx_shmtx.h \
25           src/core/ngx_connection.h \
26           src/core/ngx_cycle.h \
27           src/core/ngx_conf_file.h \
28           src/core/ngx_resolver.h \
29           src/core/ngx_open_file_cache.h \
30           src/core/ngx_crypt.h"

複製代碼
核心模塊的源文件

定義了CORE_SRCS變量表示核心模塊的源文件路徑,這些源文件就是實現核心模塊的代碼。服務器

 1CORE_SRCS="src/core/nginx.c \
2           src/core/ngx_log.c \
3           src/core/ngx_palloc.c \
4           src/core/ngx_array.c \
5           src/core/ngx_list.c \
6           src/core/ngx_hash.c \
7           src/core/ngx_buf.c \
8           src/core/ngx_queue.c \
9           src/core/ngx_output_chain.c \
10           src/core/ngx_string.c \
11           src/core/ngx_parse.c \
12           src/core/ngx_inet.c \
13           src/core/ngx_file.c \
14           src/core/ngx_crc32.c \
15           src/core/ngx_murmurhash.c \
16           src/core/ngx_md5.c \
17           src/core/ngx_rbtree.c \
18           src/core/ngx_radix_tree.c \
19           src/core/ngx_slab.c \
20           src/core/ngx_times.c \
21           src/core/ngx_shmtx.c \
22           src/core/ngx_connection.c \
23           src/core/ngx_cycle.c \
24           src/core/ngx_spinlock.c \
25           src/core/ngx_cpuinfo.c \
26           src/core/ngx_conf_file.c \
27           src/core/ngx_resolver.c \
28           src/core/ngx_open_file_cache.c \
29           src/core/ngx_crypt.c"

複製代碼

二、正則表達式模塊

正則表達式(PCRE)模塊是Nginx實現rewrite模塊的關鍵,在auto/sources中定義了依賴文件和源文件兩個變量來編譯正則表達式。微信

1REGEX_DEPS=src/core/ngx_regex.h
2REGEX_SRCS=src/core/ngx_regex.c
複製代碼

三、OpenSSL模塊

這個模塊是ssl加密模塊,用來實現HTTPS加密。定義了三個變量來編譯該模塊,語法同CORE模塊。併發

1OPENSSL_MODULE=ngx_openssl_module
2OPENSSL_DEPS=src/event/ngx_event_openssl.h
3OPENSSL_SRCS=src/event/ngx_event_openssl.c
複製代碼

四、事件模塊

事件模塊是nginx很是重要的模塊,咱們都知道nginx的高併發就是由於使用了優秀的I/O模塊。nginx爲了可移植性,定義了一個事件框架的模型,在不一樣的平臺上面可使用不一樣的事件處理機制,一般在Linux環境下,咱們都是用epoll事件機制。
一樣的,auto/sources定義了幾個變量分別表示事件模塊的模塊名,頭文件,依賴文件和源碼文件,能夠參考CORE_MODULE的分析。app

 1EVENT_MODULES="ngx_events_module ngx_event_core_module"
2
3EVENT_INCS="src/event src/event/modules"
4
5EVENT_DEPS="src/event/ngx_event.h \
6            src/event/ngx_event_timer.h \
7            src/event/ngx_event_posted.h \
8            src/event/ngx_event_busy_lock.h \
9            src/event/ngx_event_connect.h \
10            src/event/ngx_event_pipe.h"

11
12EVENT_SRCS="src/event/ngx_event.c \
13            src/event/ngx_event_timer.c \
14            src/event/ngx_event_posted.c \
15            src/event/ngx_event_busy_lock.c \
16            src/event/ngx_event_accept.c \
17            src/event/ngx_event_connect.c \
18            src/event/ngx_event_pipe.c"

複製代碼

五、事件驅動模塊

在上面的第4步中,咱們提到了爲了在多個平臺上運行,Nginx實現了一套事件機制,這樣用戶就能夠根據當前的平臺選擇不一樣的事件處理方法了。好比在Mac上面可使用kqueue,在linux上面可使用select,epoll等。Nginx自帶了不少種事件處理模塊的實現,咱們能夠根據本身的需求選用不用的模塊。
每種模塊的定義方式都是相同的。

 1#select 模塊
2SELECT_MODULE=ngx_select_module
3SELECT_SRCS=src/event/modules/ngx_select_module.c
4WIN32_SELECT_SRCS=src/event/modules/ngx_win32_select_module.c
5
6### poll模塊
7POLL_MODULE=ngx_poll_module
8POLL_SRCS=src/event/modules/ngx_poll_module.c
9
10# kqueue模塊
11KQUEUE_MODULE=ngx_kqueue_module
12KQUEUE_SRCS=src/event/modules/ngx_kqueue_module.c
13
14### devpoll模塊
15DEVPOLL_MODULE=ngx_devpoll_module
16DEVPOLL_SRCS=src/event/modules/ngx_devpoll_module.c
17
18EVENTPORT_MODULE=ngx_eventport_module
19EVENTPORT_SRCS=src/event/modules/ngx_eventport_module.c
20
21EPOLL_MODULE=ngx_epoll_module
22EPOLL_SRCS=src/event/modules/ngx_epoll_module.c
23
24RTSIG_MODULE=ngx_rtsig_module
25RTSIG_SRCS=src/event/modules/ngx_rtsig_module.c
26
27# windows平臺使用iocp模塊
28IOCP_MODULE=ngx_iocp_module
29IOCP_SRCS=src/event/modules/ngx_iocp_module.c
30
31### aio模塊,這是linux平臺的一種文件異步處理機制
32AIO_MODULE=ngx_aio_module
33AIO_SRCS="src/event/modules/ngx_aio_module.c \
34          src/os/unix/ngx_aio_read.c \
35          src/os/unix/ngx_aio_write.c \
36          src/os/unix/ngx_aio_read_chain.c \
37          src/os/unix/ngx_aio_write_chain.c"

38
39FILE_AIO_SRCS="src/os/unix/ngx_file_aio_read.c"
40LINUX_AIO_SRCS="src/os/unix/ngx_linux_aio_read.c"
複製代碼

六、操做系統相關模塊

對於不一樣的平臺,nginx都實現了一種平臺特定代碼,用於最底層的數據傳輸等。咱們只看unix/linux相關的部分,其他的平臺也是同樣的。其實也沒啥要說的哦,就是定義了一些依賴文件,頭文件,源文件的目錄。

 1UNIX_INCS="$CORE_INCS $EVENT_INCS src/os/unix"
2
3UNIX_DEPS="$CORE_DEPS $EVENT_DEPS \
4            src/os/unix/ngx_time.h \
5            src/os/unix/ngx_errno.h \
6            src/os/unix/ngx_alloc.h \
7            src/os/unix/ngx_files.h \
8            src/os/unix/ngx_channel.h \
9            src/os/unix/ngx_shmem.h \
10            src/os/unix/ngx_process.h \
11            src/os/unix/ngx_setproctitle.h \
12            src/os/unix/ngx_atomic.h \
13            src/os/unix/ngx_gcc_atomic_x86.h \
14            src/os/unix/ngx_thread.h \
15            src/os/unix/ngx_socket.h \
16            src/os/unix/ngx_os.h \
17            src/os/unix/ngx_user.h \
18            src/os/unix/ngx_process_cycle.h"

19
20# add to UNIX_DEPS
21#            src/os/unix/ngx_gcc_atomic_amd64.h \
22#            src/os/unix/ngx_gcc_atomic_sparc64.h \
23#            src/os/unix/ngx_gcc_atomic_ppc.h \
24#            src/os/unix/ngx_sunpro_atomic_sparc64.h \
25#            src/os/unix/ngx_sunpro_x86.il \
26#            src/os/unix/ngx_sunpro_amd64.il \
27#            src/os/unix/ngx_sunpro_sparc64.il \
28
29
30UNIX_SRCS="$CORE_SRCS $EVENT_SRCS \
31            src/os/unix/ngx_time.c \
32            src/os/unix/ngx_errno.c \
33            src/os/unix/ngx_alloc.c \
34            src/os/unix/ngx_files.c \
35            src/os/unix/ngx_socket.c \
36            src/os/unix/ngx_recv.c \
37            src/os/unix/ngx_readv_chain.c \
38            src/os/unix/ngx_udp_recv.c \
39            src/os/unix/ngx_send.c \
40            src/os/unix/ngx_writev_chain.c \
41            src/os/unix/ngx_channel.c \
42            src/os/unix/ngx_shmem.c \
43            src/os/unix/ngx_process.c \
44            src/os/unix/ngx_daemon.c \
45            src/os/unix/ngx_setproctitle.c \
46            src/os/unix/ngx_posix_init.c \
47            src/os/unix/ngx_user.c \
48            src/os/unix/ngx_process_cycle.c"

複製代碼

linux相關的內容以下:

1LINUX_DEPS="src/os/unix/ngx_linux_config.h src/os/unix/ngx_linux.h"
2LINUX_SRCS=src/os/unix/ngx_linux_init.c
3LINUX_SENDFILE_SRCS=src/os/unix/ngx_linux_sendfile_chain.c
複製代碼

七、HTTP模塊

下面的重頭戲來了,開始定義HTTP相關的內容了。

基礎的模塊

定義了一些基礎的模塊,是必需要編譯到nginx中的內容,包括ngx_http_modulengx_http_core_modulengx_http_log_modulengx_http_upstream_module,以下:

1# the http modules that have their logging formats
2# must be after ngx_http_log_module
3
4HTTP_MODULES="ngx_http_module \
5              ngx_http_core_module \
6              ngx_http_log_module \
7              ngx_http_upstream_module"

複製代碼

以及HTTP模塊的頭文件,依賴文件和源代碼文件路徑,以下:

 1HTTP_INCS="src/http src/http/modules"
2
3HTTP_DEPS="src/http/ngx_http.h \
4           src/http/ngx_http_request.h \
5           src/http/ngx_http_config.h \
6           src/http/ngx_http_core_module.h \
7           src/http/ngx_http_cache.h \
8           src/http/ngx_http_variables.h \
9           src/http/ngx_http_script.h \
10           src/http/ngx_http_upstream.h \
11           src/http/ngx_http_upstream_round_robin.h \
12           src/http/ngx_http_busy_lock.h"

13
14HTTP_SRCS="src/http/ngx_http.c \
15           src/http/ngx_http_core_module.c \
16           src/http/ngx_http_special_response.c \
17           src/http/ngx_http_request.c \
18           src/http/ngx_http_parse.c \
19           src/http/ngx_http_header_filter_module.c \
20           src/http/ngx_http_write_filter_module.c \
21           src/http/ngx_http_copy_filter_module.c \
22           src/http/modules/ngx_http_log_module.c \
23           src/http/ngx_http_request_body.c \
24           src/http/ngx_http_variables.c \
25           src/http/ngx_http_script.c \
26           src/http/ngx_http_upstream.c \
27           src/http/ngx_http_upstream_round_robin.c \
28           src/http/ngx_http_parse_time.c \
29           src/http/modules/ngx_http_static_module.c \
30           src/http/modules/ngx_http_index_module.c \
31           src/http/modules/ngx_http_chunked_filter_module.c \
32           src/http/modules/ngx_http_range_filter_module.c \
33           src/http/modules/ngx_http_headers_filter_module.c \
34           src/http/modules/ngx_http_not_modified_filter_module.c"

複製代碼
自定義模塊

剩下的是一些自定義模塊,咱們在configure的時候能夠選擇是否編譯,若是不編譯,那麼在生成的Makefile中就不會生成相應的代碼,咱們隨便找一個分析一下:

1HTTP_REALIP_MODULE=ngx_http_realip_module
2HTTP_REALIP_SRCS=src/http/modules/ngx_http_realip_module.c
複製代碼

上面的模塊是獲取客戶端真實ip的模塊,若是咱們在configure的時候選擇編譯該模塊,那麼生成Makefile的時候就會將相應的源碼和依賴包含進去。

八、郵件模塊

這部分忽略,由於我歷來沒用過nginx當郵件服務器。
可是它的格式和其餘模塊如出一轍,能夠參考上面的東東自行分析該部分。

九、Google Perftool

Google Perftool它是由google開發的用來分析C/C++程序性能的一套工具,這裏的性能分析主要包括內存和CPU兩個方面,內存分析使用google-perftool所提供的tcmallocCPU分析使用它所提供的profiler
這裏簡單瞭解一下就好了,和nginx的實現無關。

1NGX_GOOGLE_PERFTOOLS_MODULE=ngx_google_perftools_module
2NGX_GOOGLE_PERFTOOLS_SRCS=src/misc/ngx_google_perftools_module.c
複製代碼

十、C++測試模塊

這個就是一個測試模塊,測試是否支持C++內嵌C語法,沒什麼做用(至少我沒發現有啥做用)。

下篇內容

一大篇文章,詳細的分析了auto/sources模塊的功能,但願對你有所幫助。請繼續關注後續文章。



喜歡本文的朋友們,歡迎長按下圖關注訂閱號鄭爾多斯,更多精彩內容第一時間送達
鄭爾多斯
鄭爾多斯
相關文章
相關標籤/搜索