将附件存储改为本地minio

7/18/2024 博客

本来是在用免费图床smms (opens new window)的,但是今天发现之前上传的一些图片都404了,不知道怎么被删除了,而且我还没有写alt导致我也不知道图片是干啥截出来的。所以准备换个存储方式。 思来想去准备使用minio来做存储,将图片存到本地。

好处有以下几点

  1. 不用担心图片被平台删除
  2. 最近一直在用minio,比较熟悉
  3. 通过docker部署很方便
  4. 服务器有多余空间可以存储附件

所以准备自己搞一个minio来做文件存储服务 直接用docker创建即可

docker run -itd -p 127.0.0.1:9000:9000 -p 127.0.0.1:9001:9001 --name minio -e MINIO_ROOT_USER="minioadmin" -e MINIO_ROOT_PASSWORD="minioadmin" -v /data/minio:/bitnami/minio/data -v /data/minio/config:/root/.minio -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro bitnami/minio:2024.7.13
1

这里的127.0.0.1:是禁止外网访问,然后通过nginx进行代理 之后创建好容器后,需要给/data/minio权限

chmod 777 /data/minio
1

然后修改nginx,我这里是将9000端口和9001端口分别做了二级域名映射

# 9001端口
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name xxxx.com;

    ssl_certificate cert/xxxx.com.pem;
    ssl_certificate_key cert/xxxx.com.key;
    ssl_ciphers xxxxxxxxxxxxxxxxxxxx;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_pass http://127.0.0.1:9001;
        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 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        send_timeout 300;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header Access-Control-Expose-Headers 'Content-Length,Content-Range';
    }
    location /ws/ {
        proxy_pass http://127.0.0.1:9001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        send_timeout 300;

        # 设置CORS头部
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header Access-Control-Expose-Headers 'Content-Length,Content-Range';
    }
}
# 9000端口
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name oss.xxxx.com;

    ssl_certificate cert/xxxx.com.pem;
    ssl_certificate_key cert/xxxx.com.key;
    ssl_ciphers xxxxxxxxxxx;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    # 代理API请求到9000端口
    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_cache_valid 200 24h;
        proxy_cache_valid 404 1m;
        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 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        send_timeout 300;

        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header Access-Control-Expose-Headers 'Content-Length,Content-Range';
        add_header X-Cache-Status $upstream_cache_status;
    }
}
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

之后重启nginx,再修改cloudflare添加解析记录即可使用。