当前位置: > Linux服务器 > nginx >

Nginx手动编译的编译选项解析

时间:2015-01-10 13:26来源:linux.it.net.cn 作者:IT网

管理操作系统肯定要面临装软件,Linux有很多种装软件的办法,而手动编译是最通用的办法。Nginx是一款非常优秀的Web服务器/反向代理服务器,具有轻量、占用资源少、支持高并发等优秀特点。正因为Nginx有着高效、多功能的特性,Nginx的编译安全的选项也是非常多的。本文根据Centos 6下的Nginx进行手工编译安装的选项进行剖析解读。

Nginx server compile list

1.分析现有的RPM包的选项

 (1) 操作系统: CentOS 6.4 x86_64 最小化安装

[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

(2) Nginx版本:

Epel yum源RPM包:nginx-1.0.15-5.el6.x86_64.rpm

Nginx官方RPM包: nginx-1.4.4-1.el6.ngx.x86_64.rpm

(3) 分析方法:

安装好版本后使用如下命令查看相应的安装选项信息:

nginx -V

(4) 分析结构:

1)epel 源Nginx版本:

--prefix=/usr/share/nginx
--sbin-path=/usr/sbin/nginx
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi
--pid-path=/var/run/nginx.pid
--lock-path=/var/lock/subsys/nginx
--user=nginx
--group=nginx
--with-file-aio
--with-ipv6
--with-http_ssl_module
--with-http_realip_module
--with-http_addition_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_stub_status_module
--with-mail
--with-mail_ssl_module

--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
--with-http_perl_module
--with-debug
--with-http_xslt_module
--with-http_image_filter_module
--with-http_geoip_module
--with-http_degradation_module
--with-ld-opt=-Wl,-E

2) 官方Nginx版本:

--prefix=/etc/nginx
--sbin-path=/usr/sbin/nginx
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
--user=nginx
--group=nginx
--with-file-aio
--with-ipv6
--with-http_ssl_module
--with-http_realip_module
--with-http_addition_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_stub_status_module
--with-mail
--with-mail_ssl_module
--with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

--with-http_gunzip_module

3) 总结:

两个RPM包的安装方式不同,但是安装原理都是相同的,都是通过rpm命令来安装的。两种版本在安装过程中除了相关文件的路径不同外,还有以上两段高亮的不同部分。当然,这两种RPM包的安装的默认选项是用来适应所有使用者的,几乎可以说是通用的Nginx编译选项,如果需要特定环境做特定的定制,还需要对Nginx编译选项有详细的了解。

2、Nginx编译选项解析:

下载Nginx官方最新稳定版本,通过 ./configure –help 查看相关的可选参数:

[root@localhost nginx-1.4.4]# ./configure --help

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

 --user=USER set non-privileged user for
 worker processes
 --group=GROUP set non-privileged group for
 worker processes

 --builddir=DIR set build directory

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

 --with-file-aio enable file AIO support
 --with-ipv6 enable IPv6 support

 --with-http_ssl_module enable ngx_http_ssl_module
 --with-http_spdy_module enable ngx_http_spdy_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_gunzip_module enable ngx_http_gunzip_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

 --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_conn_module disable ngx_http_limit_conn_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
 --without-http_upstream_least_conn_module
 disable ngx_http_upstream_least_conn_module
 --without-http_upstream_keepalive_module
 disable ngx_http_upstream_keepalive_module

 --with-http_perl_module enable ngx_http_perl_module
 --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

 --without-http disable HTTP server
 --without-http-cache disable HTTP cache

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

 --with-google_perftools_module enable ngx_google_perftools_module
 --with-cpp_test_module enable ngx_cpp_test_module

 --add-module=PATH enable an external module

 --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:
 pentium, pentiumpro, pentium3, pentium4,
 athlon, opteron, sparc32, sparc64, ppc64

 --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
 --with-pcre-jit build PCRE with JIT compilation support

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

 --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
 for the specified CPU, valid values:
 pentium, pentiumpro

 --with-libatomic force libatomic_ops library usage
 --with-libatomic=DIR set path to libatomic_ops library sources

 --with-openssl=DIR set path to OpenSSL library sources
 --with-openssl-opt=OPTIONS set additional build options for OpenSSL

 --with-debug enable debug logging

3、Nginx编译选项逐一解析:

注:以下测试均使用Centos 6.4 x64 ,Nginx官方nginx-1.4.4-1.el6.ngx.x86_64.rpm版本

注:大部分来自Nginx官方文档

–prefix=<path> – Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。这里的安装目录并非所谓的程序安装路径,而是类似Apache的WWW目录的按装路径。

–sbin-path=<path> – Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。

–conf-path=<path> – 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。

–error-log-path=<path> – 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。

–http-log-path=<path> – 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。

–pid-path=<path> – 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。pid文件只是在程序或者服务运行时会有,停止后将自动删除。pid文件内容只是当前Nginx运行的pid值

–lock-path=<path> – nginx.lock文件的路径。

–user=<user> – 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。这里虽然设置了启动用户为nginx,但是master进程的用户仍然为root,可见,限定的用户只是worker进程的用户。

–group=<group> – 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。

–builddir=<dir> – 指定编译的目录。

–with-rtsig_module – 开启RTSIG模块

–with-select_module / –without-select_module – 开启和关闭select模块。如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式

–with-poll_module / –without-poll_module – 开启和关闭poll模块,和select一样,如果在编译的时候没有更好的选择,poll将默认启动。

select、poll、epoll、Kqueue都是用来做Nginx的I/O优化的,具体配置可以在nginx.conf的events { .. }中进行配置,只要内核支持,完全可以根据自己的需求去选择相应的模型。

–with-file-aio – 开启文件aio功能。和上边提到的select等,都是I/O处理模型。“ aio 特性会大大提高性能,图片站的特点是大量的读 io 操作,nginx aio 不用等待每次 io 的结果有助于并发处理大量 io 和提高 nginx 处理效率。”(引自:http://www.vpsee.com/2010/12/nginx-with-file-aio-need-linux-kernel-support/)

–with-ipv6 – IPV6的支持(3mc2.com)

–with-http_ssl_module – 开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl-dev。

–with-http_spdy_module – 开启支持SPDY协议。SPDY是Google开发的基于传输控制协议(TCP)的应用层协议 。Google最早是在Chromium中提出的SPDY协议。目前已经被用于Google Chrome浏览器中来访问Google的SSL加密服务。SPDY当前并不是一个标准协议,但SPDY的开发组已经开始推动SPDY成为正式标准(现为互联网草案),Google Chrome,Mozilla Firefox,Opera和Internet Explorer均已支持SPDY协议。SPDY协议类似于HTTP,但旨在缩短网页的加载时间和提高安全性。SPDY协议通过压缩、多路复用和优先级来缩短加载时间。SPDY并不是首字母缩略字,而仅仅是”speedy”的缩写。SPDY现为Google的商标。(引自:http://zh.wikipedia.org/zh/SPDY)

–with-http_realip_module – 开启realip模块,从字面理解是真实IP,这里的功能是在反向代理过程中,需要将当前用户的IP地址,通过X-Forwarded-For头部来传递给后端的服务器,这样获取到的IP地址就是访问者的真实IP,而不是Nginx代理服务器的IP。

–with-http_addition_module – 开启Nginx的附加模块,功能为在页面前后添加文本,比如添加header或者footer就非常方便,估计很少人用这个功能。

–with-http_xslt_module – 在响应XML文件时,转为一个或多个XSLT样式。

–with-http_image_filter_module – 开启图片转换功能,依赖 libgd 库,能够对图片进行转换格式,尺寸等。

–with-http_geoip_module – 开启GeoIP功能,需要geoIP 数据库的支持。(3mc2.com)

–with-http_sub_module – 该功能为替换相应包指定内容,类似PHP的substr_replace()函数。

–with-http_dav_module – 开启WebDAV功能,可以通过WebDAV协议管理Web内容。此方法可以开启PUT、DELETE、COPY、MKCOL、MOVE等HTTP方法。(此模块具有一定的危险性,谨慎开启)

–with-http_flv_module – 开启Flv流视频格式的支持,可以通过起始点进行访问请求。

–with-http_mp4_module – 开启Mp4流媒体支持,主要为H.264/AAC编码格式的支持,扩展名可以为.mp4, .m4v, .m4a。可以通过其实位置请求。

–with-http_gunzip_module – 开启gzip方式传输模式,gzip方式传输数据能够大大减少带宽消耗,提升网页的响应速度,这也需要客户端的支持。

–with-http_gzip_static_module – 开启预压缩功能,此功能主要是针对静态文件请求后,自动预压缩为.gz文件,返回给客户。

–with-http_random_index_module – 开启此功能,Nginx将响应用户以 “/” 结尾的请求,并在该目录下随机选择一个文件作为index文件。此模块先于 ngx_http_index_module 被执行。

–with-http_secure_link_module – 开启安全链接功能。通过认证文件的配置,限制用户访问权限。

–with-http_degradation_module – 内存不足时,Nginx将返回204或444给客户端。

–with-http_stub_status_module – 开启服务器状态查看功能。如Active connections: 291 ;server accepts handled requests; 16630948 16630948 31070465; Reading: 6 Writing: 179 Waiting: 106。

–without基本上都是必须要用到的模块,默认安装,当然也可以使用–without-****来不安装它们,以下是一些–without的模块,不建议–withou取消他们的安装。

--without-http_charset_module
--without-http_gzip_module
--without-http_ssi_module
--without-http_userid_module
--without-http_access_module
--without-http_auth_basic_module
--without-http_autoindex_module
--without-http_geo_module
--without-http_map_module
--without-http_split_clients_module
--without-http_referer_module
--without-http_rewrite_module
--without-http_proxy_module
--without-http_fastcgi_module
--without-http_uwsgi_module
--without-http_scgi_module
--without-http_memcached_module
--without-http_limit_conn_module
--without-http_limit_req_module
--without-http_empty_gif_module
--without-http_browser_module
--without-http_upstream_ip_hash_module
--without-http_upstream_least_conn_module
--without-http_upstream_keepalive_module

–with-http_perl_module、–with-perl_modules_path=PATH、–with-perl=PATH,这三个选项主要是启用Nginx的perl支持,也可以把Perl加入到SSI中。官方警告,此模块处于测试阶段,慎重使用。

设置相关Fastcgi、uwsgi等临时缓存路径,可以使用以下配置,个人感觉默认即可:

--http-client-body-temp-path=PATH
--http-proxy-temp-path=PATH
--http-fastcgi-temp-path=PATH
--http-uwsgi-temp-path=PATH
--http-scgi-temp-path=PATH

如果不想使用Nginx作为HTTP服务器可以使用–without-http-cache、–without-http 来禁止HTTP服务器的使用,很少有这样做的。

如果想开启Nginx对Mail的支持,可以使用Nginx的mail模块,官方文档中有更多针对Mail的模块,使Nginx对邮件服务有更多的选择。

--with-mail
--with-mail_ssl_module
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module

–with-google_perftools_module – 为Worker开启 Google Performance Tools, gperftools是一个类似Facebook的Xhprof工具。类似的模块也可以开启 “–with-cpp_test_module ”。

–add-module=PATH – 这个模块比较重要,一般你在编译好后,突然发现有个模块没有编译上,这时就可以使用这个选项来添加一个模块,在日常运维中用的还是比较多的。

如果你的C等编译环境不是默认的,需要手动指定,你可以使用如下选项指定:

--with-cc=PATH
--with-cpp=PATH
--with-cc-opt=OPTIONS
--with-ld-opt=OPTIONS
--with-cpu-opt=CPU

如果想使用伪静态,PCRE是必须安装的,默认是安装的,这里编译的时候需要指定路径:

--without-pcre
--with-pcre
--with-pcre=DIR
--with-pcre-opt=OPTIONS
--with-pcre-jit

Nginx还可以支持加密处理,如md5、sha1加密算法:

--with-md5=DIR
--with-md5-opt=OPTIONS
--with-md5-asm

--with-sha1=DIR
--with-sha1-opt=OPTIONS
--with-sha1-asm

Zlib支持的配置选项

--with-zlib=DIR
--with-zlib-opt=OPTIONS
--with-zlib-asm=CPU

Atom的支持:

--with-libatomic
--with-libatomic=DIR

指定openssl路径及配置:

--with-openssl=DIR
--with-openssl-opt=OPTIONS

–with-debug – 最后一个不解释。

 


 

参考文档:

Nginx官方RPM包:http://nginx.org/packages/rhel/6/x86_64/RPMS/
Nginx官方编译选项:http://wiki.nginx.org/ChsInstallOptions
Nginx官方文档:http://nginx.org/en/docs/

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容