1.5.3 configure生成的文件

当configure执行成功时会生成objs目录,并在该目录下产生以下目录和文件:


|—-ngx_auto_headers.h

|—-autoconf.err

|—-ngx_auto_config.h

|—-ngx_modules.c

|—-src

||—-core

||—-event

|||—-modules

||—-os

|||—-unix

|||—-win32

||—-http

|||—-modules

||||—-perl

||—-mail

||—-misc

|—-Makefile


上述目录和文件介绍如下。

1)src目录用于存放编译时产生的目标文件。

2)Makefile文件用于编译Nginx工程以及在加入install参数后安装Nginx。

3)autoconf.err保存configure执行过程中产生的结果。

4)ngx_auto_headers.h和ngx_auto_config.h保存了一些宏,这两个头文件会被src/core/ngx_config.h及src/os/unix/ngx_linux_config.h文件(可将"linux"替换为其他UNIX操作系统)引用。

5)ngx_modules.c是一个关键文件,我们需要看看它的内部结构。一个默认配置下生成的ngx_modules.c文件内容如下:


include<ngx_config.h>

include<ngx_core.h>

……

ngx_module_t*ngx_modules[]={

&ngx_core_module,

&ngx_errlog_module,

&ngx_conf_module,

&ngx_events_module,

&ngx_event_core_module,

&ngx_epoll_module,

&ngx_http_module,

&ngx_http_core_module,

&ngx_http_log_module,

&ngx_http_upstream_module,

&ngx_http_static_module,

&ngx_http_autoindex_module,

&ngx_http_index_module,

&ngx_http_auth_basic_module,

&ngx_http_access_module,

&ngx_http_limit_zone_module,

&ngx_http_limit_req_module,

&ngx_http_geo_module,

&ngx_http_map_module,

&ngx_http_split_clients_module,

&ngx_http_referer_module,

&ngx_http_rewrite_module,

&ngx_http_proxy_module,

&ngx_http_fastcgi_module,

&ngx_http_uwsgi_module,

&ngx_http_scgi_module,

&ngx_http_memcached_module,

&ngx_http_empty_gif_module,

&ngx_http_browser_module,

&ngx_http_upstream_ip_hash_module,

&ngx_http_write_filter_module,

&ngx_http_header_filter_module,

&ngx_http_chunked_filter_module,

&ngx_http_range_header_filter_module,

&ngx_http_gzip_filter_module,

&ngx_http_postpone_filter_module,

&ngx_http_ssi_filter_module,

&ngx_http_charset_filter_module,

&ngx_http_userid_filter_module,

&ngx_http_headers_filter_module,

&ngx_http_copy_filter_module,

&ngx_http_range_body_filter_module,

&ngx_http_not_modified_filter_module,

NULL

};


ngx_modules.c文件就是用来定义ngx_modules数组的。

ngx_modules是非常关键的数组,它指明了每个模块在Nginx中的优先级,当一个请求同时符合多个模块的处理规则时,将按照它们在ngx_modules数组中的顺序选择最靠前的模块优先处理。对于HTTP过滤模块而言则是相反的,因为HTTP框架在初始化时,会在ngx_modules数组中将过滤模块按先后顺序向过滤链表中添加,但每次都是添加到链表的表头,因此,对HTTP过滤模块而言,在ngx_modules数组中越是靠后的模块反而会首先处理HTTP响应(参见第6章及第11章的11.9节)。

因此,ngx_modules中模块的先后顺序非常重要,不正确的顺序会导致Nginx无法工作,这是auto/modules脚本执行后的结果。读者可以体会一下上面的ngx_modules中同一种类型下(第8章会介绍模块类型,第10章、第11章将介绍的HTTP框架对HTTP模块的顺序是最敏感的)各个模块的顺序以及这种顺序带来的意义。

可以看出,在安装过程中,configure做了大量的幕后工作,我们需要关注在这个过程中Nginx做了哪些事情。configure除了寻找依赖的软件外,还针对不同的UNIX操作系统做了许多优化工作。这是Nginx跨平台的一种具体实现,也体现了Nginx追求高性能的一贯风格。

configure除了生成Makefile外,还生成了ngx_modules.c文件,它决定了运行时所有模块的优先级(在编译过程中而不是编码过程中)。对于不需要的模块,既不会加入ngx_modules数组,也不会编译进Nginx产品中,这也体现了轻量级的概念。