Nginx源码分析之--auto/sources

645 阅读13分钟

微信公众号:郑尔多斯
关注可了解更多的Nginx知识。任何问题或建议,请公众号留言;
关注公众号,有趣有内涵的文章第一时间送达!

回顾

上一篇文章我们详细的讲解了auto/init文件,该文件主要是初始化一些文件目录,便于后面的编译过程。
configure执行auto/init之后就会执行auto/sources文件,所以本文分析一下auto/sources文件,这个文件虽然内容很多,但是结构非常简单,全部是初始化操作,为后面的Makefile文件生成各种依赖。比如编译Core模块用到的依赖,编译pcre模块用到的依赖等等。

auto/sources

该文件主要初始化了一下几部分内容:

auto-source一览
auto-source一览

下面我们分析一下各个模块的内容。

1、核心模块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表示核心模块的头文件所在目录。

1CORE_INCS="src/core"
依赖文件

定义了一个CORE_DEPS(depandencies:依赖)变量表示核心模块的依赖文件。编译核心模块的时候要使用到这些依赖文件。

 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"

2、正则表达式模块

正则表达式(PCRE)模块是Nginx实现rewrite模块的关键,在auto/sources中定义了依赖文件和源文件两个变量来编译正则表达式。

1REGEX_DEPS=src/core/ngx_regex.h
2REGEX_SRCS=src/core/ngx_regex.c

3、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

4、事件模块

事件模块是nginx非常重要的模块,我们都知道nginx的高并发就是因为使用了优秀的I/O模块。nginx为了可移植性,定义了一个事件框架的模型,在不同的平台上面可以使用不同的事件处理机制,通常在Linux环境下,我们都是用epoll事件机制。
同样的,auto/sources定义了几个变量分别表示事件模块的模块名,头文件,依赖文件和源码文件,可以参考CORE_MODULE的分析。

 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"

5、事件驱动模块

在上面的第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"

6、操作系统相关模块

对于不同的平台,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

7、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的时候就会将相应的源码和依赖包含进去。

8、邮件模块

这部分忽略,因为我从来没用过nginx当邮件服务器。
但是它的格式和其他模块一模一样,可以参考上面的东东自行分析该部分。

9、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

10、C++测试模块

这个就是一个测试模块,测试是否支持C++内嵌C语法,没什么作用(至少我没发现有啥作用)。

下篇内容

一大篇文章,详细的分析了auto/sources模块的功能,希望对你有所帮助。请继续关注后续文章。



喜欢本文的朋友们,欢迎长按下图关注订阅号郑尔多斯,更多精彩内容第一时间送达
郑尔多斯
郑尔多斯