源码编译安装 nginx 时,必须在 configure 阶段通过参数确定安装路径和启用模块:--prefix 指定根目录,其他 ---path 参数精细控制文件位置,--with- 启用模块如 ssl、http/2、realip 等。

源码编译安装 Nginx 时,指定安装目录和启用模块全靠 ./configure 命令的参数控制。关键不是“装完再改”,而是在编译前就通过参数把路径和功能定死。
指定安装目录的核心参数
最常用、最必须明确的是 --prefix,它定义整个 Nginx 的根安装位置:
-
--prefix=/usr/local/nginx:所有文件(二进制、配置、日志、模块)都放在这个目录下,比如配置文件默认为/usr/local/nginx/conf/nginx.conf - 若想更精细控制,可单独指定关键路径:
-
--sbin-path=/usr/sbin/nginx:主程序 nginx 可执行文件位置 -
--conf-path=/etc/nginx/nginx.conf:配置文件路径(推荐与系统习惯一致) -
--error-log-path=/var/log/nginx/error.log:错误日志路径 -
--http-log-path=/var/log/nginx/access.log:访问日志路径 -
--pid-path=/var/run/nginx.pid和--lock-path=/var/run/nginx.lock:运行时文件位置
-
启用常用模块的写法
模块默认大多关闭,需用 --with- 显式开启。生产环境高频组合如下:
-
--with-http_ssl_module:支持 HTTPS(必须有 OpenSSL 开发库) -
--with-http_v2_module:启用 HTTP/2 协议(需 OpenSSL 1.0.2+) -
--with-http_realip_module:从代理头(如 X-Forwarded-For)还原真实客户端 IP -
--with-http_stub_status_module:提供基础状态页(/nginx_status),用于监控 -
--with-http_gzip_static_module:支持预压缩的 .gz 文件直接返回,节省 CPU -
--with-file-aio:启用内核级异步 I/O,提升大文件读取性能(Linux 下有效) -
--with-threads:启用线程池,缓解阻塞操作对 worker 的影响
一个典型生产配置示例
把上面两类参数组合起来,就是实际可用的命令:
./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-file-aio \
--with-threads
执行完 ./configure 后,再运行 make && sudo make install 即可完成安装。路径和模块在 configure 阶段就已固化,后续无法更改。











