微信公众号:郑尔多斯
关注可了解更多的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
模块的功能,但愿对你有所帮助。请继续关注后续文章。