一、前言:为什么需要进阶?
在上一篇Nginx入门指南中,我们掌握了Nginx的基础配置、虚拟主机、Location匹配等核心技能。然而,在真实的生产环境中,Nginx远不止是一个静态资源服务器。
它更多承担着流量入口 、服务网关 和集群调度中枢的角色。企业级架构中,Nginx通常作为整个系统的"第一道防线",负责流量分发、安全过滤、性能优化和高可用保障。
本文将带你深入Nginx的进阶能力,涵盖以下核心主题:
- 反向代理与动静分离
- 负载均衡策略与健康检查
- 缓存机制与性能加速
- Keepalived实现双机热备
- 限流、安全加固与监控
- 平滑升级与配置热重载
- 企业级电商架构实战
通过本篇学习,你将具备构建高可用、高性能Web架构的能力。
二、反向代理:企业架构中的流量调度核心
2.1 什么是反向代理?
反向代理:(Reverse Proxy)是指Nginx作为前端服务器,接收客户端请求,并将请求转发给后端应用服务器(如Tomcat、Node.js、Python服务等),再将响应返回给客户端。
与正向代理(如VPN)不同,反向代理对客户端是透明的,客户端并不知道自己访问的是代理服务器。
2.2 核心配置示例
nginx
# 定义上游服务器组
upstream backend {
server 172.16.1.11:8080; # 应用服务器1
server 172.16.1.12:8080; # 应用服务器2
}
server {
listen 80;
server_name app.freed.cn;
# 反向代理配置
location / {
proxy_pass http://backend; # 核心指令:指向上游组
# 重要:设置正确的HTTP头
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 超时设置
proxy_connect_timeout 30s;
proxy_read_timeout 30s;
proxy_send_timeout 30s;
}
# 静态资源由Nginx直接处理,实现动静分离
location ~* \.(jpg|jpeg|png|gif|css|js)$ {
root /app/static;
expires 7d;
access_log off;
}
}
2.3 关键Header说明
| 头信息 | 作用 | 后端获取方式(Java示例) |
|---|---|---|
X-Real-IP |
客户端真实IP | request.getHeader("X-Real-IP") |
X-Forwarded-For |
代理链IP列表 | request.getHeader("X-Forwarded-For") |
X-Forwarded-Proto |
原始协议(http/https) | request.getHeader("X-Forwarded-Proto") |
生产建议 :后端服务应通过这些Header获取真实客户端信息,而非直接使用
$remote_addr。
三、负载均衡:流量分发策略详解
3.1 常见负载均衡算法
| 算法 | 配置方式 | 特点 | 适用场景 |
|---|---|---|---|
| 轮询(默认) | server ... |
均匀分发 | 服务器性能相近 |
| 加权轮询 | server ... weight=3 |
按权重分配 | 服务器配置不均 |
| IP Hash | ip_hash; |
同一IP固定访问某节点 | 需要会话保持 |
| 最少连接 | least_conn; |
优先发给连接少的节点 | 请求处理时间差异大 |
3.2 完整负载均衡配置
nginx
upstream web_cluster {
# 加权轮询示例
server 172.16.1.11:8080 weight=5 max_fails=3 fail_timeout=30s;
server 172.16.1.12:8080 weight=3 max_fails=3 fail_timeout=30s;
server 172.16.1.13:8080 weight=1 max_fails=3 fail_timeout=30s;
# 备份服务器,平时不参与负载,主服务器宕机时启用
server 172.16.1.14:8080 backup;
# 会话保持:同一客户端IP固定访问某后端(15分钟)
ip_hash;
# 或者使用最少连接算法
# least_conn;
}
server {
listen 80;
server_name cluster.freed.cn;
location / {
proxy_pass http://web_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# 健康检查相关
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 3s;
}
# Nginx状态监控(负载均衡状态)
location /nginx_status {
stub_status;
allow 172.16.1.0/24;
deny all;
access_log off;
}
}
3.3 健康检查机制
Nginx开源版采用被动健康检查:
sh
upstream backend {
server 172.16.1.11:8080 max_fails=3 fail_timeout=30s;
server 172.16.1.12:8080 max_fails=3 fail_timeout=30s;
}
max_fails=3:连续失败3次标记为不可用fail_timeout=30s:30秒内不向该节点转发请求
企业级方案 :Nginx Plus支持主动健康检查,或可使用第三方模块
nginx_upstream_check_module实现更精细的监控。
四、缓存优化:提升性能与降低后端压力
4.1 代理缓存配置
nginx
# 定义缓存路径和参数
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m
max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name cache.freed.cn;
# 缓存配置
proxy_cache my_cache;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 10m; # 200/302状态码缓存10分钟
proxy_cache_valid 404 1m; # 404状态码缓存1分钟
proxy_cache_valid any 5m; # 其他状态码缓存5分钟
# 缓存锁定:防止多个请求同时回源
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
# 添加缓存状态头(用于调试)
add_header X-Cache-Status $upstream_cache_status;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
# 仅缓存GET和HEAD请求
proxy_cache_methods GET HEAD;
# 根据响应头决定是否缓存
proxy_ignore_headers Cache-Control;
proxy_hide_header Cache-Control;
}
}
4.2 缓存状态说明
通过 X-Cache-Status 响应头判断缓存状态:
HIT:缓存命中MISS:未命中,回源获取BYPASS:跳过缓存(如配置了proxy_cache_bypass)EXPIRED:缓存已过期UPDATING:缓存正在更新
建议:在测试阶段开启此Header,便于调试缓存策略。
五、高可用集群:Keepalived + Nginx 双机热备
5.1 架构设计
互联网
|
+-- 虚拟IP (VIP): 172.16.1.100
|
+-- Nginx + Keepalived (主节点: lb01)
|
+-- Nginx + Keepalived (备节点: lb02)
|
+-- 后端应用服务器集群 (app01, app02, app03)
通过Keepalived实现VRRP协议,主节点持有虚拟IP,当主节点宕机时,VIP自动漂移到备节点,实现高可用。
5.2 Keepalived 主节点配置
主节点 (lb01) /etc/keepalived/keepalived.conf:
bash
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL01 # 节点标识,每个节点不同
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh" # 健康检查脚本
interval 2
weight -20 # 检查失败时优先级降低20
}
vrrp_instance VI_1 {
state MASTER # 主节点为MASTER,备节点为BACKUP
interface eth0 # 网络接口
virtual_router_id 51 # 虚拟路由ID,主备节点必须相同
priority 100 # 优先级,主节点高于备节点(如备节点设为90)
advert_int 1 # 心跳间隔
authentication {
auth_type PASS
auth_pass 1111 # 密码,主备节点相同
}
track_script {
chk_nginx # 引用健康检查脚本
}
virtual_ipaddress {
172.16.1.100/24 # 虚拟IP(VIP)
}
}
5.3 备节点配置(关键差异)
备节点 (lb02) 配置类似,主要修改:
bash
global_defs {
router_id LVS_DEVEL02 # 修改节点标识
}
vrrp_instance VI_1 {
state BACKUP # 备节点
priority 90 # 优先级低于主节点
# 其他配置与主节点相同
}
5.4 Nginx健康检查脚本
bash
#!/bin/bash
# Nginx健康检查脚本
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ];then
# 尝试重启Nginx
systemctl start nginx
sleep 2
# 检查重启是否成功
if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
# 重启失败,停止keepalived,触发VIP漂移
systemctl stop keepalived
fi
fi
# 添加更严格的检查:检查Nginx是否真正响应
if ! curl -f http://localhost/nginx_status &>/dev/null; then
systemctl stop keepalived
fi
赋予执行权限:
bash
chmod +x /etc/keepalived/check_nginx.sh
5.5 启动与验证
bash
# 两个节点都执行
systemctl enable keepalived
systemctl start keepalived
# 检查VIP是否在主节点上
ip addr show eth0 | grep 172.16.1.100
# 测试故障转移:在主节点停止keepalived,观察VIP是否漂移到备节点
systemctl stop keepalived # 在主节点执行
六、高级功能实战
6.1 限流与防护
nginx
# 定义限流区域(在http块中)
http {
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
server {
location /api/ {
# 限流:每秒10个请求,突发不超过20个
limit_req zone=api burst=20 nodelay;
proxy_pass http://backend;
}
# 下载限速
location /download/ {
limit_rate 500k; # 限速500KB/s
root /data/files;
}
}
}
rate=10r/s:每秒10个请求burst=20:突发允许20个请求nodelay:不延迟处理,超出立即拒绝
6.2 安全加固
nginx
server {
# 隐藏Nginx版本号
server_tokens off;
# 安全头设置
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
# 限制HTTP方法
if ($request_method !~ ^(GET|POST|HEAD)$) {
return 444; # 关闭连接,不发送响应
}
# 禁止敏感文件访问
location ~* \.(log|env|git|svn)$ {
deny all;
return 404;
}
}
七、配置热重载(零停机)
bash
# 测试配置语法
nginx -t
# 重载配置(优雅重启工作进程)
nginx -s reload
注意:重载不会影响正在处理的请求,是生产环境推荐的操作方式。
八、监控与排错
8.1 状态监控
nginx
# 扩展状态监控
location /nginx_status {
stub_status;
access_log off;
allow 172.16.1.0/24;
deny all;
}
# 自定义状态页面
location /status {
# 使用第三方模块或自定义HTML展示详细状态
return 200 "Nginx Status: Active\n";
add_header Content-Type text/plain;
}
访问后返回:
Active connections: 2
server accepts handled requests
106 106 795
Reading: 0 Writing: 1 Waiting: 1
8.2 自定义日志格式
nginx
log_format main_ext '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time uct="$upstream_connect_time" '
'uht="$upstream_header_time" urt="$upstream_response_time"';
access_log /var/log/nginx/access.log main_ext;
可用于分析响应时间、上游延迟等性能指标。
九、企业级实战案例:电商平台架构
9.1 需求分析
- 域名:
mall.freed.cn - 要求:
- 高可用
- 负载均衡
- 动静分离
- 缓存加速
- 安全防护
- 管理后台IP限制
9.2 完整配置
nginx
# 全局配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 10240;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 日志格式
log_format main_ext '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main_ext;
# 基础优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# 缓存配置
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mall_cache:50m
max_size=10g inactive=24h use_temp_path=off;
# 上游服务器定义
upstream mall_backend {
least_conn;
server 172.16.1.21:8080 weight=3 max_fails=3 fail_timeout=30s;
server 172.16.1.22:8080 weight=3 max_fails=3 fail_timeout=30s;
server 172.16.1.23:8080 weight=2 max_fails=3 fail_timeout=30s;
server 172.16.1.24:8080 backup;
}
upstream mall_static {
server 172.16.1.31:80 weight=1;
server 172.16.1.32:80 weight=1;
}
# 主服务器配置
server {
listen 80;
server_name mall.freed.cn;
root /app/mall;
# 安全头
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
# 静态资源 - CDN边缘节点处理
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
# 可配置代理到静态资源专用服务器
# proxy_pass http://mall_static;
}
# API接口 - 负载均衡 + 缓存
location /api/ {
proxy_pass http://mall_backend;
proxy_cache mall_cache;
proxy_cache_valid 200 302 5m;
proxy_cache_valid 404 1m;
# 限流
limit_req zone=api burst=10 nodelay;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 动态页面 - 负载均衡
location / {
proxy_pass http://mall_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 管理后台 - IP限制
location /admin/ {
allow 172.16.1.0/24;
allow 10.0.0.0/8;
deny all;
proxy_pass http://mall_backend;
proxy_set_header Host $host;
}
# 状态监控
location /nginx_status {
stub_status;
access_log off;
allow 172.16.1.0/24;
deny all;
}
}
}
十、总结:企业级Nginx架构师能力模型
| 能力层级 | 核心技能 |
|---|---|
| 基础运维 | 安装、配置、虚拟主机、日志管理 |
| 进阶实战 | 反向代理、负载均衡、缓存策略 |
| 架构设计 | 高可用集群、动静分离、安全控制 |
| 性能专家 | 性能调优、监控告警、故障排查 |