在 CentOS 7 上编译 Nginx 时,负载均衡功能(upstream)默认已包含在内,无需额外编译模块,但需注意系统已停止维护带来的源仓库风险。
先说结论:开源版 Nginx 自带负载均衡能力,编译时无需特殊参数,重点在于解决 CentOS 7 停服后的依赖安装问题。
- 适合:需要在 CentOS 7 旧环境中自定义编译参数或升级 Nginx 版本的场景
- 先看:确认系统 yum 源是否可用,否则需切换至 vault 源
- 建议:生产环境优先考虑迁移至受支持的操作系统,降低安全风险
命令速用版
# 安装编译依赖
sudo yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 下载源码(版本号请根据实际需求调整,建议使用 HTTPS)
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 配置编译参数(负载均衡模块默认启用)
./configure `--prefix`=/usr/local/nginx
# 编译并安装(需要 root 权限)
make && sudo make install核心原理说明
很多用户误以为负载均衡需要单独编译模块,实际上 Nginx 的 ngx_http_upstream_module 是核心 HTTP 模块的一部分,在开源版中默认启用。编译时不需要添加 `--add-module` 或特殊开关即可使用 upstream 指令。真正需要关注的是 CentOS 7 已于 2024 年 6 月 30 日停止维护,官方镜像源已归档,直接运行 yum install 可能会失败,需要手动切换源地址。
分步处理
1. 修复 yum 源
如果执行 yum install 报错,说明默认源已失效。需将 /etc/yum.repos.d/CentOS-Base.repo 中的 mirror.centos.org 替换为 vault.centos.org,或者直接使用国内归档源。
2. 安装依赖包
编译 Nginx 需要 GCC 编译器以及 PCRE、Zlib、OpenSSL 库。执行:
sudo yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel如果缺少 devel 包,编译时会报 header file not found 错误。
3. 下载与解压
从 Nginx 官网下载稳定版源码包,解压后进入目录。不要随意使用第三方修改版,以免引入未知风险。
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.04. 集成新版 OpenSSL(可选但推荐)
CentOS 7 自带的 OpenSSL 版本较旧,如果需要支持 TLS 1.3 等新特性,建议下载 OpenSSL 源码并指定路径编译,而不是使用系统库。
# 下载 OpenSSL 源码(版本按需选择)
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar -zxvf openssl-1.1.1w.tar.gz
# 配置 Nginx 时指定 OpenSSL 路径
./configure `--prefix`=/usr/local/nginx `--with-http`_ssl_module `--with-openssl`=../openssl-1.1.1w5. 配置编译选项
运行 ./configure。负载均衡功能无需额外参数。如果需要 SSL 支持,需添加 `--with-http`_ssl_module。指定安装路径:
./configure `--prefix`=/usr/local/nginx `--with-http`_ssl_module6. 编译与安装
执行 make && sudo make install。编译过程较慢,请勿中断。安装完成后,二进制文件位于 /usr/local/nginx/sbin/nginx。注意 make install 通常需要 root 权限。
7. 配置 systemd 服务(开机自启)
为了方便管理,建议创建 systemd 服务文件 /usr/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
PIDFile=/usr/local/nginx/logs/nginx.pid
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创建完成后重载配置并启动:
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx怎么验证是否生效
1. 检查编译参数
运行 /usr/local/nginx/sbin/nginx -V,输出中应包含 `--with-http`_ssl_module(如果配置了),且没有报错。虽然 upstream 模块不会直接显示在参数列表中,但它是默认内置的。
2. 检查配置文件
在 conf/nginx.conf 中添加简单的 upstream 配置:
upstream backend {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
location / {
proxy_pass http://backend;
}
}运行 /usr/local/nginx/sbin/nginx -t,如果显示 syntax is ok 和 test is successful,说明负载均衡配置被正确识别。
3. 日志验证
启动 Nginx 后,访问页面并检查 logs/access.log,确认请求是否按预期分发。也可使用 curl -v http://localhost 查看响应头。
常见坑
1. 源仓库失效
CentOS 7 停服后,默认 yum 源无法连接。如果不切换至 vault 源,依赖包无法安装,编译会卡在第一步。
2. 命令语法错误
编译参数中不要包含 Markdown 反引号(如 `--prefix`),直接复制执行会报错。确保命令是纯文本。
3. 权限不足make install 和 systemd 配置需要 root 权限,请使用 sudo 或切换到 root 用户执行。
4. SELinux 拦截
如果开启 SELinux,Nginx 可能无法绑定端口或访问文件。测试环境可临时关闭,生产环境建议配置正确的 SELinux 策略而非直接关闭。
5. 误以为需要第三方模块
除非需要特殊的负载均衡算法(如 fair、consistent_hash),否则不要随意编译第三方模块,这会增加维护难度和稳定性风险。
参考来源
- CentOS Wiki: CentOS-7 - CentOS Wiki (https://wiki.centos.org/Manuals/ReleaseNotes/CentOS7)
- Nginx Official Docs: ngx_http_upstream_module (https://nginx.org/en/docs/http/ngx_http_upstream_module.html)
- Nginx Official Docs: Installing nginx open source (https://nginx.org/en/docs/install.html)
- OpenSSL Official Source (https://www.openssl.org/source/)