编译自:
installing-nginx-open-sourcehtml
nginx 提供两种版本的源码包:linux
mainline 版。该版本包含最新的特性和bug修改,而且老是保持更新。该版本是可靠的,但它可能会包含实验性的模块,以及必定数量的新 bug。nginx
stable 版。该版本不包含新特性,但包含关键 bug 修复。推荐使用该版用于生产环境。git
mainline 版和 stable 版都提供两种安装方式:github
从预编译的 package 进行安装。这是快速和容易的安装方式。预编译的 package 包含几乎全部的 nginx 官方模块,且适用于大多数流行的操做系统。正则表达式
从源码编译安装。这种方式更为灵活:你能够添加特定的模块,包含添加第三方的模块,或者应用最新的安全补丁。centos
从源码编译安装的方式更为灵活:你能够添加特定的模块,包含添加第三方的模块,或者应用最新的安全补丁。安全
在编译安装 nginx 以前,须要首先安装 nginx 的依赖:服务器
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz $ tar -zxf pcre-8.38.tar.gz $ cd pcre-8.38 $ ./configure $ make $ sudo make install
译注:也可用 yum install pcre-devel
替代cookie
$ wget http://zlib.net/zlib-1.2.8.tar.gz $ tar -zxf zlib-1.2.8.tar.gz $ cd zlib-1.2.8 $ ./configure $ make $ sudo make install
译注:也可用 yum install zlib-devel
替代
$ wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz $ tar -zxf openssl-1.0.2f.tar.gz $ cd openssl-1.0.2f $ ./configure darwin64-x86_64-cc --prefix=/usr $ make $ sudo make install
译注:也可用 yum install openssl-devel
替代
源码 tarball 下载地址是:http://nginx.org/en/download.html
mainline 版:
$ wget http://nginx.org/download/nginx-1.11.1.tar.gz $ tar zxf nginx-1.11.1.tar.gz $ cd nginx-1.11.1
stable 版:
$ wget http://nginx.org/download/nginx-1.10.1.tar.gz $ tar zxf nginx-1.10.1.tar.gz $ cd nginx-1.10.1
源码包中提供 configure 脚本用于在编译前定义 nginx 各方面的配置。 执行 configure 脚本最后生成 Makefile,make 命令根据 Makefile 进行编译安装。
子目录:
配置示例:
$ ./configure \ --sbin-path=/usr/local/nginx/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --with-pcre=../pcre-8.38 \ --with-zlib=../zlib-1.2.8 \ --with-http_ssl_module \ --with-stream \ --with-mail=dynamic \ --add-module=/usr/build/nginx-rtmp-module \ --add-dynamic-module=/usr/build/3party_module
使用 configure 脚本可设置 nginx 的文件安装路径,包括 nginx 二进制文件和配置文件,以及设置依赖库如 PCRE 和 SSL 的源码所在路径(用于对其进行静态编译)。
--prefix=path
定义 nginx 文件的安装路径。configure 的其余选项若是使用相对路径,那么以此路径为基础路径。(except for paths to libraries sources)。nginx.conf 文件中的相对路径也以此为基础路径。默认
--prefix=/usr/local/nginx
--sbin-path=path
设置 nginx 二进制程序的路径名,这个名字只在安装期间使用。默认
--sbin-path=prefix/sbin/nginx
--conf-path=path
设置 nginx.conf 的路径。nginx 可在启动时手动以
-c file
参数指定其余配置文件。默认--conf-path=prefix/conf/nginx.conf
--pid-path=path
设置 nginx.pid 文件的路径。安装nginx以后,可在
nginx.conf
文件中使用 pid 指令修改该路径。默认--pid-path=prefix/logs/nginx.pid
--error-log-path=path
设置 nginx 错误日志的路径。安装nginx以后,可在
nginx.conf
文件中使用 error_log 指令修改该路径。默认--error-log-path=prefix/logs/error.log
--http-log-path=path
设置 nginx 访问日志的路径。安装nginx以后,可在
nginx.conf
文件中使用 access_log 指令修改该路径。默认--http-log-path=prefix/logs/access.log
--user=name
设置启动 worker 进程时所使用的非特权用户名。安装nginx以后,可在
nginx.conf
文件中使用 user 指令修改用户名。默认--user=nobody
--group=name
设置启动 worker 进程时所使用的非特权用户组名。安装nginx以后,可在
nginx.conf
文件中使用 user 指令修改用户组名。默认--group=nobody
--with-pcre=path
设置 PCRE 库的源码路径。首先须要下载和解压 PCRE 库。要求 PCRE 的版本范围为 4.4 — 8.38。设置以后,其他的就交给 ./configure 和 make 命令。nginx 使用 PCRE 库用于支持正则表达式。正则表达式在 location 指令和 rewrite 模块中会用到。
--with-pcre-jit
编译 PCRE 库时,加入 “just-in-time compilation” 支持 (1.1.12, the pcre_jit directive)
--with-zlib=path
设置 zlib 库的源码路径。首先须要下载和解压 zlib 库。 要求 zlib 库的版本范围为 1.1.3 — 1.2.8,设置以后,其他的就交给 ./configure 和 make 命令。gzip 压缩模块依赖 zlib 库。
指定编译相关选项:
--with-cc-opt=parameters
为 CFLAGS 变量设置额外的参数。好比 FreeBSD 下使用 PCRE 库,必须指定
--with-cc-opt="-I /usr/local/include"
。 好比 但愿增长 select() 支持的文件数,可指定:--with-cc-opt="-D FD_SETSIZE=2048"
--with-ld-opt=parameters
设置连接时的额外参数。好比,FreeBSD 使用 PCRE 库时,必须指定
--with-ld-opt="-L /usr/local/lib"
。
可参考:Connection Processing Methods
--with-select_module
--without-select_module
是否编译 select 模块。使用 select 模块可以使 nginx 工做于
select()
模式。 若是 nginx 不支持其余更合适的模块,如kqueue
,epoll
或者/dev/poll
,该模块被自动编译。
--with-poll_module
--without-poll_module
是否编译 poll 模块。使用 poll 模块可以使 nginx 工做于
poll()
模式。 若是 nginx 不支持其余更合适的模块,如kqueue
,epoll
或者/dev/poll
,该模块被自动编译。
nginx 由不少模块组成。一些模块被默认编译进 nginx,所以不须要在 configure
脚本的选项中显式地指定。可是若是你但愿不编译某个默认模块,可以使用 --without-MODULE
选项将其排除在外。
没有默认编译的模块以及第三方模块,必须在执行 configure
脚本时进行显式地指定。对于这些模块,其中一部分是静态连接到 nginx 库,另外一些是可动态连接到 nginx 库。
nginx.conf
中指定了的该模块,模块才会被加载到 nginx 中。若是你但愿不编译某个默认模块,可以使用 --without-MODULE
选项将其排除在外:
$ ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-stream --with-pcre=../pcre-8.38 --with-zlib=../zlib-1.2.8 **--without-http_empty_gif_module**
在响应首部的 “Content-Type” 字段添加指定的字符集,可以对数据进行字符集转换。
使用 gzip 对响应报文进行压缩,可减小传输的数据大小,可减小一半或更多。
Processes SSI (Server Side Includes) commands in responses passing through it.
为客户端标识设置合适的 cookies
Limits access to certain client addresses. 经过 “IP地址” 限制某个客户端的访问
使用 HTTP 基本认证协议,对客户端进行认证,以限制对资源的访问。
Processes requests ending with the slash character (‘/’) and produces a directory listing.
Creates variables with values depending on the client IP address.
Creates variables whose values depend on values of other variables.
Creates variables suitable for A/B testing, also known as split testing.
对客户端的访问,若是在请求首部的 Referer 字段有无效的值,则阻止其对某个站点的访问。
使用正则表达式修改请求 URI,并返回重定向指令;根据条件判断选择配置。依赖于 PCRE 库。
将请求转发给其余服务器
将请求转发给 FastCGI 服务器。
将请求转发给 uwsgi 服务器。
将请求转发给 SCGI 服务器。
从一个 memcached 服务器获取响应。
对每一个定义的 key,限制其链接数,特别是限制来自同一个 IP 地址的链接数。
对每一个定义的 key,限制其请求的处理速率,特别是限制来自同一个 IP 地址的请求处理速率。
Emits single-pixel transparent GIF.
Creates variables whose values depend on the value of the “User-Agent” request header field.
激活基于 hash 的负载均衡策略
激活基于 IP hash 的负载均衡策略
http_upstream_least_conn_module
激活基于 “最小链接数” 的负载均衡策略
http_upstream_keepalive_module
激活 keepalive 链接保持
激活共享内存区
一些模块是默认不编译的,你须要使用 ./configure
命令添加该选项。在这些默认不编译的模块中,有些模块可编译为动态模块,以下:
mail stream geoip image_filter perl xslt
示例:
$ ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-pcre=../pcre-8.38 --with-zlib=../zlib-1.2.8 **--with-http_ssl_module** **--with-stream** **--with-mail**
--with-threads
Enables NGINX to use thread pools. See Thread Pools in NGINX Boost Performance 9x! blog post for details.
--with-file-aio
Enables asynchronous I/O.
--with-ipv6
Enables IPv6 support.
--with-http_ssl_module
Provides support for HTTPS. Requires an SSL library such as OpenSSL.See the ngx_http_ssl_module reference for the list of directives.
--with-http_v2_module
Provides support for HTTP/2.See the ngx_http_v2_module reference for the list of directives and the “HTTP/2 Module in NGINX” blog post for details.
--with-http_realip_module
Changes the client address to the one sent in the specified header field. See the ngx_http_realip_module reference for the list of directives.
--with-http_addition_module
Adds text before and after a response. See the ngx_http_addition_module reference for the list of directives.
--with-http_xslt_module or --with-http_xslt_module=dynamic
Transforms XML responses using one or more XSLT stylesheets. The module requires the Libxml2 and XSLT libraries. See the ngx_http_xslt_module reference for the list of directives. The module can also be compiled as dynamic.
--with-http_image_filter_module or --with-http_image_filter_module=dynamic
Transforms images in JPEG, GIF, and PNG formats. The module requires the LibGD library. See ngx_http_image_filter_module reference for the list of directives. The module can also be compiled as dynamic.
--with-http_geoip_module or --with-http_geoip_module=dynamic
Allows creating variables whose values depend on the client IP address. The module uses MaxMind GeoIP databases. See the ngx_http_geoip_module reference for the list of directives. The module can also be compiled as dynamic.
--with-http_sub_module
Modifies a response by replacing one specified string by another. See the ngx_http_sub_module reference for the list of directives.
--with-http_dav_module
Intended for file management automation via the WebDAV protocol. See the ngx_http_dav_module reference for the list of directives.
--with-http_flv_module
Provides pseudo-streaming server-side support for Flash Video (FLV) files. See the ngx http_flv_module reference for the list of directives.
--with-mp4_module
Provides pseudo-streaming server-side support for MP4 files. See the ngx_http_mp4_module reference for the list of directives.
--with-http_gunzip_module
Decompresses responses with Content-Encoding: gzip for clients that do not support zip encoding method. See the ngx_http_gunzip_module for the list of directives.
--with-http_gzip_static_module
Allows sending precompressed files with the *.gz filename extension instead of regular files. See the ngx_http_gzip_static_module for the list of directives.
--with-http_auth_request_module
Implements client authorization based on the result of a subrequest. See the http_auth_request_module for the list of directives.
--with-http_random_index_module
Processes requests ending with the slash character (‘/’) and picks a random file in a directory to serve as an index file. See the ngx_http_random_index_module for the list of directives.
--with-http_secure_link_module
Used to check authenticity of requested links, protect resources from unauthorized access, and limit link lifetime. See the ngx_http_secure_link_module for the list of directives.
--with-http_slice_module
Allows splitting a request into subrequests, each subrequest returns a certain range of response. Provides more effective caching of large files. See the ngx_http_slice_module reference for the list of directives.
--with-http_degradation_module
Allows returning an error when a memory size exceeds the defined value.
--with-http_stub_status_module
Provides access to basic status information. See the ngx_http_stub_status_module reference for the list of directives. Note that NGINX Plus customers do not require this module as they are already provided with extended status metrics and interactive dashboard.
--with-http_perl_module or --with-http_perl_module=dynamic
Used to implement location and variable handlers in Perl and insert Perl calls into SSI. Requires the PERL library. See the ngx_http_perl_module reference for the list of directives. The module can also be compiled as dynamic.
--with-mail or --with-mail=dynamic
Enables mail proxy functionality. See the ngx_mail_core_module reference for the list of directives. The module can also be compiled as dynamic.
--with-mail_ssl_module
Provides support for a mail proxy server to work with the SSL/TLS protocol. Requires an SSL library such as OpenSSL. See the ngx_mail_ssl_module reference for the list of directives.
--with-stream or --with-stream=dynamic
Enables the TCP proxy functionality. See the ngx_stream_core_module reference for the list of directives. The module can also be compiled as dynamic.
--with-stream_ssl_module
Provides support for a stream proxy server to work with the SSL/TLS protocol. Requires an SSL library such as OpenSSL. See the ngx_stream_ssl_module reference for the list of directives.
--with-google_perftools_module
Allows using Google Performance tools library.
--with-cpp_test_module --with-debug
Enables the debugging log.
你能够为 nginx 编译第三方模块,一些第三方模块可参见:modules。 使用第三方模块,须要本身承担稳定性的风险,由于第三方模块的稳定性是没有保证的。
大多数被编译进 nginx 的模块是被静态连接的,它们在编译 nginx 时被构建到 nginx 中,而且被 nginx 的可执行文件静态地连接。被静态连接的模块没法被 disabled,只有从新编译 nginx 才能达到这个目的。
以静态方式编译第三方模块,以下所示,在执行 configure 脚本时,添加 --add-module=
选项,并输入模块的路径:
$ ./configure ... --add-module=/usr/build/nginx-rtmp-module
某些 nginx 模块也能够被编译为共享对象(*.so 文件),并在运行时被加载到 nginx 中。这种方式提供了更多的灵活性,由于能够自由选择加载或不加载某个动态模块。要加载某个动态模块,只要在 nginx.conf
中使用 load_module指令指定该模块。
支持动态加载的模块,它们不是默认编译的:
mail stream geoip image_filter perl xslt
以动态方式编译第三方模块,以下所示,在执行 configure 脚本时,添加 --add-dynamic-module=
选项,并输入模块的路径:
$ ./configure ... --add-dynamic-module=/path/to/module
生成的 *.so 文件可在 prefix/modules/
目录中找到,例如默认的路径为 /usr/local/nginx/modules
。
安装完成后,若是要加载某个动态模块,只要在 nginx.conf
中使用 load_module指令指定该模块。
相关扩展阅读: Introducing Dynamic Modules in NGINX 1.9.11 Extending NGINX
使用 configure 脚本配置完成后,可进行编译安装:
$ make $ sudo make install
安装顺利完成后,执行 nginx
命令,启动 nginx:
$ sudo nginx
安装预编译的 package 相对更简单和快速。缺点是缺乏灵活性。可安装预编译的 package 的系统有: Red Hat, CentOS, Debian, Ubuntu 以及 SLES。
关于预编译的 package 所包含的模块,请参见 linux_packages 。
nginx 为 Red Hat/CentOS 5, 5.x, 6, 6.x, 7 and 7.x 提供了预编译 package,package 有两个来源:
默认的 Red Hat or CentOS yum 仓库。这是最快的方式,但所提供的 package 版本比较老旧。好比说,CentOS 7.0 默认提供 nginx/1.6.2 released in September, 2014
nginx repo。为了从 nginx repo 安装 package,你须要设置相应的 yum 仓库。这里提供最新版本的 package
安装 EPEL 仓库:
$ sudo yum install epel-release
更新仓库,并安装 nginx:
$ sudo yum update
验证所安装的 nginx 版本:
$ sudo nginx -v nginx version: nginx/1.6.3
设置 yum 仓库:
$ cd /etc/yum.repos.d $ sudo vi /etc/yum.repos.d/nginx.repo
添加以下内容:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/ gpgcheck=0 enabled=1
其中:
好比,为 CentOS 7.0 获取最新的 mainline package:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=0 enabled=1
保存退出
更新仓库,并安装 nginx:
$ sudo yum update
运行 nginx:
$ sudo nginx
验证 nginx 是否启动:
$ curl -I 127.0.0.1 HTTP/1.1 200 OK Server: nginx/1.11.1
版权信息:
本文编译自 installing-nginx-open-source