微信公衆號:鄭爾多斯
關注可瞭解更多的Nginx
知識。任何問題或建議,請公衆號留言;
關注公衆號,有趣有內涵的文章第一時間送達!linux
上一篇文章咱們詳細的講解了auto/init
文件,該文件主要是初始化一些文件目錄,便於後面的編譯過程。
configure
執行auto/init
以後就會執行auto/sources
文件,因此本文分析一下auto/sources
文件,這個文件雖然內容不少,可是結構很是簡單,所有是初始化操做,爲後面的Makefile
文件生成各類依賴。好比編譯Core
模塊用到的依賴,編譯pcre
模塊用到的依賴等等。nginx
該文件主要初始化了一下幾部份內容:
web
首先定義了一個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
複製代碼
這個模塊是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
相關的內容了。
定義了一些基礎的模塊,是必需要編譯到nginx
中的內容,包括ngx_http_module
,ngx_http_core_module
,ngx_http_log_module
,ngx_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
開發的用來分析C/C++
程序性能的一套工具,這裏的性能分析主要包括內存和CPU
兩個方面,內存分析使用google-perftool
所提供的tcmalloc
,CPU
分析使用它所提供的profiler
。
這裏簡單瞭解一下就好了,和nginx
的實現無關。
1NGX_GOOGLE_PERFTOOLS_MODULE=ngx_google_perftools_module
2NGX_GOOGLE_PERFTOOLS_SRCS=src/misc/ngx_google_perftools_module.c
複製代碼
這個就是一個測試模塊,測試是否支持C++
內嵌C
語法,沒什麼做用(至少我沒發現有啥做用)。
一大篇文章,詳細的分析了auto/sources
模塊的功能,但願對你有所幫助。請繼續關注後續文章。