从源码构建 nginx

构建是使用 configure 命令配置的。它定义了系统的各个方面,包括 nginx 允许用于连接处理的方法。最后,它创建一个 Makefile

configure 命令支持以下参数

--help
打印帮助信息。

--prefix=path
定义存放服务器文件的目录。该目录也将用于 configure 设置的所有相对路径(库源码路径除外)以及 nginx.conf 配置文件中。默认设置为 /usr/local/nginx 目录。
--sbin-path=path
设置 nginx 可执行文件的名称。此名称仅在安装期间使用。默认情况下,文件名为 prefix/sbin/nginx
--modules-path=path
定义安装 nginx 动态模块的目录。默认使用 prefix/modules 目录。
--conf-path=path
设置 nginx.conf 配置文件的名称。如果需要,nginx 始终可以使用不同的配置文件启动,通过在命令行参数中指定-c file。默认文件名为 prefix/conf/nginx.conf
--error-log-path=path
设置主要的错误、警告和诊断日志文件名。安装后,文件名始终可以在 nginx.conf 配置文件中使用 error_log 指令更改。默认文件名为 prefix/logs/error.log
--pid-path=path
设置存放主进程 ID 的 nginx.pid 文件名。安装后,文件名始终可以在 nginx.conf 配置文件中使用 pid 指令更改。默认文件名为 prefix/logs/nginx.pid
--lock-path=path
设置锁文件名前缀。安装后,该值始终可以在 nginx.conf 配置文件中使用 lock_file 指令更改。默认值为 prefix/logs/nginx.lock

--user=name
设置工作进程将使用的非特权用户名。安装后,该名称始终可以在 nginx.conf 配置文件中使用 user 指令更改。默认用户名为 nobody。
--group=name
设置工作进程将使用的组名。安装后,该名称始终可以在 nginx.conf 配置文件中使用 user 指令更改。默认情况下,组名设置为非特权用户的名称。

--build=name
设置可选的 nginx 构建名称。
--builddir=path
设置构建目录。

--with-select_module
--without-select_module
启用或禁用构建允许服务器使用 select() 方法的模块。如果平台似乎不支持 kqueue、epoll 或 /dev/poll 等更合适的方法,则会自动构建此模块。
--with-poll_module
--without-poll_module
启用或禁用构建允许服务器使用 poll() 方法的模块。如果平台似乎不支持 kqueue、epoll 或 /dev/poll 等更合适的方法,则会自动构建此模块。
--with-threads
启用使用 线程池
--with-file-aio
在 FreeBSD 和 Linux 上启用使用 异步文件 I/O (AIO)。

--with-http_ssl_module
启用构建一个为 HTTP 服务器添加 HTTPS 协议支持 的模块。默认不构建此模块。构建和运行此模块需要 OpenSSL 库。
--with-http_v2_module
启用构建一个提供 HTTP/2 支持的模块。默认不构建此模块。
--with-http_v3_module
启用构建一个提供 HTTP/3 支持的模块。默认不构建此模块。建议使用提供 HTTP/3 支持的 SSL 库来构建和运行此模块,例如 BoringSSLLibreSSLQuicTLS。否则,如果使用 OpenSSL 库,将使用不支持 QUIC early data 的 OpenSSL 兼容层。
--with-http_realip_module
启用构建 ngx_http_realip_module 模块,该模块将客户端地址更改为指定头部字段中发送的地址。默认不构建此模块。
--with-http_addition_module
启用构建 ngx_http_addition_module 模块,该模块在响应之前和之后添加文本。默认不构建此模块。
--with-http_xslt_module
--with-http_xslt_module=dynamic
启用构建 ngx_http_xslt_module 模块,该模块使用一个或多个 XSLT 样式表转换 XML 响应。默认不构建此模块。构建和运行此模块需要 libxml2libxslt 库。
--with-http_image_filter_module
--with-http_image_filter_module=dynamic
启用构建 ngx_http_image_filter_module 模块,该模块转换 JPEG, GIF, PNG 和 WebP 格式的图片。默认不构建此模块。
--with-http_geoip_module
--with-http_geoip_module=dynamic
启用构建 ngx_http_geoip_module 模块,该模块根据客户端 IP 地址和预编译的 MaxMind 数据库创建变量。默认不构建此模块。
--with-http_sub_module
启用构建 ngx_http_sub_module 模块,该模块通过将一个指定字符串替换为另一个来修改响应。默认不构建此模块。
--with-http_dav_module
启用构建 ngx_http_dav_module 模块,该模块通过 WebDAV 协议提供文件管理自动化。默认不构建此模块。
--with-http_flv_module
启用构建 ngx_http_flv_module 模块,该模块为 Flash Video (FLV) 文件提供服务器端伪流支持。默认不构建此模块。
--with-http_mp4_module
启用构建 ngx_http_mp4_module 模块,该模块为 MP4 文件提供服务器端伪流支持。默认不构建此模块。
--with-http_gunzip_module
启用构建 ngx_http_gunzip_module 模块,该模块为不支持“gzip”编码方法的客户端解压带有“Content-Encoding: gzip”的响应。默认不构建此模块。
--with-http_gzip_static_module
启用构建 ngx_http_gzip_static_module 模块,该模块允许发送带有“.gz”文件扩展名的预压缩文件,而不是常规文件。默认不构建此模块。
--with-http_auth_request_module
启用构建 ngx_http_auth_request_module 模块,该模块根据子请求的结果实现客户端授权。默认不构建此模块。
--with-http_random_index_module
启用构建 ngx_http_random_index_module 模块,该模块处理以斜杠字符(‘/’)结尾的请求,并在目录中随机选择一个文件作为索引文件提供服务。默认不构建此模块。
--with-http_secure_link_module
启用构建 ngx_http_secure_link_module 模块。默认不构建此模块。
--with-http_degradation_module
启用构建 ngx_http_degradation_module 模块。默认不构建此模块。
--with-http_slice_module
启用构建 ngx_http_slice_module 模块,该模块将请求分割成子请求,每个子请求返回响应的某个范围。该模块提供更有效的大响应缓存。默认不构建此模块。
--with-http_stub_status_module
启用构建 ngx_http_stub_status_module 模块,该模块提供对基本状态信息的访问。默认不构建此模块。

--without-http_charset_module
禁用构建 ngx_http_charset_module 模块,该模块将指定的字符集添加到“Content-Type”响应头字段,并可以额外将数据从一个字符集转换为另一个字符集。
--without-http_gzip_module
禁用构建一个 压缩 HTTP 服务器响应 的模块。构建和运行此模块需要 zlib 库。
--without-http_ssi_module
禁用构建 ngx_http_ssi_module 模块,该模块处理通过其的响应中的 SSI (Server Side Includes) 命令。
--without-http_userid_module
禁用构建 ngx_http_userid_module 模块,该模块设置适用于客户端标识的 cookie。
--without-http_access_module
禁用构建 ngx_http_access_module 模块,该模块允许限制对某些客户端地址的访问。
--without-http_auth_basic_module
禁用构建 ngx_http_auth_basic_module 模块,该模块允许通过使用“HTTP Basic Authentication”协议验证用户名和密码来限制对资源的访问。
--without-http_mirror_module
禁用构建 ngx_http_mirror_module 模块,该模块通过创建后台镜像子请求来实现原始请求的镜像。
--without-http_autoindex_module
禁用构建 ngx_http_autoindex_module 模块,该模块处理以斜杠字符(‘/’)结尾的请求,并在 ngx_http_index_module 模块找不到索引文件时生成目录列表。
--without-http_geo_module
禁用构建 ngx_http_geo_module 模块,该模块创建其值取决于客户端 IP 地址的变量。
--without-http_map_module
禁用构建 ngx_http_map_module 模块,该模块创建其值取决于其他变量值的变量。
--without-http_split_clients_module
禁用构建 ngx_http_split_clients_module 模块,该模块创建用于 A/B 测试的变量。
--without-http_referer_module
禁用构建 ngx_http_referer_module 模块,该模块可以阻止“Referer”头字段中值无效的请求访问站点。
--without-http_rewrite_module
禁用构建一个允许 HTTP 服务器重定向请求并更改请求 URI 的模块。构建和运行此模块需要 PCRE 库。
--without-http_proxy_module
禁用构建 HTTP 服务器代理模块
--without-http_fastcgi_module
禁用构建 ngx_http_fastcgi_module 模块,该模块将请求传递给 FastCGI 服务器。
--without-http_uwsgi_module
禁用构建 ngx_http_uwsgi_module 模块,该模块将请求传递给 uwsgi 服务器。
--without-http_scgi_module
禁用构建 ngx_http_scgi_module 模块,该模块将请求传递给 SCGI 服务器。
--without-http_grpc_module
禁用构建 ngx_http_grpc_module 模块,该模块将请求传递给 gRPC 服务器。
--without-http_memcached_module
禁用构建 ngx_http_memcached_module 模块,该模块从 memcached 服务器获取响应。
--without-http_limit_conn_module
禁用构建 ngx_http_limit_conn_module 模块,该模块限制每个键的连接数,例如来自单个 IP 地址的连接数。
--without-http_limit_req_module
禁用构建 ngx_http_limit_req_module 模块,该模块限制每个键的请求处理速率,例如来自单个 IP 地址的请求处理速率。
--without-http_empty_gif_module
禁用构建一个 发送单像素透明 GIF 的模块。
--without-http_browser_module
禁用构建 ngx_http_browser_module 模块,该模块创建其值取决于“User-Agent”请求头字段值的变量。
--without-http_upstream_hash_module
禁用构建一个实现 hash 负载均衡方法的模块。
--without-http_upstream_ip_hash_module
禁用构建一个实现 ip_hash 负载均衡方法的模块。
--without-http_upstream_least_conn_module
禁用构建一个实现 least_conn 负载均衡方法的模块。
--without-http_upstream_random_module
禁用构建一个实现 random 负载均衡方法的模块。
--without-http_upstream_keepalive_module
禁用构建一个提供到上游服务器连接缓存的模块。
--without-http_upstream_zone_module
禁用构建一个使其能够在共享内存区域中存储上游组运行时状态的模块。

--with-http_perl_module
--with-http_perl_module=dynamic
启用构建嵌入式 Perl 模块。默认不构建此模块。
--with-perl_modules_path=path
定义存放 Perl 模块的目录。
--with-perl=path
设置 Perl 二进制文件的名称。

--http-log-path=path
设置 HTTP 服务器的主要请求日志文件名。安装后,文件名始终可以在 nginx.conf 配置文件中使用 access_log 指令更改。默认文件名为 prefix/logs/access.log
--http-client-body-temp-path=path
定义一个目录,用于存储包含客户端请求体的临时文件。安装后,该目录始终可以在 nginx.conf 配置文件中使用 client_body_temp_path 指令更改。默认目录名为 prefix/client_body_temp
--http-proxy-temp-path=path
定义一个目录,用于存储从代理服务器接收到的数据的临时文件。安装后,该目录始终可以在 nginx.conf 配置文件中使用 proxy_temp_path 指令更改。默认目录名为 prefix/proxy_temp
--http-fastcgi-temp-path=path
定义一个目录,用于存储从 FastCGI 服务器接收到的数据的临时文件。安装后,该目录始终可以在 nginx.conf 配置文件中使用 fastcgi_temp_path 指令更改。默认目录名为 prefix/fastcgi_temp
--http-uwsgi-temp-path=path
定义一个目录,用于存储从 uwsgi 服务器接收到的数据的临时文件。安装后,该目录始终可以在 nginx.conf 配置文件中使用 uwsgi_temp_path 指令更改。默认目录名为 prefix/uwsgi_temp
--http-scgi-temp-path=path
定义一个目录,用于存储从 SCGI 服务器接收到的数据的临时文件。安装后,该目录始终可以在 nginx.conf 配置文件中使用 scgi_temp_path 指令更改。默认目录名为 prefix/scgi_temp

--without-http
禁用 HTTP 服务器。
--without-http-cache
禁用 HTTP 缓存。

--with-mail
--with-mail=dynamic
启用 POP3/IMAP4/SMTP 邮件代理 服务器。
--with-mail_ssl_module
启用构建一个为邮件代理服务器添加 SSL/TLS 协议支持 的模块。默认不构建此模块。构建和运行此模块需要 OpenSSL 库。
--without-mail_pop3_module
在邮件代理服务器中禁用 POP3 协议。
--without-mail_imap_module
在邮件代理服务器中禁用 IMAP 协议。
--without-mail_smtp_module
在邮件代理服务器中禁用 SMTP 协议。

--with-stream
--with-stream=dynamic
启用构建用于通用 TCP/UDP 代理和负载均衡的 stream 模块。默认不构建此模块。
--with-stream_ssl_module
启用构建一个为 stream 模块添加 SSL/TLS 协议支持 的模块。默认不构建此模块。构建和运行此模块需要 OpenSSL 库。
--with-stream_realip_module
启用构建 ngx_stream_realip_module 模块,该模块将客户端地址更改为 PROXY 协议头中发送的地址。默认不构建此模块。
--with-stream_geoip_module
--with-stream_geoip_module=dynamic
启用构建 ngx_stream_geoip_module 模块,该模块根据客户端 IP 地址和预编译的 MaxMind 数据库创建变量。默认不构建此模块。
--with-stream_ssl_preread_module
启用构建 ngx_stream_ssl_preread_module 模块,该模块允许在不终止 SSL/TLS 的情况下从 ClientHello 消息中提取信息。默认不构建此模块。
--without-stream_limit_conn_module
禁用构建 ngx_stream_limit_conn_module 模块,该模块限制每个键的连接数,例如来自单个 IP 地址的连接数。
--without-stream_access_module
禁用构建 ngx_stream_access_module 模块,该模块允许限制对某些客户端地址的访问。
--without-stream_geo_module
禁用构建 ngx_stream_geo_module 模块,该模块创建其值取决于客户端 IP 地址的变量。
--without-stream_map_module
禁用构建 ngx_stream_map_module 模块,该模块创建其值取决于其他变量值的变量。
--without-stream_split_clients_module
禁用构建 ngx_stream_split_clients_module 模块,该模块创建用于 A/B 测试的变量。
--without-stream_return_module
禁用构建 ngx_stream_return_module 模块,该模块向客户端发送指定的某个值,然后关闭连接。
--without-stream_set_module
禁用构建 ngx_stream_set_module 模块,该模块为变量设置值。
--without-stream_upstream_hash_module
禁用构建一个实现 hash 负载均衡方法的模块。
--without-stream_upstream_least_conn_module
禁用构建一个实现 least_conn 负载均衡方法的模块。
--without-stream_upstream_random_module
禁用构建一个实现 random 负载均衡方法的模块。
--without-stream_upstream_zone_module
禁用构建一个使其能够在共享内存区域中存储上游组运行时状态的模块。

--with-google_perftools_module
启用构建 ngx_google_perftools_module 模块,该模块允许使用 Google Performance Tools 对 nginx 工作进程进行性能分析。该模块供 nginx 开发者使用,默认不构建。
--with-cpp_test_module
启用构建 ngx_cpp_test_module 模块。

--add-module=path
启用外部模块。
--add-dynamic-module=path
启用外部动态模块。

--with-compat
启用动态模块兼容性。

--with-cc=path
设置 C 编译器的名称。
--with-cpp=path
设置 C 预处理器的名称。
--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"
--with-cpu-opt=cpu
启用针对指定 CPU 的构建:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, sparc32, sparc64, ppc64

--without-pcre
禁用使用 PCRE 库。
--with-pcre
强制使用 PCRE 库。
--with-pcre=path
设置 PCRE 库源码路径。需要从 PCRE 站点下载库分发包并解压。其余工作由 nginx 的 ./configuremake 完成。该库在 location 指令中的正则表达式支持和 ngx_http_rewrite_module 模块中是必需的。
--with-pcre-opt=parameters
设置 PCRE 的附加构建选项。
--with-pcre-jit
构建带有“just-in-time compilation”(即时编译)支持的 PCRE 库 (1.1.12, pcre_jit 指令)。
--without-pcre2
禁用使用 PCRE2 库替代原始 PCRE 库 (1.21.5)。

--with-zlib=path
设置 zlib 库源码路径。需要从 zlib 站点下载库分发包并解压。其余工作由 nginx 的 ./configuremake 完成。该库是 ngx_http_gzip_module 模块必需的。
--with-zlib-opt=parameters
设置 zlib 的附加构建选项。
--with-zlib-asm=cpu
启用使用针对指定 CPU 之一优化的 zlib 汇编源码:pentium, pentiumpro

--with-libatomic
强制使用 libatomic_ops 库。
--with-libatomic=path
设置 libatomic_ops 库源码路径。

--with-openssl=path
设置 OpenSSL 库源码路径。
--with-openssl-opt=parameters
设置 OpenSSL 的附加构建选项。

--with-debug
启用调试日志

参数用法示例(所有内容需要在同一行输入)

./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-pcre=../pcre2-10.39
    --with-zlib=../zlib-1.3

配置完成后,使用 make 编译并安装 nginx。