nginx配置编译选项

配置编译选项

源码包中提供 configure 脚本用于在编译前定义 nginx 各方面的配置。
执行 configure 脚本最后生成 Makefile,make 命令根据 Makefile 进行编译安装。

子目录:

  • 配置 nginx 文件安装路径
  • 配置 nginx gcc 选项
  • 指定 nginx 并发模型
  • nginx 的模块
  • 默认编译的模块
  • 默认不编译的模块
  • 第三方模块
  • 静态链接模块和动态链接模块

配置示例:

$ ./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
配置 nginx 文件安装路径

使用 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 库。

配置 nginx gcc 选项

指定编译相关选项:

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

指定 nginx 并发模型

可参考: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 由很多模块组成。一些模块被默认编译进 nginx,因此不需要在 configure 脚本的选项中显式地指定。但是如果你希望不编译某个默认模块,可使用 --without-MODULE 选项将其排除在外。

没有默认编译的模块以及第三方模块,必须在执行 configure 脚本时进行显式地指定。对于这些模块,其中一部分是静态链接到 nginx 库,另一些是可动态链接到 nginx 库。

  • 如果是静态链接到 nginx,当每次 nginx 启动时,这些模块被加载到 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

http_charset_module

在响应首部的 “Content-Type” 字段添加指定的字符集,能够对数据进行字符集转换。

http_gzip_module

使用 gzip 对响应报文进行压缩,可减少传输的数据大小,可减少一半或更多。

http_ssi_module

Processes SSI (Server Side Includes) commands in responses passing through it.

http_userid_module

为客户端标识设置合适的 cookies

http_access_module

Limits access to certain client addresses.
通过 “IP地址” 限制某个客户端的访问

http_auth_basic_module

使用 HTTP 基本认证协议,对客户端进行认证,以限制对资源的访问。

http_autoindex_module

Processes requests ending with the slash character (‘/’) and produces a directory listing.

http_geo_module

Creates variables with values depending on the client IP address.

http_map_module

Creates variables whose values depend on values of other variables.

http_split_clients_module

Creates variables suitable for A/B testing, also known as split testing.

http_referer_module

对客户端的访问,如果在请求首部的 Referer 字段有无效的值,则阻止其对某个站点的访问。

http_rewrite_module

使用正则表达式修改请求 URI,并返回重定向指令;根据条件判断选择配置。依赖于 PCRE 库。

http_proxy_module

将请求转发给其他服务器

http_fastcgi_module

将请求转发给 FastCGI 服务器。

http_uwsgi_module

将请求转发给 uwsgi 服务器。

http_scgi_module

将请求转发给 SCGI 服务器。

http_memcached_module

从一个 memcached 服务器获取响应。

http_limit_conn_module

对每个定义的 key,限制其连接数,特别是限制来自同一个 IP 地址的连接数。

http_limit_req_module

对每个定义的 key,限制其请求的处理速率,特别是限制来自同一个 IP 地址的请求处理速率。

http_empty_gif_module

Emits single-pixel transparent GIF.

http_browser_module

Creates variables whose values depend on the value of the “User-Agent” request header field.

http_upstream_hash_module

激活基于 hash 的负载均衡策略

http_upstream_ip_hash_module

激活基于 IP hash 的负载均衡策略

http_upstream_least_conn_module

激活基于 “最小连接数” 的负载均衡策略

http_upstream_keepalive_module

激活 keepalive 连接保持

http_upstream_zone_module

激活共享内存区

默认不编译的模块

一些模块是默认不编译的,你需要使用 ./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指令指定该模块。



作者:C86guli
链接:https://www.jianshu.com/p/48fd79a3cd23
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值