编译
先装依赖库
$ unzip pcre-8.33.zip
$ cd pcre-8.33
$ ./configure
$ make; make install
configure
命令做了大量的“幕后”工作, 包括检测操作系统内核和已经安装的软件,参数的解析,中间目录的生成以及根据各种参数生成一些 C 源码文件、Makefile 文件等。make命令根据configure命令生成的 Makefile 文件编译 Nginx 工程, 并生成目标文件、 最终的二进制文件。make install 命令根据 configure 执行时的参数将 Nginx 部署到指定的安装目录,包括相关目录的建立和二进制文件、 配置文件的复制。
可以看出, configure 命令至关重要, 下文将详细介绍如何使用 configure 命令, 并分析 configure 到底是如何工作的,从中我们也可以看出 Nginx 的一些设计思想。
configure的命令参数
使用help命令可以查看configure包含的参数。
wu@ubuntu:~/opt/nginx-1.0.15$ ./configure --help
1、路径相关的参数
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
--with-perl_modules_path=PATH set Perl modules path
--with-perl=PATH set perl binary pathname
--http-log-path=PATH set http access log pathname
--http-client-body-temp-path=PATH set path to store
http client request body temporary files
--http-proxy-temp-path=PATH set path to store
http proxy temporary files
--http-fastcgi-temp-path=PATH set path to store
http fastcgi temporary files
--http-uwsgi-temp-path=PATH set path to store
http uwsgi temporary files
--http-scgi-temp-path=PATH set path to store
http scgi temporary files
2、编译相关的参数
--with-cc=PATH set C compiler pathname
--with-cpp=PATH set C preprocessor pathname
--with-cc-opt=OPTIONS set additional C compiler options
--with-ld-opt=OPTIONS set additional linker options
--with-cpu-opt=CPU build for the specified CPU, valid values:
3、依赖软件的相关参数
PCRE的设置参数
--without-pcre disable PCRE library usage
--with-pcre force PCRE library usage
--with-pcre=DIR set path to PCRE library sources
--with-pcre-opt=OPTIONS set additional build options for PCRE
OpenSSL的设置参数
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL
原子库的设置参数
--with-libatomic force libatomic_ops library usage
--with-libatomic=DIR set path to libatomic_ops library sources
散列函数库的设置参数
--with-md5=DIR set path to md5 library sources
--with-md5-opt=OPTIONS set additional build options for md5
--with-md5-asm use md5 assembler sources
--with-sha1=DIR set path to sha1 library sources
--with-sha1-opt=OPTIONS set additional build options for sha1
--with-sha1-asm use sha1 assembler sources
zlib库的设置参数
--with-zlib=DIR set path to zlib library sources
--with-zlib-opt=OPTIONS set additional build options for zlib
--with-zlib-asm=CPU use zlib assembler sources optimized
4、模块相关的参数
除了少量核心代码外, Nginx完全是由各种功能模块组成的。 这些模块会根据配置参数决定自己的行为, 因此, 正确地使用各个模块非常关键。 在configure的参数中, 我们把它们分为五大类:
( 1) 事件模块
下面列出了Nginx可以选择哪些事件模块编译到产品中:
--with-rtsig_module enable rtsig module
--with-select_module enable select module
--without-select_module disable select module
--with-poll_module enable poll module
--without-poll_module disable poll module
(2)默认即编译进入Nginx的HTTP模块
--without-http_charset_module disable ngx_http_charset_module
--without-http_gzip_module disable ngx_http_gzip_module
--without-http_ssi_module disable ngx_http_ssi_module
--without-http_userid_module disable ngx_http_userid_module
--without-http_access_module disable ngx_http_access_module
--without-http_auth_basic_module disable ngx_http_auth_basic_module
--without-http_autoindex_module disable ngx_http_autoindex_module
--without-http_geo_module disable ngx_http_geo_module
--without-http_map_module disable ngx_http_map_module
--without-http_split_clients_module disable ngx_http_split_clients_module
--without-http_referer_module disable ngx_http_referer_module
--without-http_rewrite_module disable ngx_http_rewrite_module
--without-http_proxy_module disable ngx_http_proxy_module
--without-http_fastcgi_module disable ngx_http_fastcgi_module
--without-http_uwsgi_module disable ngx_http_uwsgi_module
--without-http_scgi_module disable ngx_http_scgi_module
--without-http_memcached_module disable ngx_http_memcached_module
--without-http_limit_zone_module disable ngx_http_limit_zone_module
--without-http_limit_req_module disable ngx_http_limit_req_module
--without-http_empty_gif_module disable ngx_http_empty_gif_module
--without-http_browser_module disable ngx_http_browser_module
--without-http_upstream_ip_hash_module disable ngx_http_upstream_ip_hash_module
(3)默认不会编译进入Nginx的HTTP模块
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module
--with-http_image_filter_module enable ngx_http_image_filter_module
--with-http_geoip_module enable ngx_http_geoip_module
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_http_mp4_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_random_index_module enable ngx_http_random_index_module
--with-http_secure_link_module enable ngx_http_secure_link_module
--with-http_degradation_module enable ngx_http_degradation_module
--with-http_stub_status_module enable ngx_http_stub_status_module
--with-google_perftools_module enable ngx_google_perftools_module
(4)邮件代理服务器相关的mail模块
--with-mail enable POP3/IMAP4/SMTP proxy module
--with-mail_ssl_module enable ngx_mail_ssl_module
--without-mail_pop3_module disable ngx_mail_pop3_module
--without-mail_imap_module disable ngx_mail_imap_module
--without-mail_smtp_module disable ngx_mail_smtp_module
(5)其他模块
--user=USER set non-privileged user for worker processes
--group=GROUP set non-privileged group for worker processes
--with-file-aio enable file AIO support
--with-ipv6 enable IPv6 support
--without-http disable HTTP server
--without-http-cache disable HTTP cache
--add-module=PATH enable an external module
--with-debug enable debug logging
configure执行流程
configure由Shell脚本编写,中间会调用<nginx-source>/auto/目录下的脚本。这里将只对configure脚本本身做分析,对于它所调用的auto目录下的其他工具脚本则只做功能性的说明。
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
| |----------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响应。
因此,ngx_modules中模块的先后顺序非常重要,不正确的顺序会导致Nginx无法工作, 这是auto/modules脚本执行后的结果。读者可以体会一下上面的ngx_modules中同一种类型下各个模块的顺序以及这种顺序带来的意义。
可以看出,在安装过程中,configure做了大量的幕后工作, 我们需要关注在这个过程中Nginx做了哪些事情。configure除了寻找依赖的软件外,还针对不同的UNIX操作系统做了许多优化工作。这是Nginx跨平台的一种具体实现,也体现了Nginx追求高性能的一贯风格。 configure除了生成Makefile外,还生成了ngx_modules.c文件, 它决定了运行时所有模块的优先级( 在编译过程中而不是编码过程中) 。对于不需要的模块,既不会加入ngx_modules数组, 也不会编译进Nginx产品中, 这也体现了轻量级的概念。
转自: http://wuyudong.com/2016/11/11/2982.html