技能目标
- 熟练掌握 Nginx 核心性能优化参数与配置方法
- 掌握 Nginx 多维度监控方案,实现服务状态可视化与故障预警
前言
在互联网流量持续增长、高并发场景常态化的今天,Nginx 作为 Web 服务、反向代理与负载均衡的核心组件,其运行效率直接决定业务响应速度与系统稳定性。通过合理调优释放服务器硬件性能,通过全面监控实时掌握服务运行状态,是运维人员保障业务连续性的必备能力。
本章围绕 Nginx 性能调优、日志管理、全方位监控三大核心内容,通过实操配置与工具部署,帮助学习者掌握生产环境下 Nginx 的高效运维方案。
一、Nginx 性能优化实战
1. 标准化编译安装 Nginx
(1)安装基础依赖组件
Nginx 编译运行依赖开发库与工具,需提前安装相关依赖包:
bash
运行
[root@localhost ~]# dnf install -y gcc make pcre-devel zlib-devel openssl-devel perl-ExtUtils-MakeMaker git wget
(2)创建专用运行用户与日志目录
为提升运行安全,创建无登录权限的专用用户,并配置独立日志目录:
bash
运行
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx:nginx /var/log/nginx
(3)源码编译与安装
选用稳定版 Nginx,编译时启用高性能与安全相关模块:
bash
运行
[root@localhost ~]# tar zxf nginx-1.26.3.tar.gz
[root@localhost ~]# cd nginx-1.26.3
[root@localhost nginx-1.26.3]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-pcre \
--with-stream
[root@localhost nginx-1.26.3]# make && make install
(4)创建软链接与系统服务
bash
运行
[root@localhost nginx-1.26.3]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
配置 systemd 服务文件,实现开机自启与标准化管理:
bash
运行
[root@localhost ~]# vim /lib/systemd/system/nginx.service
ini
[Unit]
Description=Nginx HTTP and Reverse Proxy Server
After=network.target
[Service]
Type=forking
ExecStartPre=/usr/local/sbin/nginx -t
ExecStart=/usr/local/sbin/nginx
ExecReload=/usr/local/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
User=root
Group=root
[Install]
WantedBy=multi-user.target
加载配置并启动服务:
bash
运行
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx
2. 运行用户与用户组优化
Nginx 默认使用 nobody 用户运行,生产环境需改为专用 nginx 用户,有两种配置方式:
- 编译安装时指定:
--user=nginx --group=nginx - 配置文件修改:
bash
运行
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
3. 进程数与最大连接数优化
(1)工作进程数优化
worker_processes 建议设置为CPU 核心数,高并发场景可设为 2 倍:
bash
运行
[root@localhost ~]# nproc
4
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
开启 CPU 亲和性,充分利用多核处理器:
bash
运行
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
(2)单进程最大连接数优化
提升单个 worker 进程可处理的并发连接数量:
bash
运行
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 4096;
}
Nginx 总并发连接计算公式:总并发 = worker_processes × worker_connections
4. 静态资源缓存优化
对图片、样式、图标等静态资源设置浏览器缓存,减少重复请求:
bash
运行
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~* \.(gif|jpg|jpeg|png|bmp|ico|css|js)$ {
root html;
expires 7d;
}
配置完成后检查语法并重新加载:
bash
运行
[root@localhost ~]# nginx -t && nginx -s reload
5. 连接超时参数优化
合理设置长连接超时时间,避免连接占用资源:
bash
运行
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
keepalive_timeout 65 180;
}
- 第一个值:服务器端关闭连接的超时时间
- 第二个值:响应头中告知客户端的超时时间
6. 自动化日志切割
Nginx 无原生日志分割功能,通过脚本 + 计划任务实现定期切割:
(1)编写日志切割脚本
bash
运行
[root@localhost ~]# vim /opt/cut_logs.sh
bash
运行
#!/bin/bash
log_dir="/var/log/nginx"
ng_log="/usr/local/nginx/logs/access.log"
pid_file="/usr/local/nginx/logs/nginx.pid"
date_str=$(date -d "yesterday" +%Y%m%d)
[ -d ${log_dir} ] || mkdir -p ${log_dir}
mv ${ng_log} ${log_dir}/web-access-${date_str}.log
kill -USR1 $(cat ${pid_file})
find ${log_dir} -mtime +30 -delete
(2)授权并配置定时任务
bash
运行
[root@localhost ~]# chmod +x /opt/cut_logs.sh
[root@localhost ~]# crontab -e
30 1 * * * /opt/cut_logs.sh
每天凌晨 1:30 自动执行日志切割。
7. Gzip 网页压缩优化
开启 Gzip 压缩,降低传输带宽占用,提升访问速度:
bash
运行
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
nginx
http {
gzip on;
gzip_min_length 1k;
gzip_buffers 4 64k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_vary on;
gzip_types text/plain text/css text/javascript application/javascript application/xml image/svg+xml;
}
验证压缩是否生效:
bash
运行
[root@localhost ~]# curl -I -H "Accept-Encoding: gzip" http://服务器IP
响应头出现Content-Encoding: gzip即为生效。
二、Nginx 全方位监控方案
1. GoAccess 实时日志监控
GoAccess 是轻量级开源日志分析工具,支持终端与网页可视化展示。
(1)安装 GoAccess
bash
运行
[root@localhost ~]# dnf install -y gcc make ncurses-devel openssl-devel gettext-devel wqy-microhei-fonts
[root@localhost ~]# tar -xzvf goaccess-1.7.2.tar.gz
[root@localhost ~]# cd goaccess-1.7.2
[root@localhost goaccess-1.7.2]# ./configure --enable-utf8 --with-openssl
[root@localhost goaccess-1.7.2]# make && make install
(2)配置中文环境
bash
运行
[root@localhost ~]# dnf install -y glibc-langpack-zh
[root@localhost ~]# localectl set-locale LANG=zh_CN.UTF-8
(3)生成实时 HTML 监控报告
bash
运行
[root@localhost ~]# goaccess /usr/local/nginx/logs/access.log \
--log-format=COMBINED \
--output=/usr/local/nginx/html/monitor.html \
--real-time-html
(4)访问查看
浏览器访问:http://服务器IP/monitor.html,查看实时访问数据。
2. Nginx-VTS 流量状态监控
VTS 模块用于监控虚拟主机流量、请求数、带宽等核心指标。
(1)编译 Nginx 并添加 VTS 模块
bash
运行
[root@localhost ~]# unzip nginx-module-vts-master.zip
[root@localhost ~]# cd nginx-1.26.3
[root@localhost nginx-1.26.3]# ./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 \
--add-module=/root/nginx-module-vts-master
[root@localhost nginx-1.26.3]# make && make install
(2)开启 VTS 监控配置
bash
运行
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
nginx
http {
vhost_traffic_status_zone;
server {
listen 80;
location /vts-status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
access_log off;
}
}
}
(3)验证监控页面
bash
运行
[root@localhost ~]# nginx -t && nginx -s reload
浏览器访问:http://服务器IP/vts-status,查看流量与请求统计。
总结
本章完整覆盖 Nginx 性能调优与深度监控两大核心技能:
- 性能优化:通过进程配置、连接优化、静态缓存、Gzip 压缩、日志切割等手段,显著提升高并发场景下的处理效率与资源利用率。
- 全方位监控:基于 GoAccess 实现日志可视化,基于 VTS 模块实现流量状态监控,双方案结合可实时掌握 Nginx 运行健康度。
- 生产价值:优化降低服务器负载,监控提前发现故障,共同保障 Web 服务稳定、高效、持续运行。
掌握以上技能,可从容应对中小型至大型网站的 Nginx 运维需求。