源码编译安装是生产环境 Nginx 常用部署方式,可以自定义安装路径、按需开启模块;同时 Nginx 支持不停业务平滑升级、版本回滚,这也是 Nginx 高可用的重要特性。

编译器介绍

源码安装需要提前准备标准编译器。 GCC 全称 GNU Compiler collection,由 GNU 组织开发,遵循 GPL 与 LGPL 许可证,是类 UNIX、Mac OS 操作系统的标准编译器。 GCC 最初仅支持 C 语言,原名 GNU C 语言编译器;后续不断迭代,现已支持 C++、Fortran、Pascal、ObjectiveC、Java、Ada 等多种编程语言。

源码编译还需要 Automake 工具,用于自动生成 Makefile。 Nginx 部分模块依赖第三方库:

  • pcre:支持 rewrite 地址重写功能
  • zlib:gzip 压缩模块依赖库
  • openssl:SSL/HTTPS 加密模块依赖库

一、编译安装实操

官方源码包下载地址

nginx: download

1. 下载源码包

[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.2.tar.gz

2. 创建 Nginx 运行用户

创建系统用户 nginx,不登录、不创建家目录,用于运行 nginx 进程,安全隔离:

[root@Nginx ~]# useradd -s /sbin/nologin -M nginx

3. 查看编译参数说明

[root@Nginx nginx-1.28.2]# ./configure --help
  • --prefix=PATH:指定安装路径
  • --with-xxx_module:默认不开启的模块,需要使用就添加该参数启用
  • --without-xxx_module:默认开启的模块,不需要则添加该参数关闭

4. 解压源码包,执行 configure 预检测

[root@Nginx ~]# tar zxf nginx-1.28.2.tar.gz
[root@Nginx ~]# cd nginx-1.28.2/

执行 configure,自定义模块参数:

[root@Nginx nginx-1.28.2]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \                        # 指定运行用户
> --group=nginx \                       # 指定运行用户组
> --with-http_ssl_module \              # 开启HTTPS支持
> --with-http_v2_module \               # 开启HTTP2协议
> --with-http_realip_module \           # 后端获取真实客户端IP
> --with-http_stub_status_module \      # Nginx状态监控页面
> --with-http_gzip_static_module \      # 静态文件预压缩
> --with-pcre \                         # 正则库,rewrite依赖
> --with-stream \                       # 四层TCP反向代理
> --with-stream_ssl_module \            # Stream四层SSL加密
> --with-stream_realip_module           # 四层代理透传真实IP

单行写法:

./configure --prefix=/usr/local/nginx --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-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

5. 编译过程常见报错处理

报错 1:缺少 C 编译器
checking for C compiler ... not found
./configure: error: C compiler cc is not found

解决方案:安装 gcc 编译器

[root@Nginx nginx-1.28.2]# dnf install gcc -y

安装完成后,重新执行上面的 configure 命令。

报错 2:缺少 PCRE 库(rewrite 模块依赖)
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

解决方案:安装 pcre 开发库

[root@Nginx nginx-1.28.2]# dnf install pcre-devel.x86_64 -y

安装完成后,重新执行 configure。

报错 3:缺少 OpenSSL 库(ssl 模块依赖)
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

解决方案:安装 openssl 开发库

[root@Nginx nginx-1.28.2]# dnf install openssl-devel.x86_64 -y

安装完成后,重新执行 configure。

报错 4:缺少 zlib 库(gzip 压缩模块依赖)
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

解决方案:安装 zlib 开发库

[root@Nginx nginx-1.28.2]# dnf install zlib-devel.x86_64 -y

所有依赖安装完毕,configure 检测成功,开始编译 + 安装:

[root@Nginx nginx-1.28.2]# make && make install

6. Nginx 安装后的核心目录

安装完成后,/usr/local/nginx下 4 个主要目录:

[root@Nginx ~]# ls /usr/local/nginx/
conf  html  logs  sbin
  • conf:所有配置文件目录,nginx.conf为主配置文件;.default为样板配置,可复制去掉后缀直接使用,如 fastcgi 相关配置。
  • html:默认网站根目录,存放静态页面;内置 50x 系列错误页面。可修改配置指向其他目录。
  • logs:存放访问日志access.log、错误日志error.log、pid 文件;可迁移到/var/log/nginx等路径。
  • sbin:nginx 二进制主程序,支持各类管理参数。

7. 配置环境变量

将 nginx 命令加入环境变量,任意目录可直接执行 nginx 命令:

[root@Nginx ~]# vim .bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@Nginx ~]# source .bash_profile

验证版本与编译参数:

[root@Nginx ~]# nginx -V
nginx version: nginx/1.28.2
built by gcc 11.4.1 20230605 (Red Hat 11.4.1-2) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --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-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

8. 创建 systemd 管理启动文件

编写nginx.service,方便使用 systemctl 管理服务:

[root@Nginx ~]# vim /lib/systemd/system/nginx.service

写入内容:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重载 systemd 识别新服务单元:

[root@Nginx ~]# systemctl daemon-reload

9. 启动 Nginx

方式 1:nginx 原生命令启动

[root@Nginx ~]# nginx
[root@Nginx ~]# ps aux | grep nginx
root      42714  0.0  0.0   9916   936 ?        Ss   14:50   0:00 nginx: master process nginx
nginx     42715  0.0  0.1  13812  4756 ?        S    14:50   0:00 nginx: worker process
root      42717  0.0  0.0   6636  2204 pts/0    S+   14:50   0:00 grep --color=auto nginx

方式 2:systemctl 启动并设置开机自启

[root@Nginx ~]# systemctl enable --now nginx

二、Nginx 平滑升级与版本回滚

原理介绍

业务在线运行时,不能停机,当需要升级版本、新增模块,就使用平滑升级。 平滑升级流程:

  1. 备份旧的 nginx 二进制程序
  2. 替换新的 nginx 程序文件
  3. 向旧 master 发送USR2信号
  4. master 将 pid 文件重命名nginx.pid.oldbin,启动新 master 进程,新旧两个 master 共存
  5. 新 master 创建新 worker 进程,处理新连接;旧 worker 继续处理已有长连接
  6. 向旧 master 发送WINCH信号,旧 master 优雅关闭旧 worker 进程,不再接收新请求
  7. 确认业务正常,向旧 master 发送QUIT,彻底关闭旧 master,删除.oldbinpid 文件

回滚:升级后发现异常,向旧 master 发送HUP信号,重新拉起旧版本 worker;向新 master 发送 QUIT 关闭新版进程,完成回滚。

平滑升级实操

1.下载更高版本源码包

[root@Nginx ~]# wget https://nginx.org/download/nginx-1.29.6.tar.gz

2.解压源码包,执行 configure(编译参数必须和旧版本完全一致)

[root@Nginx ~]# tar zxf nginx-1.29.6.tar.gz
[root@Nginx ~]# cd nginx-1.29.6/
[root@Nginx nginx-1.29.6]# ./configure --prefix=/usr/local/nginx --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-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

重点:只执行 make,不要 make install,否则会覆盖配置文件!

[root@Nginx nginx-1.29.6]# make

3.对比新旧二进制文件

[root@Nginx nginx-1.29.6]# ll objs/nginx /usr/local/nginx/sbin/nginx
-rwxr-xr-x 1 root root 6097248  3月 22 15:53 objs/nginx
-rwxr-xr-x 1 root root 5893792  3月 22 14:03 /usr/local/nginx/sbin/nginx

4.备份旧 nginx 程序,替换新程序

[root@Nginx ~]# cd /usr/local/nginx/sbin/
[root@Nginx sbin]# cp nginx nginx.28.bak
[root@Nginx sbin]# cp -p ~/nginx-1.29.6/objs/nginx ./nginx -f

5.检查配置语法

[root@Nginx sbin]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

6.发送 USR2 信号,启动新 master

# 查看当前旧master进程号
[root@Nginx sbin]# ps aux | grep nginx
root       5199  0.0  0.0   9916   932 ?        Ss   16:47   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      5200  0.0  0.1  13812  5268 ?        S    16:47   0:00 nginx: worker process
root       5292  0.0  0.0   6636  2216 pts/0    S+   16:50   0:00 grep --color=auto nginx

# 发送USR2,触发平滑升级
[root@Nginx sbin]# kill -USR2 5199

执行后,会同时存在新旧两个 master 进程,pid 目录会出现nginx.pid.oldbin

[root@Nginx sbin]# ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid  nginx.pid.oldbin

查看进程:

[root@Nginx sbin]# ps aux | grep nginx
root       5199  0.0  0.0   9916  2528 ?        Ss   16:47   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      5200  0.0  0.1  13812  5268 ?        S    16:47   0:00 nginx: worker process
root       5293  0.0  0.1   9964  6152 ?        S    16:50   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      5294  0.0  0.1  13872  4996 ?        S    16:50   0:00 nginx: worker process
root       5296  0.0  0.0   6636  2212 pts/0    S+   16:51   0:00 grep --color=auto nginx

7.向旧 master 发送 WINCH,优雅关闭旧 worker 进程

[root@Nginx sbin]# kill -WINCH 5199

此时旧 master 保留,但旧 worker 全部退出,新 worker 接管全部流量。

8. 验证版本生效

[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.29.6
Date: Sun, 22 Mar 2026 08:32:16 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sun, 22 Mar 2026 06:03:42 GMT
Connection: keep-alive
ETag: "69bf863e-267"
Accept-Ranges: bytes

业务无中断,升级完成。确认业务稳定后,可发送kill -QUIT 旧masterPID彻底关闭旧主进程。

平滑回滚实操(升级异常时)

1.先备份新版程序,恢复旧版本二进制文件

[root@Nginx sbin]# cp nginx nginx.29.bak
[root@Nginx sbin]# mv nginx.28.bak nginx

2.向旧 master 进程发送 HUP 信号,拉起旧版本 worker 进程

[root@Nginx sbin]# kill -HUP 5199

3.向新 master 发送 WINCH,关闭新版本 worker

[root@Nginx sbin]# kill -WINCH 5293

4.验证版本回滚成功

[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.28.2
Date: Sun, 22 Mar 2026 09:01:15 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sun, 22 Mar 2026 08:47:28 GMT
Connection: keep-alive
ETag: "69bfaca0-267"
Accept-Ranges: bytes

回滚成功,流量切回旧版本。确认业务正常后,执行kill -QUIT 5293关闭新版 master。

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐