Nginx 配置 2
约 4380 字大约 15 分钟
2025-10-29
配置日志轮换策略
日志轮换:
日志轮换就像是给你的日志文件"自动整理术":
- 📁 自动归档:把旧的日志打包存档
- 🆕 创建新文件:让 Nginx 开始写新的日志
- 🗑️ 清理旧文件:删除太老的日志,节省空间
- 📦 压缩存储:把旧日志压缩,减少磁盘占用
## 如果没有轮换,日志文件会无限增长:
ls -lh /var/log/nginx/
-rw-r--r-- 1 nginx nginx 15G Dec 1 access.log # 15GB!磁盘要爆了!
-rw-r--r-- 1 nginx nginx 8.2G Dec 1 error.log解决方案:
## 启用轮换后:
ls -lh /var/log/nginx/
-rw-r--r-- 1 nginx nginx 2.1M Dec 1 access.log # 当前日志不大
-rw-r--r-- 1 nginx nginx 156K Dec 1 error.log
-rw-r--r-- 1 nginx nginx 45M Nov 30 access.log.1 # 昨天的日志
-rw-r--r-- 1 nginx nginx 12M Nov 30 error.log.1
-rw-r--r-- 1 nginx nginx 42M Nov 29 access.log.2.gz # 前天的,已压缩logrotate 工具详解
- 🔧 系统自带的日志管理工具
- ⏰ 通过 crontab 定时运行(不是常驻进程)
- 📝 配置文件驱动,简单易用
基础配置文件 (/etc/logrotate.d/nginx):
/var/log/nginx/*.log { # 监控这些日志文件
daily # 📅 每天轮换一次
missingok # ❌ 如果日志不存在,不报错
rotate 14 # 🔢 保留14个旧文件(两周)
compress # 📦 压缩旧日志
delaycompress # ⏳ 延迟一天压缩(方便查看昨天日志)
notifempty # 📭 如果日志为空,不轮换
create 0640 nginx nginx # 👤 新日志文件权限和属主
sharedscripts # 🔗 所有文件共享同一个脚本
postrotate # 🔄 轮换后执行的命令
# 告诉 Nginx 重新打开日志文件
invoke-rc.d nginx reload >/dev/null 2>&1
endscript
}参数详细说明
| 参数 | 作用 | 示例值 |
|---|---|---|
daily | 轮换频率 | daily(天)weekly(周)monthly(月) |
rotate | 保留数量 | 14(保留14个版本) |
compress | 是否压缩 | compress(启用) |
size | 按大小轮换 | size 100M(达到100MB就轮换) |
missingok | 文件不存在 | missingok(不报错) |
notifempty | 空文件不轮换 | notifempty |
create | 新文件权限 | create 0640 nginx nginx |
实际使用方法
检查当前配置:
## 查看所有日志轮换配置
sudo logrotate -d /etc/logrotate.conf
## 查看 Nginx 特定配置
sudo logrotate -d /etc/logrotate.d/nginx手动立即轮换:
## 强制立即轮换所有日志
sudo logrotate -vf /etc/logrotate.d/nginx
## 只轮换 Nginx 日志
sudo logrotate -vf /etc/logrotate.conf /etc/logrotate.d/nginx测试配置(不实际执行):
## 干跑测试,看会执行什么操作
sudo logrotate -d /etc/logrotate.d/nginx多目录配置示例
如果你的 Nginx 日志分布在多个目录:
## 主日志目录
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
create 0640 nginx nginx
postrotate
/bin/kill -USR1 `cat /var/run/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}
## 虚拟主机日志目录
/var/log/nginx/domains/*.log {
daily
missingok
rotate 30
compress
create 0640 nginx nginx
postrotate
/bin/kill -USR1 `cat /var/run/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}
## 特定域名日志(更长的保留时间)
/var/log/nginx/domains/example.com/*.log {
daily
missingok
rotate 90
compress
create 0640 nginx nginx
postrotate
/bin/kill -USR1 `cat /var/run/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}关键注意事项
1. postrotate 脚本的重要性:
postrotate
## 必须执行这个命令,让 Nginx 重新打开日志文件
/bin/kill -USR1 `cat /var/run/nginx.pid` 2>/dev/null || true
endscript如果不执行这个,Nginx 会继续往旧的日志文件写数据!
2. 权限问题:
create 0640 nginx nginx确保新创建的日志文件 Nginx 进程有权限写入。
3. 磁盘空间监控:
## 检查日志目录大小
du -sh /var/log/nginx/
## 查看最大的日志文件
ls -lhS /var/log/nginx/故障排查
问题:轮换后没有新日志
## 检查 Nginx 进程是否收到了信号
sudo ps aux | grep nginx
## 手动发送重新打开日志的信号
sudo kill -USR1 $(cat /var/run/nginx.pid)问题:权限错误
## 检查日志文件权限
ls -la /var/log/nginx/
## 修复权限
sudo chown nginx:nginx /var/log/nginx/*.log
sudo chmod 640 /var/log/nginx/*.log总结
日志轮换是 Nginx 运维的基础技能,通过合理的配置可以:
- 防止磁盘被日志撑爆
- 方便历史日志查询
- 优化系统性能
- 满足合规要求
建议在生产环境至少保留 7-30 天的日志,根据业务需求和磁盘空间灵活调整。
Nginx Logrotate 企业级完整配置指南
1. 环境检查与安装
检查当前日志状态
# 查看当前 Nginx 日志文件
ls -la /var/log/nginx/
# 检查日志文件大小
du -sh /var/log/nginx/*
# 查看当前 Nginx 进程
ps aux | grep nginx安装 logrotate(通常系统已自带)
# CentOS/RHEL
sudo yum install logrotate -y
# Ubuntu/Debian
sudo apt-get update && sudo apt-get install logrotate -y
# 验证安装
logrotate --version2. 备份原有配置
# 备份现有 logrotate 配置
sudo cp /etc/logrotate.conf /etc/logrotate.conf.backup
sudo cp -r /etc/logrotate.d/ /etc/logrotate.d.backup/
# 备份 Nginx 日志(可选)
sudo tar -czf nginx-logs-backup-$(date +%Y%m%d).tar.gz /var/log/nginx/3. 企业级 Nginx Logrotate 配置
创建主配置文件 /etc/logrotate.d/nginx:
sudo tee /etc/logrotate.d/nginx > /dev/null << 'EOF'
## =============================================================================
## Nginx Logrotate Enterprise Configuration
## =============================================================================
/var/log/nginx/*.log {
# ==================== 基本设置 ====================
daily # 📅 轮换频率:每天执行一次
missingok # ❌ 如果日志文件不存在,不报错继续执行
rotate 30 # 🔢 保留30个归档文件(企业标准:1个月)
compress # 📦 启用gzip压缩归档文件
delaycompress # ⏳ 延迟压缩:轮换后下一个周期再压缩
notifempty # 📭 如果日志文件为空,不执行轮换
create 0640 nginx adm # 👤 创建新日志文件的权限和属主(用户:组)
# ==================== 高级设置 ====================
dateext # 📝 在归档文件名中添加日期扩展名
dateformat -%Y%m%d # 🗓️ 日期格式:-20231201
extension .log # 🔄 保持文件扩展名一致
# ==================== 大小限制 ====================
maxsize 100M # 📏 如果日志超过100MB立即轮换(即使未到时间)
# ==================== 错误处理 ====================
ifempty # ✅ 即使空文件也轮换(与notifempty互斥,这里用notifempty)
nomail # 📮 不通过邮件发送删除的日志
noolddir # 📁 不在其他目录存储归档
# ==================== 脚本设置 ====================
sharedscripts # 🔗 对所有匹配的日志文件只执行一次脚本
copytruncate # 🔄 复制截断模式(可选,与postrotate二选一)
# ==================== 执行脚本 ====================
prerotate # 🔧 轮换前执行的命令
# 这里可以执行日志分析、备份等预处理
echo "$(date): Starting Nginx log rotation" >> /var/log/logrotate.log
endscript
postrotate # 🔄 轮换后执行的命令(关键!)
# 方法1:使用系统服务命令(推荐)
/bin/systemctl reload nginx > /dev/null 2>&1 || true
# 方法2:直接向Nginx进程发送信号
# /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || true
# 方法3:使用invoke-rc.d(Debian/Ubuntu)
# /usr/sbin/invoke-rc.d nginx rotate > /dev/null 2>&1
# 记录轮换日志
echo "$(date): Nginx logs rotated successfully" >> /var/log/logrotate.log
endscript
# ==================== 错误处理脚本 ====================
lastaction # 🚨 所有轮换完成后执行的命令(即使出错也会执行)
# 清理临时文件、发送通知等
find /var/log/nginx/ -name "*.tmp" -delete
echo "Log rotation completed at $(date)" >> /var/log/logrotate-status.log
endscript
}
## =============================================================================
## 虚拟主机单独配置(可选)
## =============================================================================
/var/log/nginx/domains/*.log {
daily
missingok
rotate 90 # 🗓️ 业务日志保留90天
compress
delaycompress
notifempty
create 0640 nginx adm
dateext
dateformat -%Y%m%d
sharedscripts
postrotate
/bin/systemctl reload nginx > /dev/null 2>&1 || true
endscript
}
## =============================================================================
## 访问日志单独配置(高频访问场景)
## =============================================================================
/var/log/nginx/access.log {
size 1G # 📏 按大小轮换:达到1GB立即轮换
rotate 15 # 🔢 保留15个版本
compress
delaycompress
copytruncate # 🔄 使用复制截断,避免信号问题
missingok
notifempty
create 0640 nginx adm
postrotate
# 对于高频访问日志,使用更轻量的处理
/bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || true
endscript
}
## =============================================================================
## 错误日志配置(长期保留用于分析)
## =============================================================================
/var/log/nginx/error.log {
weekly # 📅 错误日志轮换频率较低
rotate 52 # 🔢 保留52周(1年)的错误日志
compress
missingok
notifempty
create 0640 nginx adm
sharedscripts
postrotate
/bin/systemctl reload nginx > /dev/null 2>&1 || true
endscript
}
EOF4. 配置全局 logrotate 设置
编辑 /etc/logrotate.conf:
sudo tee -a /etc/logrotate.conf > /dev/null << 'EOF'
## =============================================================================
## Global Logrotate Settings
## =============================================================================
# 全局设置
weekly # 默认轮换周期
rotate 4 # 默认保留4个归档
create # 创建新日志文件
dateext # 使用日期扩展名
# 包含所有自定义配置
include /etc/logrotate.d
# 特定目录配置
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# 不压缩某些文件
nocompress
EOF5. 权限和安全设置
# 设置配置文件权限
sudo chmod 644 /etc/logrotate.d/nginx
sudo chown root:root /etc/logrotate.d/nginx
# 创建日志轮换状态目录
sudo mkdir -p /var/lib/logrotate
sudo touch /var/log/logrotate.log
sudo touch /var/log/logrotate-status.log
# 设置状态文件权限
sudo chmod 644 /var/log/logrotate.log
sudo chmod 644 /var/log/logrotate-status.log6. 测试配置
语法检查
# 检查配置文件语法
sudo logrotate -d /etc/logrotate.d/nginx手动测试轮换
# 强制立即执行轮换(详细模式)
sudo logrotate -vf /etc/logrotate.d/nginx
# 测试但不实际执行(干跑模式)
sudo logrotate -d /etc/logrotate.conf验证 Nginx 信号处理
# 检查 Nginx 是否正确处理 USR1 信号
sudo nginx -T | grep pid
# 或者查看进程
sudo systemctl status nginx7. 设置定时任务
检查 logrotate 的 cron 配置:
# 查看 logrotate 定时任务
sudo cat /etc/cron.daily/logrotate
# 或者查看系统 cron
sudo cat /etc/crontab | grep logrotate通常系统会自动在 /etc/cron.daily/logrotate 中配置每日执行。
8. 监控和维护
创建监控脚本
sudo tee /usr/local/bin/check-logrotate.sh > /dev/null << 'EOF'
#!/bin/bash
## Logrotate 状态检查脚本
LOG_FILE="/var/log/logrotate-status.log"
NGINX_LOGS="/var/log/nginx"
echo "=== Logrotate Status Check ==="
echo "检查时间: $(date)"
# 检查最近轮换状态
echo "=== 最近轮换记录 ==="
tail -10 /var/log/logrotate.log 2>/dev/null || echo "无轮换记录"
# 检查日志文件大小
echo -e "\n=== 日志文件大小 ==="
find $NGINX_LOGS -name "*.log" -type f -exec ls -lh {} \; 2>/dev/null
# 检查归档文件
echo -e "\n=== 归档文件统计 ==="
find $NGINX_LOGS -name "*.gz" -type f | wc -l | xargs echo "压缩归档数量:"
# 检查磁盘使用情况
echo -e "\n=== 磁盘使用 ==="
du -sh $NGINX_LOGS
# 检查配置语法
echo -e "\n=== 配置语法检查 ==="
logrotate -d /etc/logrotate.d/nginx > /dev/null 2>&1 && echo "✓ 配置语法正确" || echo "✗ 配置语法错误"
EOF
sudo chmod +x /usr/local/bin/check-logrotate.sh设置定期检查
# 添加到 crontab 每周检查
sudo crontab -l | { cat; echo "0 2 * * 1 /usr/local/bin/check-logrotate.sh >> /var/log/logrotate-monitor.log"; } | sudo crontab -9. 常见问题排查
问题1:轮换后 Nginx 不写新日志
# 解决方案:手动重新加载 Nginx
sudo systemctl reload nginx
# 或者直接发送信号
sudo kill -USR1 $(cat /var/run/nginx.pid)问题2:权限错误
# 检查并修复权限
sudo chown -R nginx:adm /var/log/nginx/
sudo chmod 640 /var/log/nginx/*.log问题3:磁盘空间不足
# 紧急清理脚本
sudo tee /usr/local/bin/emergency-log-clean.sh > /dev/null << 'EOF'
#!/bin/bash
# 保留最近7天日志,删除更早的
find /var/log/nginx -name "*.log.*" -mtime +7 -delete
find /var/log/nginx -name "*.gz" -mtime +7 -delete
# 立即轮换当前日志
logrotate -f /etc/logrotate.d/nginx
EOF
sudo chmod +x /usr/local/bin/emergency-log-clean.sh关键参数说明表
| 参数 | 推荐值 | 说明 |
|---|---|---|
rotate | 30-90 | 根据存储空间和合规要求调整 |
maxsize | 1G-10G | 防止单个日志文件过大 |
compress | 启用 | 节省 70-90% 存储空间 |
dateext | 启用 | 便于日志管理和追溯 |
index 默认首页文件
index 指令的作用
index 指令用于定义默认首页文件。当客户端访问目录路径(以 / 结尾)时,Nginx 会按照配置的顺序依次查找这些文件,返回第一个找到的文件作为响应。
在 http 块中一次性定义全局 index 规则,避免在各个 server 和 location 中重复配置。
错误配置:重复定义
http {
index index.php index.htm index.html; # 全局定义
server {
server_name www.example.com;
location / {
index index.php index.html index.$geo.html; # ❌ 重复定义
...
}
}
server {
server_name www.example.com;
location / {
index index.php index.htm index.html; # ❌ 重复定义
...
}
location /data {
index index.php; # ❌ 重复定义
...
}
}
}正确配置:集中管理
http {
## 一次性定义全局默认首页文件
## 查找顺序:index.php → index.htm → index.html → 根据地理位置变量
index index.php index.htm index.html index.$geo.html;
server {
server_name www.example.com;
location / {
## 自动继承 http 块中的 index 配置
...
}
}
server {
server_name www.example.com;
location / {
## 自动继承 http 块中的 index 配置
...
}
location /data {
## 自动继承 http 块中的 index 配置
...
}
}
}index 指令工作机制
访问流程示例:
用户访问: https://example.com/
Nginx 依次查找:
1. /var/www/example.com/index.php
2. /var/www/example.com/index.htm
3. /var/www/example.com/index.html
4. /var/www/example.com/index.$geo.html
返回第一个找到的文件特殊情况处理
需要覆盖默认配置的场景:
http {
## 全局默认配置
index index.php index.html index.htm;
server {
server_name example.com;
## 大部分 location 继承全局配置
location /api {
## API 接口不需要首页文件
index index.php; # ✅ 合理覆盖:只允许 PHP 文件
}
location /legacy {
## 老系统需要不同的首页顺序
index index.htm index.html; # ✅ 合理覆盖:排除 PHP 文件
}
location /admin {
## 管理后台特殊配置
index admin.php dashboard.php index.php; # ✅ 合理覆盖:特殊业务需求
}
}
}企业级最佳实践
基础配置:
http {
## 标准首页文件顺序(覆盖大部分场景)
index index.php index.html index.htm;
}动态内容优先:
http {
## 动态页面优先,提高处理效率
index index.php index.py index.jsp index.html index.htm;
}多变量支持:
http {
## 支持地理位置、设备类型等变量
index index.$geo.html index.$device.html index.php index.html;
}变量
在 index 指令中使用变量可以实现动态首页选择,根据客户端特征(地理位置、设备类型、语言等)返回最合适的首页文件。
常用变量示例
http {
## 基于多种条件的动态首页配置
index index.$geo.html index.$device.html index.$lang.php index.php index.html;
...
}可用变量详解
1. 地理位置变量
## 需要在 http 块中先定义 geo 变量
geo $geo {
default unknown;
192.168.1.0/24 internal;
10.0.0.0/8 corporate;
1.2.3.4 us;
5.6.7.8 eu;
}
http {
index index.$geo.html index.php index.html;
server {
location / {
## 根据客户端IP返回不同的首页:
## - 内部网络: index.internal.html
## - 公司网络: index.corporate.html
## - 美国用户: index.us.html
## - 欧洲用户: index.eu.html
## - 其他: index.unknown.html → index.php → index.html
}
}
}2. 设备类型变量
## 通过 map 定义设备类型
map $http_user_agent $device {
default "desktop";
"~*android|mobile|iphone|ipod|ipad" "mobile";
"~*bot|crawl|spider" "bot";
}
http {
index index.$device.html index.php index.html;
server {
location / {
## 根据User-Agent返回不同的首页:
## - 手机访问: index.mobile.html (移动端优化)
## - 爬虫访问: index.bot.html (SEO专用)
## - 桌面访问: index.desktop.html (完整功能)
}
}
}3. 语言偏好变量
## 解析 Accept-Language 头
map $http_accept_language $lang {
default "en";
"~*zh" "zh";
"~*ja" "ja";
"~*ko" "ko";
"~*fr" "fr";
"~*de" "de";
}
http {
index index.$lang.php index.php index.html;
server {
location / {
## 根据浏览器语言返回不同版本:
## - 中文用户: index.zh.php
## - 日文用户: index.ja.php
## - 英文用户: index.en.php → index.php
}
}
}完整企业级配置示例
## 定义各种变量
geo $geo {
default global;
CN china;
US america;
JP japan;
EU europe;
}
map $http_user_agent $device {
default "desktop";
"~*mobile|android|iphone" "mobile";
"~*tablet|ipad" "tablet";
"~*bot|crawl|spider" "bot";
}
map $http_accept_language $lang {
default "en";
"~*zh" "zh";
"~*ja" "ja";
"~*ko" "ko";
}
http {
## 动态首页优先级:
## 1. 地理位置+设备+语言组合
## 2. 地理位置+设备
## 3. 地理位置
## 4. 设备类型
## 5. 默认动态页面
## 6. 默认静态页面
index index.$geo-$device-$lang.html
index.$geo-$device.html
index.$geo.html
index.$device.html
index.$lang.php
index.php
index.html;
server {
server_name example.com;
root /var/www/example.com;
location / {
## 实际查找顺序示例:
## 中国移动用户:
## /index.china-mobile-zh.html
## /index.china-mobile.html
## /index.china.html
## /index.mobile.html
## /index.zh.php
## /index.php
## /index.html
}
}
}文件结构示例
/var/www/example.com/
├── index.china-mobile-zh.html # 中国移动端中文版
├── index.china-mobile.html # 中国移动端通用版
├── index.china.html # 中国区通用版
├── index.mobile.html # 全局移动端版
├── index.zh.php # 中文动态版
├── index.php # 默认动态版
├── index.html # 最终回退版
└── assets/ # 静态资源实际应用场景
1. 国际化网站
index index.$lang.$device.html index.$lang.html index.html;
## 文件: index.zh.mobile.html, index.en.desktop.html, index.jp.html2. A/B 测试
map $remote_addr $ab_group {
default "A";
"~*1$" "B"; # IP以1结尾的用户看到B版本
}
index index.$ab_group.html index.html;
## 文件: index.A.html (对照组), index.B.html (实验组)3. 企业内网外网区分
geo $network {
default "external";
10.0.0.0/8 "internal";
192.168.0.0/16 "internal";
}
index index.$network.html index.php index.html;
## 文件: index.internal.html (内网版), index.external.html (外网版)注意事项
- 文件管理:确保所有变量组合对应的文件都存在
- 回退机制:最后一定要有默认文件(index.php/index.html)
- 性能考虑:变量过多可能增加文件查找开销
- 测试验证:充分测试各种场景下的文件查找逻辑
- 缓存策略:对动态首页设置合适的缓存策略
现代定位总结
业务逻辑归业务,基础设施归基础设施,Nginx 应该专注于其核心优势,而非处理应该由应用层负责的业务逻辑。
现代架构的演进,SPA 时代的变革
## 现代 SPA 配置(简单直接)
server {
listen 80;
server_name example.com;
root /var/www/dist;
## 所有路由都返回 index.html,由前端路由处理
location / {
try_files $uri $uri/ /index.html;
}
## API 代理到后端
location /api/ {
proxy_pass http://backend:3000;
}
}为什么 Nginx index 变量过时了
1. 逻辑层转移
- ✅ 现代方案:路由逻辑在前端框架中(React Router、Vue Router)
- ✅ 服务端渲染:在 Node.js/Next.js/Nuxt.js 层面处理
- ❌ Nginx 方案:静态文件匹配,无法实现复杂业务逻辑
2. 动态化能力
// 现代方案 - 完全动态
// Next.js API 路由示例
export async function getServerSideProps(context) {
const { req } = context;
const userAgent = req.headers['user-agent'];
const acceptLanguage = req.headers['accept-language'];
const ip = req.socket.remoteAddress;
// 从数据库获取个性化配置
const personalizedConfig = await fetchPersonalizedConfig({
userAgent,
acceptLanguage,
ip,
userId: req.user?.id
});
return { props: { personalizedConfig } };
}3. 维护性对比
## ❌ 传统 Nginx 配置(难以维护)
index index.$geo-$device-$lang.html index.$geo-$device.html ... [10个组合];
## 需要创建大量静态文件:
# index.china-mobile-zh.html
# index.china-mobile-en.html
# index.us-desktop-en.html
# ... (几何级数增长)// ✅ 现代方案(集中管理)
// components/PersonalizedHomepage.jsx
export default function Homepage({ user }) {
const getHomepageVariant = () => {
if (user.region === 'CN' && user.device === 'mobile') {
return <ChineseMobileHomepage />;
}
if (user.region === 'US' && user.preferences?.layout === 'minimal') {
return <MinimalUSHomepage />;
}
return <DefaultHomepage />;
};
return getHomepageVariant();
}架构演进对比
| 传统方案 | 现代方案 |
|---|---|
| ❌ Nginx 变量动态路由 | ✅ 前端路由 (React/Vue Router) |
| ❌ 静态文件组合匹配 | ✅ 服务端渲染 (Next.js/Nuxt.js) |
| ❌ 基于IP/UA的简单规则 | ✅ 数据库驱动的个性化 |
| ❌ 几何级数文件管理 | ✅ 代码逻辑动态生成 |
Nginx 的正确角色
## 1. 静态资源服务
location /static/ {
root /var/www;
expires 1y;
}
## 2. API 反向代理
location /api/ {
proxy_pass http://backend:3000;
}
## 3. 负载均衡
upstream backend {
server 10.0.1.1:3000;
server 10.0.1.2:3000;
}
## 4. SPA 路由回退
location / {
try_files $uri $uri/ /index.html;
}现代技术栈分工
前端框架 → 路由管理、UI个性化
后端应用 → 业务逻辑、数据个性化
数据库 → 动态配置存储
Nginx → 高性能代理、静态服务、负载均衡
总结要点
- 关注点分离:Nginx 负责基础设施,应用负责业务逻辑
- 灵活性:代码层面的个性化远比静态文件匹配灵活
- 可维护性:集中业务逻辑,避免配置分散
- 动态能力:数据库驱动的实时配置更新
- 开发体验:现代框架提供的完整解决方案
Nginx 作为高性能代理服务器的定位很明确 - 把业务逻辑交给更适合的工具,各自发挥最大价值。