qiangdanxitong/Nginx缓存配置说明.md

460 lines
9.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Nginx缓存配置说明
## 📝 配置概述
**更新时间**: 2025-12-23
**服务器**: 公网服务器 156.225.27.52:50232
**配置文件**: /www/server/panel/vhost/nginx/qd.yydd.ai.conf
**状态**: ✅ 已禁用所有缓存(开发模式)
---
## 🚫 当前缓存设置
### 已禁用的缓存功能
```nginx
location / {
# 反向代理配置
proxy_pass http://127.0.0.1:5246;
# 🚫 禁用缓存 - 开发模式
proxy_buffering off; # 禁用代理缓冲
proxy_cache off; # 禁用代理缓存
proxy_no_cache 1; # 不缓存响应
proxy_cache_bypass 1; # 绕过缓存检查
# 添加缓存控制头
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
add_header Pragma "no-cache";
add_header Expires "0";
}
```
---
## 📊 缓存控制说明
### Nginx层面缓存控制
| 指令 | 作用 | 值 |
|------|------|-----|
| `proxy_buffering` | 代理缓冲 | off(禁用) |
| `proxy_cache` | 代理缓存 | off(禁用) |
| `proxy_no_cache` | 不缓存响应 | 1(启用) |
| `proxy_cache_bypass` | 绕过缓存 | 1(启用) |
### HTTP响应头
| 响应头 | 作用 | 值 |
|--------|------|-----|
| `Cache-Control` | 缓存策略 | no-store, no-cache, must-revalidate |
| `Pragma` | HTTP/1.0兼容 | no-cache |
| `Expires` | 过期时间 | 0(立即过期) |
---
## 🎯 效果说明
### 对开发的好处
1. **✅ 实时更新**
- 前端代码更新立即生效
- 无需手动清除浏览器缓存
- API响应不会被缓存
2. **✅ 调试便利**
- 每次请求都是最新内容
- 便于测试新功能
- 避免缓存导致的问题
3. **✅ 快速迭代**
- 修改代码后立即看到效果
- 减少"看不到更新"的困惑
- 提高开发效率
---
## 🔄 切换到生产模式
### 当系统正式上线后
**建议**:启用缓存以提高性能和减少服务器负载
```nginx
location / {
proxy_pass http://127.0.0.1:5246;
# 启用缓存(生产模式)
proxy_buffering on;
# 静态资源缓存(仅示例,根据实际调整)
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
proxy_pass http://127.0.0.1:5246;
proxy_cache my_cache;
proxy_cache_valid 200 30d;
proxy_cache_valid 404 1m;
add_header Cache-Control "public, max-age=2592000";
}
# API响应不缓存
location /api/ {
proxy_pass http://127.0.0.1:5246;
proxy_buffering off;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}
}
```
---
## 📝 切换步骤
### 从开发模式切换到生产模式
```bash
# 1. SSH登录公网服务器
ssh -p 50232 root@156.225.27.52
# 2. 编辑Nginx配置
vi /www/server/panel/vhost/nginx/qd.yydd.ai.conf
# 3. 修改缓存配置(根据需要)
# 移除或注释 proxy_buffering off 等指令
# 添加适当的缓存配置
# 4. 测试配置
nginx -t
# 5. 重载Nginx
nginx -s reload
```
---
## 🧪 测试缓存配置
### 验证缓存已禁用
```bash
# 方式1:查看HTTP响应头
curl -I https://qd.yydd.ai
# 应该看到:
# Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0
# Pragma: no-cache
# Expires: 0
# 方式2:在浏览器开发者工具中
# Network → Headers → Response Headers
# 检查 Cache-Control 等头部
```
### 测试更新是否实时
1. **前端更新测试**
```bash
# 修改前端代码
cd /Users/lanmu/Desktop/qiangdanxitong/frontend
# 编辑任意文件
# 构建并上传
pnpm build
scp -P 5204 -r dist/* root@156.225.27.52:/opt/designhub/frontend/
# 立即刷新浏览器(Ctrl+R,无需Ctrl+Shift+R强制刷新)
# 应该看到更新
```
2. **API更新测试**
```bash
# 修改后端代码
# 重启后端服务
ssh -p 5204 root@156.225.27.52
systemctl restart designhub-backend
# 立即在浏览器中调用API
# 应该返回最新结果
```
---
## ⚠️ 注意事项
### 开发模式的影响
**优点**:
- ✅ 开发调试方便
- ✅ 代码更新实时生效
- ✅ 避免缓存问题
**缺点**:
- ⚠️ 服务器负载略高(每次请求都处理)
- ⚠️ 用户体验略差(无缓存加速)
- ⚠️ 带宽消耗略大
**建议**:
- 开发阶段:使用当前配置(无缓存)
- 生产阶段:启用适当缓存(性能优化)
---
## 🎯 完整配置文件
### 当前配置(开发模式)
```nginx
# /www/server/panel/vhost/nginx/qd.yydd.ai.conf
# HTTP自动跳转HTTPS
server {
listen 80;
server_name qd.yydd.ai;
location /.well-known/acme-challenge/ {
root /www/wwwroot/qd.yydd.ai;
}
location / {
return 301 https://$server_name$request_uri;
}
}
# HTTPS配置
server {
listen 443 ssl http2;
server_name qd.yydd.ai;
# SSL证书
ssl_certificate /etc/letsencrypt/live/qd.yydd.ai/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/qd.yydd.ai/privkey.pem;
# SSL优化
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
client_max_body_size 100M;
# 反向代理 - 无缓存模式
location / {
proxy_pass http://127.0.0.1:5246;
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;
# WebSocket支持
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 超时设置
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
# 🚫 禁用缓存 - 开发模式
proxy_buffering off;
proxy_cache off;
proxy_no_cache 1;
proxy_cache_bypass 1;
# 添加缓存控制头
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
add_header Pragma "no-cache";
add_header Expires "0";
}
access_log /www/wwwlogs/qd.yydd.ai.https.access.log;
error_log /www/wwwlogs/qd.yydd.ai.https.error.log;
}
```
---
## 🔧 常用命令
### 修改配置后重载
```bash
# SSH登录
ssh -p 50232 root@156.225.27.52
# 编辑配置
vi /www/server/panel/vhost/nginx/qd.yydd.ai.conf
# 测试配置
nginx -t
# 重载Nginx(不中断服务)
nginx -s reload
```
### 查看配置
```bash
# 查看当前配置
ssh -p 50232 root@156.225.27.52
cat /www/server/panel/vhost/nginx/qd.yydd.ai.conf
```
### 验证缓存状态
```bash
# 方式1:curl查看响应头
curl -I https://qd.yydd.ai
# 方式2:详细查看
curl -v https://qd.yydd.ai 2>&1 | grep -i cache
```
---
## 📊 浏览器缓存说明
### 浏览器本地缓存
**注意**: 即使Nginx禁用了缓存,浏览器仍可能有自己的缓存
**开发时的刷新方式**:
| 刷新方式 | 快捷键 | 效果 |
|----------|--------|------|
| 普通刷新 | F5 或 Ctrl+R | 可能使用缓存 |
| 硬刷新 | Ctrl+Shift+R | 强制不使用缓存 |
| 清空缓存 | DevTools → Network → Disable cache | 开发时建议勾选 |
**建议**:
- 开发时打开浏览器开发者工具(F12)
- 勾选 Network → Disable cache
- 这样可以完全禁用浏览器缓存
---
## 🚀 性能对比
### 开发模式 vs 生产模式
| 指标 | 开发模式(无缓存) | 生产模式(有缓存) |
|------|-------------------|-------------------|
| 首次加载 | 正常 | 正常 |
| 重复访问 | 每次都请求服务器 | 使用缓存,快速 |
| 服务器负载 | 较高 | 较低 |
| 带宽消耗 | 较高 | 较低 |
| 开发便利性 | ⭐⭐⭐⭐⭐ | ⭐⭐ |
| 用户体验 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
---
## 💡 最佳实践
### 开发阶段(当前)
✅ **已实施**:
- Nginx层面禁用所有缓存
- 添加 no-cache 响应头
- 便于快速迭代开发
### 测试阶段(即将上线)
建议:
- 保持当前配置
- 测试功能完整性
- 验证无缓存相关bug
### 生产阶段(正式上线后)
建议:
- 启用静态资源缓存(JS/CSS/图片)
- API响应保持不缓存
- 设置合理的缓存时间
- 使用CDN加速
---
## 🔄 快速切换配置
### 配置模板
**开发模式配置**(当前):
```nginx
# 完全禁用缓存
proxy_buffering off;
proxy_cache off;
proxy_no_cache 1;
proxy_cache_bypass 1;
add_header Cache-Control "no-store, no-cache, must-revalidate";
```
**生产模式配置**(未来):
```nginx
# 启用适度缓存
proxy_buffering on;
# 静态资源缓存7天
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
add_header Cache-Control "public, max-age=604800";
}
# API不缓存
location /api/ {
add_header Cache-Control "no-cache";
}
```
---
## 📞 技术支持
### 修改缓存配置
如需修改缓存配置,请:
1. SSH登录公网服务器
2. 编辑 `/www/server/panel/vhost/nginx/qd.yydd.ai.conf`
3. 测试配置:`nginx -t`
4. 重载Nginx:`nginx -s reload`
### 验证配置生效
```bash
# 查看HTTP响应头
curl -I https://qd.yydd.ai | grep -i cache
# 应该看到当前设置的缓存策略
```
---
## 📝 更新记录
| 日期 | 操作 | 说明 |
|------|------|------|
| 2025-12-23 | 禁用缓存 | 开发模式,便于测试 |
| 待定 | 启用缓存 | 生产模式,性能优化 |
---
## 🎯 总结
✅ **当前状态**: 所有缓存已禁用
✅ **适用场景**: 开发测试阶段
✅ **效果**: 代码更新立即生效
✅ **访问地址**: https://qd.yydd.ai
**开发体验**:
- 修改前端代码 → 构建上传 → 普通刷新(F5)即可看到更新
- 修改后端代码 → 重启服务 → 立即生效
- 无需清除浏览器缓存或强制刷新
🎉 **现在可以愉快地开发测试了!**
---
**创建时间**: 2025-12-23
**配置状态**: ✅ 已生效
**验证方式**: 访问 https://qd.yydd.ai 并查看响应头