💾 本地文件存储配置
🔄 上传流程
⚙️ 配置说明
基础配置示例
yaml
files:
- # 业务标识配置
business-type: demo # 业务类型标识
file-type: local # 存储类型:本地存储
# 存储路径配置
root-path: /srv/file/data # 文件根目录
http-access-path: https://files.example.com # Nginx访问域名
# 上传限制
allowed-upload-suffix: jpg,png,gif,bmp,pdf,doc # 允许的文件类型
max-size: 1MIB # 单文件大小限制
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
yaml
files:
- # 业务标识配置
business-type: demo # 业务类型标识
file-type: local # 存储类型:本地存储
# 存储路径配置
root-path: /srv/file/data # 文件根目录
http-access-path: http://api.example.com/download/demo # 接口访问地址
# 上传限制
allowed-upload-suffix: jpg,png,gif,bmp,pdf,doc # 允许的文件类型
max-size: 1MIB # 单文件大小限制
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
参数说明
参数名 | 必填 | 说明 | 示例值 |
---|---|---|---|
business-type | 是 | 业务类型标识 | demo |
file-type | 是 | 存储类型(固定为local) | local |
root-path | 是 | 文件存储根目录 | /srv/file/data |
http-access-path | 是 | 文件访问地址 | Nginx方式:https://files.example.com 接口方式: http://api.example.com/download/demo |
allowed-upload-suffix | 是 | 允许上传的文件后缀 | jpg,png,pdf |
max-size | 是 | 文件大小限制 | 1MIB |
下载配置
1. Nginx下载配置
适用场景
- 适合公开文件下载
- 需要CDN加速
- 追求最佳下载性能
- 创建存储目录
bash
# 创建根目录
mkdir -p /srv/file/data
# 设置目录权限
chmod 755 /srv/file/data
chown -R file-server:file-server /srv/file/data
1
2
3
4
5
6
2
3
4
5
6
- 配置 Nginx
nginx
server {
listen 80;
server_name files.example.com;
# 开启gzip压缩
gzip on;
gzip_types text/plain application/xml application/json application/pdf;
# 配置跨域
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, OPTIONS';
location / {
root /srv/file/data;
autoindex off;
# 缓存配置
add_header Cache-Control "public, max-age=31536000";
# 防盗链配置
valid_referers none blocked server_names *.example.com;
if ($invalid_referer) {
return 403;
}
# 大文件优化
client_max_body_size 1024m;
client_body_buffer_size 1024k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2. 接口下载配置
- 配置访问地址
yaml
http-access-path: http://api.example.com/download/demo
1
- 接口访问示例
http
# 文件路径下载
GET http://api.example.com/download/demo/2024/03/20/example.pdf
1
2
2
2. 安全建议
安全提示
- 避免将存储目录放在 Web 根目录下
- 定期清理临时文件
- 配置目录访问权限
- 开启防盗链功能
3. 性能优化
目录结构优化
- 使用日期目录分层存储
- 控制单目录文件数量
- 定期归档历史文件
访问优化
- 配置 CDN 加速
- 启用 Nginx 缓存
- 开启 GZIP 压缩
❗ 常见问题
上传失败
- 检查目录权限设置
- 验证磁盘空间是否充足
- 确认文件大小是否超限
访问路径问题
- 确保 Nginx 配置正确
- 检查文件权限设置
- 验证域名解析是否正常
性能问题
- 检查磁盘 I/O 性能
- 监控目录文件数量
- 评估存储容量使用情况
注意事项
- 建议使用 SSD 存储以提升性能
- 定期备份重要文件
- 监控磁盘使用率
- 配置日志轮转策略