Linux 服务器运维之 Nginx 案例化培训教程
Linux 服务器运维之 Nginx 案例化培训教程
第一章 Linux 基础实操案例(为 Nginx 运维打基础)
案例 1:Linux 日志分析(定位 “Nginx 安装失败” 前兆)
场景描述
在安装 Nginx 前,需确认 Linux 系统是否有 “依赖包缺失”“权限不足” 等潜在问题,可通过查看系统日志(/var/log/messages)和 yum 日志(/var/log/yum.log)排查。
前置条件
-
已登录 Linux 系统(CentOS 7 为例,root 权限)
-
系统可正常联网(需 yum 命令下载依赖)
操作步骤
-
查看系统日志中 “依赖包相关错误”
执行命令:
grep "libpcre" /var/log/messages
(Nginx 依赖 pcre 库,排查是否有该库缺失日志)
-
若输出空:说明暂无 pcre 相关错误;
-
若输出类似 “error: pcre-devel not found”:需提前安装依赖。
-
查看 yum 安装历史(确认是否曾安装过 Nginx 残留)
执行命令:
grep "nginx" /var/log/yum.log
- 若输出类似 “Installed: nginx-1.20.1-9.el7.x86_64”:需先卸载旧版本(
yum remove -y nginx
)。
-
验证当前系统网络与权限
执行命令:
ping -c 3 ``mirrors.aliyun.com
(验证联网)、id
(确认当前用户为 root,输出 “uid=0 (root)”)。
预期效果
- 能通过日志快速定位 “依赖缺失”“旧版本残留” 问题,避免后续 Nginx 安装失败。
注意事项
- Ubuntu 系统日志路径为
/var/log/syslog
,yum 日志替换为/var/log/dpkg.log
(对应 apt 命令)。
第二章 Nginx 安装配置案例(两种主流方式)
案例 2:CentOS 7 下 yum 安装 Nginx(快速部署)
场景描述
需在 10 台服务器上快速部署 Nginx,yum 方式无需编译,适合批量操作。
前置条件
-
已完成案例 1 的日志排查,无依赖冲突;
-
已配置阿里云 yum 源(避免官方源速度慢)。
操作步骤
-
配置 Nginx 官方 yum 源
创建配置文件:
vi /etc/yum.repos.d/nginx.repo
,粘贴以下内容:
\[nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx\_signing.key
保存退出(:wq
)。
-
安装并启动 Nginx
执行命令:
yum clean all && yum makecache
(清理缓存,加载新源)yum install -y nginx
(安装)systemctl start nginx
(启动)systemctl enable nginx
(设置开机自启) -
验证安装成功
-
本地验证:
curl ``http://127.0.0.1
(输出 Nginx 默认欢迎页 HTML 代码); -
远程验证:在浏览器输入服务器 IP(需开放 80 端口:
firewall-cmd --add-port=80/tcp --permanent && firewall-cmd --reload
),能看到 “Welcome to nginx!” 页面。
预期效果
- 10 分钟内完成单台 Nginx 部署,且服务可正常访问。
注意事项
- 若执行
systemctl start nginx
报错 “Job for nginx.service failed”,需查看日志:journalctl -u nginx
,大概率是 80 端口被占用(用netstat -tulpn | grep 80
排查,kill 占用进程)。
案例 3:Ubuntu 20.04 下源码安装 Nginx(自定义编译参数)
场景描述
需为 Nginx 添加 “SSL 模块”(yum 默认安装可能不含),需通过源码编译实现。
操作步骤
-
安装编译依赖
apt update && apt install -y gcc make libpcre3-dev zlib1g-dev openssl libssl-dev
-
下载并解压 Nginx 源码包
wget ``http://nginx.org/download/nginx-1.24.0.tar.gz
(官网最新稳定版)tar -zxvf nginx-1.24.0.tar.gz && cd nginx-1.24.0
-
自定义编译(启用 SSL 模块)
执行配置命令:
./configure \\\--prefix=/usr/local/nginx \ # 安装路径\--with-http\_ssl\_module \ # 启用SSL模块\--with-http\_stub\_status\_module # 启用状态监控模块
编译并安装:make && make install
-
启动并验证模块
/usr/local/nginx/sbin/nginx
(启动)/usr/local/nginx/sbin/nginx -V
(查看编译参数,需包含 “–with-http_ssl_module”)
第三章 Nginx 核心功能案例(运维高频场景)
案例 4:反向代理(代理后端 Tomcat 服务)
场景描述
后端 Tomcat 服务运行在192.168.1.100:8080
,需通过 Nginx(192.168.1.10:80
)代理访问,实现 “用户访问 80 端口→Nginx 转发到 8080 端口”。
操作步骤
-
编辑 Nginx 配置文件
打开主配置:
vi /etc/nginx/nginx.conf
(yum 安装路径),在http
块中添加server
块:
server {  listen 80;  server\_name 192.168.1.10; # Nginx服务器IP  location / {  proxy\_pass http://192.168.1.100:8080; # 转发到Tomcat  proxy\_set\_header Host \$host; # 传递客户端Host头  proxy\_set\_header X-Real-IP \$remote\_addr; # 传递客户端真实IP  }}
-
检查配置并重启 Nginx
nginx -t
(语法检查,输出 “test is successful” 为正常)systemctl restart nginx
-
验证代理效果
-
客户端浏览器访问
http://192.168.1.10
,应显示 Tomcat 默认页面; -
查看 Tomcat 日志(
CATALINA_HOME/logs/localhost_access_log.*.txt
),应能看到客户端真实 IP(而非 Nginx 的 IP)。
案例 5:负载均衡(2 台 Apache 服务分摊压力)
场景描述
有 2 台 Apache 服务器(192.168.1.20:80
、192.168.1.21:80
),需通过 Nginx 实现 “加权轮询” 负载均衡(1.20 权重 2,1.21 权重 1,即 2:1 分配请求),且故障节点自动剔除。
操作步骤
-
配置 upstream 负载均衡池
编辑
nginx.conf
,在http
块最上方添加upstream
块:
upstream apache\_servers {  server 192.168.1.20:80 weight=2 max\_fails=2 fail\_timeout=30s; # 权重2,失败2次剔除30秒  server 192.168.1.21:80 weight=1 max\_fails=2 fail\_timeout=30s; # 权重1}
-
配置 server 块转发到负载池
在
http
块中添加server
块:
server {  listen 80;  server\_name lb.nginx.com; # 自定义域名(需在客户端hosts解析)  location / {  proxy\_pass http://apache\_servers; # 转发到负载池  proxy\_set\_header Host \$host;  }}
- 验证负载效果
-
客户端配置 hosts:
192.168.1.10`` ``lb.nginx.com
; -
多次访问
http://lb.nginx.com
,查看 2 台 Apache 的访问日志(/var/log/httpd/access_log
):-
1.20 的日志条目数量约为 1.21 的 2 倍,符合权重配置;
-
手动停止 192.168.1.20 的 Apache(
systemctl stop httpd
),访问请求会全部转发到 1.21,30 秒后重启 1.20,请求会自动恢复分配。
-
案例 6:HTTPS 配置(Let’s Encrypt 免费证书)
场景描述
需为 Nginx 配置 HTTPS,使用 Let’s Encrypt 免费证书,实现 “访问http://lb.nginx.com
自动跳转https://lb.nginx.com
”。
操作步骤
-
安装 Certbot(证书申请工具)
yum install -y epel-release && yum install -y certbot python3-certbot-nginx
(CentOS 7) -
申请并自动配置证书
执行命令:
certbot --nginx -d ``lb.nginx.com
- 过程中输入邮箱(用于证书到期提醒),同意条款,选择 “Redirect”(HTTP 自动跳转 HTTPS)。
- 验证 HTTPS 效果
-
浏览器访问
http://lb.nginx.com
,会自动跳转https://lb.nginx.com
,地址栏显示 “小绿锁”; -
查看 Nginx 配置(
nginx.conf
),Certbot 已自动添加 HTTPS 相关配置:
listen 443 ssl;ssl\_certificate /etc/letsencrypt/live/lb.nginx.com/fullchain.pem;ssl\_certificate\_key /etc/letsencrypt/live/lb.nginx.com/privkey.pem;
第四章 Nginx 性能优化案例(高并发场景)
案例 7:调整并发连接数(支持 10000 + 并发)
场景描述
Nginx 默认并发连接数较低(约 1024),需优化配置,使单台 Nginx 支持 10000 + 并发请求。
操作步骤
-
优化 Nginx 配置
编辑
nginx.conf
,修改worker_processes
和events
块:
worker\_processes auto; # 自动匹配CPU核心数(如4核CPU则为4)worker\_rlimit\_nofile 65535; # 每个worker进程最大打开文件数events {  worker\_connections 20480; # 每个worker支持的最大连接数(4核×20480=81920,满足10000+)  use epoll; # 启用高效I/O模型(Linux推荐)}
-
优化 Linux 内核参数
编辑
/etc/sysctl.conf
,添加以下内容:
net.core.somaxconn = 65535 # 系统最大监听队列长度net.ipv4.tcp\_max\_tw\_buckets = 10000 # TIME\_WAIT状态连接最大数量net.ipv4.tcp\_tw\_reuse = 1 # 允许TIME\_WAIT连接复用
生效配置:sysctl -p
-
测试并发效果
使用
ab
工具(Apache Bench)测试:ab -n 10000 -c 1000 ``https://lb.nginx.com/
(10000 个请求,1000 并发)
- 预期结果:无 “Connection refused” 错误,请求成功率 100%,平均响应时间 < 500ms。
第五章 Nginx 故障排查案例(运维必踩坑)
案例 8:Nginx 启动失败(配置文件语法错误)
场景描述
执行systemctl start nginx
报错:“Job for nginx.service failed because the control process exited with error code.”
排查步骤
-
查看详细错误日志
执行命令:
journalctl -u nginx -xe
,输出类似:“nginx: [emerg] unknown directive “proxt_pass” in /etc/nginx/nginx.conf:25”
→ 定位问题:
proxt_pass
拼写错误(正确为proxy_pass
)。 -
验证配置文件语法
执行
nginx -t
,会直接提示错误位置:“nginx: [emerg] unknown directive “proxt_pass” in /etc/nginx/nginx.conf:25”
“test failed”
-
修复并重启
编辑
nginx.conf
,将 “proxt_pass” 改为 “proxy_pass”,再次执行nginx -t
(显示 “test is successful”),最后systemctl start nginx
。
案例 9:反向代理失效(后端服务宕机)
场景描述
访问http://192.168.1.10
(Nginx)时,显示 “502 Bad Gateway”。
排查步骤
-
查看 Nginx 错误日志
执行
tail -f /var/log/nginx/error.log
,输出类似:“connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.30, server: 192.168.1.10, request: “GET / HTTP/1.1”, upstream: “http://192.168.1.100:8080/”, host: “192.168.1.10””
→ 问题:Nginx 无法连接后端 192.168.1.100:8080。
-
检查后端服务状态
登录 192.168.1.100,执行
systemctl status tomcat
,显示 “inactive (dead)”→ Tomcat 未启动。 -
修复后端服务
启动 Tomcat:
systemctl start tomcat
,再次访问http://192.168.1.10
,恢复正常。
(注:文档部分内容可能由 AI 生成)