以下是一个关于Nginx动静分离的详细配置示例,涵盖基础配置、性能优化、安全防护及监控方案:
一、基础动静分离配置
http {
# 定义后端应用服务器组
upstream app_servers {
server 192.168.1.100:8080; # 主应用服务器
server 192.168.1.101:8080 backup; # 备用服务器
keepalive 32; # 保持32个长连接
}
server {
listen 80;
server_name example.com www.example.com;
# 静态资源目录配置
location /static/ {
alias /data/static_assets/; # 静态资源物理路径
expires 365d; # 浏览器缓存1年
access_log off; # 关闭访问日志
add_header Cache-Control "public, max-age=31536000";
}
# 动态请求代理
location / {
proxy_pass http://app_servers;
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 5s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}
# 敏感文件保护
location ~ /(\.git|\.env|\.htaccess) {
deny all;
return 404;
}
}
}
二、高级性能优化配置
1. 缓存策略优化
http {
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=static_cache:100m inactive=7d max_size=10g; # 缓存存储配置
server {
location /static/ {
alias /data/static_assets/;
# 缓存控制
proxy_cache static_cache;
proxy_cache_valid 200 304 7d; # 200/304状态码缓存7天
proxy_cache_use_stale error timeout updating; # 后端故障时使用过期缓存
proxy_cache_lock on; # 防止缓存击穿
}
}
}
2. 压缩传输优化
http {
# 开启Brotli压缩(需编译支持)
brotli on;
brotli_comp_level 6;
brotli_types text/css text/javascript application/javascript image/svg+xml;
# 备用GZIP压缩
gzip on;
gzip_min_length 1024;
gzip_comp_level 5;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_proxied expired no-cache no-store private auth;
}
3. 连接复用优化
http {
# TCP参数调优
tcp_nopush on; # 合并小数据包发送
tcp_nodelay on; # 禁用Nagle算法
reset_timedout_connection on; # 主动关闭超时连接
# 长连接配置
keepalive_requests 1000; # 单个长连接最大请求数
keepalive_timeout 65s; # 长连接超时时间
}
三、安全加固配置
1. 访问控制
server {
location /static/ {
# IP白名单
allow 192.168.1.0/24;
allow 10.0.0.0/8;
deny all;
# 防盗链配置
valid_referers none blocked example.com *.example.com;
if ($invalid_referer) {
return 403 "Forbidden";
}
}
}
2. 请求过滤
server {
# 限制请求方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
# 防止SQL注入
location / {
if ($query_string ~ "(\<|%3C).*script.*(\>|%3E)") {
return 403;
}
}
}
四、监控与维护配置
1. 实时状态监控
http {
server {
listen 8080;
server_name status.example.com;
location /nginx_status {
stub_status on; # 基础状态页
allow 192.168.1.0/24; # 仅允许内网访问
deny all;
}
location /cache_status {
proxy_cache_path; # 缓存状态信息(需Nginx Plus)
allow 192.168.1.0/24;
deny all;
}
}
}
2. 详细日志分析
http {
log_format动静分离 '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$upstream_cache_status ' # 缓存命中状态
'$upstream_addr $request_time';
access_log /var/log/nginx/access.log动静分离;
}
五、配置验证与优化
-
语法检查
nginx -t -c /etc/nginx/nginx.conf
-
性能调优建议
- 根据业务特性调整
worker_processes
和worker_connections
- 对高频静态资源启用
open_file_cache
- 使用
sendfile on
提升文件传输效率 - 监控
proxy_cache
命中率,定期清理过期缓存
- 根据业务特性调整
-
安全加固措施
- 通过
ssl_stapling
启用OCSP装订(HTTPS场景) - 使用
ssl_session_ticket_key
配置会话票据 - 定期轮换SSL证书和API密钥
- 通过
六、典型应用场景扩展
1. 图片处理服务
location /image/ {
alias /data/images/;
# 图片压缩参数
image_filter resize 800 600; # 限制最大尺寸
image_filter_jpeg_quality 85; # JPEG质量
image_filter_buffer 10M; # 缓冲区大小
}
2. 视频流媒体服务
location /video/ {
alias /data/videos/;
# 范围请求支持
add_header 'Accept-Ranges' 'bytes';
# 视频分片缓存
proxy_cache_key $uri$is_args$args$slice_range;
proxy_cache_valid 200 206 1h;
}
该配置示例实现了动静分离的核心功能,可根据实际需求扩展以下方向:
- 集成CDN加速(通过
resolver
指令配置DNS解析) - 添加灰度发布(通过
split_clients
模块) - 实现服务发现(与Consul/Etcd集成)
- 配置WAF防护(通过ModSecurity模块)
建议通过压力测试工具(如wrk)验证配置效果,并结合APM工具(如Prometheus+Grafana)构建完整监控体系。