nginx
Nginx 1.31.0 源码安装与升级指南(含 stub_status 模块)
本文档演示如何在 Linux 系统(OpenEuler CentOS Rocky Linux / RHEL 等)通过源码方式安装 Nginx 1.31.0,并启用 stub_status 模块。
适用于:
Spring Boot / Java 服务反向代理
高并发 Web 服务
自定义模块编译
- 需要自行控制 Nginx 版本的场景
1. 安装编译依赖
安装 Nginx 编译所需组件:
sudo dnf install -y \
gcc \
gcc-c++ \
make \
pcre-devel \
zlib-devel \
openssl-devel \
wget \
perl-devel
如果系统没有 dnf,可改用:
sudo yum install -y ...
2. 下载 Nginx 源码
建议统一放在 /opt/nginx 目录:
sudo mkdir -p /opt/nginx
cd /opt/nginx
下载源码:
wget https://nginx.org/download/nginx-1.31.0.tar.gz
解压:
tar -zxvf nginx-1.31.0.tar.gz
cd nginx-1.31.0
3. 编译安装 Nginx
3.1 配置编译参数
执行:
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-file-aio \
--with-threads \
--with-pcre
3.2 推荐可选模块
根据实际需求添加:
| 模块 | 作用 |
|---|---|
--with-stream | TCP / UDP 四层代理 |
--with-stream_ssl_module | Stream SSL 支持 |
--with-http_gzip_static_module | gzip 静态压缩 |
--with-http_v3_module | HTTP/3 支持(需要较新 OpenSSL) |
示例:
--with-stream \
--with-stream_ssl_module
3.3 编译并安装
make -j$(nproc)
sudo make install
说明:
$(nproc)会自动使用 CPU 全核心加速编译
- make install 会安装到前面 configure 指定的位置
4. 配置 systemd 服务
创建 systemd 服务文件:
sudo tee /etc/systemd/system/nginx.service > /dev/null <<EOF
[Unit]
Description=The NGINX HTTP and Reverse Proxy Server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true
Restart=on-failure
RestartSec=5s
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
4.1 重新加载 systemd
sudo systemctl daemon-reload
4.2 启动 Nginx
sudo systemctl start nginx
设置开机自启:
sudo systemctl enable nginx
查看状态:
sudo systemctl status nginx
5. 配置 stub_status 状态页
编辑配置文件:
sudo vim /etc/nginx/nginx.conf
在 server 中添加:
location /nginx_status {
stub_status;
access_log off;
allow 127.0.0.1;
allow 10.0.0.0/8;
deny all;
}
示例完整配置:
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html;
}
location /nginx_status {
stub_status;
access_log off;
allow 127.0.0.1;
allow 10.0.0.0/8;
deny all;
}
}
5.1 重载配置
sudo nginx -t
sudo systemctl reload nginx
5.2 访问状态页
curl http://127.0.0.1/nginx_status
输出示例:
Active connections: 2
server accepts handled requests
10 10 20
Reading: 0 Writing: 1 Waiting: 1
6. 验证安装
查看版本:
nginx -v
查看完整编译参数:
nginx -V
确认 stub_status 模块:
nginx -V 2>&1 | grep stub_status
查看监听端口:
ss -lntp | grep nginx
7. 升级 Nginx
源码安装升级本质上是:
>使用新版本源码重新编译覆盖旧二进制文件。
7.1 停止当前服务
sudo systemctl stop nginx
7.2 下载新版本源码
cd /opt/nginx
wget https://nginx.org/download/nginx-1.31.0.tar.gz
tar -zxvf nginx-1.31.0.tar.gz
cd nginx-1.31.0
7.3 使用相同编译参数
建议直接复制旧版本参数:
nginx -V
重新执行:
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-file-aio \
--with-threads \
--with-pcre
7.4 编译安装
make -j$(nproc)
sudo make install
说明:
会覆盖旧版本二进制
- 配置文件通常不会被覆盖
7.5 启动并验证
sudo systemctl start nginx
查看版本:
nginx -v
确认模块:
nginx -V 2>&1 | grep stub_status
8. 完全卸载 Nginx
8.1 停止并禁用服务
sudo systemctl stop nginx
sudo systemctl disable nginx
8.2 删除 systemd 服务
sudo rm -f /etc/systemd/system/nginx.service
sudo systemctl daemon-reload
8.3 删除程序与配置
sudo rm -f /usr/sbin/nginx
sudo rm -rf /etc/nginx
sudo rm -rf /usr/local/nginx
8.4 删除日志与运行文件
sudo rm -rf /var/log/nginx
sudo rm -f /run/nginx.pid
8.5 删除源码(可选)
sudo rm -rf /opt/nginx/nginx-1.31.0
sudo rm -f /opt/nginx/nginx-1.31.0.tar.gz
8.6 删除 nginx 用户(如存在)
sudo userdel -r nginx 2>/dev/null
sudo groupdel nginx 2>/dev/null
8.7 验证卸载
nginx -v
预期:
command not found
检查服务:
systemctl status nginx
预期:
Unit nginx.service could not be found
9. 常用命令
| 命令 | 说明 | |
|---|---|---|
nginx -t | 检查配置文件 | |
nginx -s reload | 重载配置 | |
systemctl restart nginx | 重启服务 | |
systemctl status nginx | 查看状态 | |
journalctl -u nginx -f | 查看实时日志 | |
nginx -V | 查看编译参数 | |
| `ps -ef | grep nginx` | 查看进程 |
| `ss -lntp | grep nginx` | 查看端口 |
10. 注意事项
10.1 防火墙开放端口
如果开启了防火墙:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
10.2 SELinux
若启用了 SELinux,可能导致:
无法访问目录
无法反向代理
403 Forbidden
临时关闭:sudo setenforce 0
永久关闭:sudo vim /etc/selinux/config
修改:SELINUX=disabled
10.3 配置文件备份
升级前建议备份:
sudo cp -r /etc/nginx /etc/nginx.bak
10.4 查找残留文件
which nginx
whereis nginx
结束
至此,你已经完成:
Nginx 源码安装
systemd 托管
stub_status 模块启用
Nginx 升级
完整卸载
典型生产架构:Browser ↓ Nginx :80 / :443 ↓ Spring Boot :8080
Nginx 负责:HTTPS
反向代理
负载均衡
静态资源
高并发连接处理
作者:zws 创建时间:2026-05-24 17:14
最后编辑:zws 更新时间:2026-05-24 17:28
最后编辑:zws 更新时间:2026-05-24 17:28