静态资源服务器&动静分离
背景:让Nginx直接处理图片、css、js等静态文件,减轻后端压力
⭕ 推荐的存储目录
txt
.data
├── file # 存储文件,比如PDF、Word、Excel等
│ ├── 2023-11
│ └── 2023-12
├── image # 存储图片,png、jpeg、jpg、gif等
│ ├── 2023-11
│ └── 2023-12
└── video # 存储视频文件 mp4等
├── 2023-11
└── 2023-12⭕ nginx.conf
txt
server {
listen 80;
server_name www.yourdomain.com;
# 静态资源路径:假设图片存在/data/images/,JS/CSS在/data/static/
location /static/ {
root /data/; # 根路径,实际文件路径是/data/static/...
autoindex off; # 禁止列出目录(安全考虑)
expires 30d; # 浏览器缓存30天,减少重复请求
gzip on; # 开启压缩,减小文件传输大小
gzip_types text/css application/javascript image/png; # 压缩类型
}
location /images/ {
root /data/;
# 防盗链:防止其他网站盗用你的图片
valid_referers none blocked www.yourdomain.com;
if ($invalid_referer) {
return 403; # 非法引用返回403错误
}
}
# 动态请求(如登录接口)还是转发给后端
location /api/ {
proxy_pass http://backend_servers/;
}
}
biubiu