现在这个网站的图片都放在 hugo 生成网站的静态文件夹中,每次复制文件都会随着重新复制一次,既不方便管理,也无法在 markdown 文档中直接预览,因此重新搭建了 minio 作为图床。
minio 安装与配置
- 下载二进制文件
https://dl.min.io/server/minio/release/linux-amd64/minio
; - 修改权限
chmod +x minio
; - 放在合适的位置,比如
mkdir -p /opt/minio && mv minio /opt/minio/minio
; - 建立 minio 所使用的目录,例如
/data/minio
; - 尝试运行
MINIO_ACCESS_KEY=minio MINIO_SECRET_KEY=minio123 nohup /opt/minio/minio server --address "${MINIO_HOST}:${MINIO_PORT}" /opt/minio-data
。
可以输出 minio 和 web ui 监听路径即可。
nginx 代理设置
预期能够通过 https + 域名 的方式访问 minio 文件服务和 webui,使用 nginx 代理,相关的配置部分如下,参考自 Configure NGINX Proxy for MinIO Server — MinIO Object Storage for Linux,记得替换自己的 ssl 证书。
server {
listen 443 ssl;
server_name cdn.shadowc.ltd;
ssl_certificate /path/to/your/cert.crt;
ssl_certificate_key /path/to/you/key.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header Host $http_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;
# Default is HTTP/1, keepalive is only enabled in HTTP/1.1
proxy_http_version 1.1;
proxy_set_header Connection "";
chunked_transfer_encoding off;
proxy_pass http://localhost:9000;
}
location /minio/ui/ {
rewrite ^/minio/ui/(.*) /$1 break;
proxy_set_header Host $http_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_set_header X-NginX-Proxy true;
# This is necessary to pass the correct IP to be hashed
real_ip_header X-Real-IP;
proxy_connect_timeout 300;
# To support websockets in MinIO versions released after January 2023
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
chunked_transfer_encoding off;
proxy_pass http://localhost:9001;
}
}
同时,minio 的启动命令也要修改,需要指定 MINIO_SERVER_URL
和 MINIO_BROWSER_REDIRECT_URL
,前者是 minio 提供的对象存储服务,后者是 Web UI。完整的命令形如:
MINIO_SERVER_URL=https://cdn.example.com \
MINIO_BROWSER_REDIRECT_URL=https://cdn.example.com/minio/ui/ \
MINIO_ROOT_USER=[you-user] \
MINIO_ROOT_PASSWORD=[you-key] \
/opt/minio/minio server \
--address 'localhost:9000' \
--console-address 'localhost:9001' \
/data/minio/
将 minio 安装为系统服务
按照上一节的命令启动 minio,已经可以提供服务了,但是为了服务的稳定性,将 minio 注册为 service,并且指定自动重启。
- 建立
/etc/systemd/system/minio.service
,内容为:
[Uint]
Description=Minio Service
After=network.target
[Service]
ExecStart=/opt/minio/minio server --address 'localhost:9000' --console-address 'localhost:9001' /data/minio/
Environment="MINIO_SERVER_URL=https://cdn.example.com"
Environment="MINIO_BROWSER_REDIRECT_URL=https://cdn.example.com/minio/ui/"
Environment="MINIO_ROOT_USER=your-user"
Environment="MINIO_ROOT_PASSWORD=your-password"
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
systemctl enable minio
;systemctl start minio
后续变动
- 使用启动命令中的用户名和密码登录 web ui,建立所需的 bucket(例如 images),然后设置匿名访问权限为 ReadOnly,就可以当作图床来用了。
- 自定义权限的格式,例如仅支持查看和上传 images 这个 bucket 的权限:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::images/*"
]
}
]
}
PS: 原本不用 docker 而自己搭建的原因是启动容器就会占用 200 MB 内存,但是现在这种方式占用的内存也并没有小到哪里去,摊手.jpg。