minio + imgproxy 实现文件存储
1、config.env
# MINIO_ROOT_USER and MINIO_ROOT_PASSWORD sets the root account for the MinIO server.
# This user has unrestricted permissions to perform S3 and administrative API operations on any resource in the deployment.
# Omit to use the default values 'minioadmin:minioadmin'.
# MinIO recommends setting non-default values as a best practice, regardless of environment
MINIO_ROOT_USER=myminioadmin
MINIO_ROOT_PASSWORD=minio-secret-key-change-me
# MINIO_VOLUMES sets the storage volume or path to use for the MinIO server.
MINIO_VOLUMES="/mnt/data"
# MINIO_OPTS sets any additional commandline options to pass to the MinIO server.
# 例如, `--console-address :9001` sets the MinIO Console listen port
MINIO_OPTS="--console-address :9001"
# MINIO_SERVER_URL sets the hostname of the local machine for use with the MinIO Server
# MinIO assumes your network control plane can correctly resolve this hostname to the local machine
# Uncomment the following line and replace the value with the correct hostname for the local machine and port for the MinIO server (9000 by default).
#MINIO_SERVER_URL="http://minio.example.net:9000"2、docker-compose.yml
minio-alias1:
container_name: "minio-alias1"
hostname: "minio-alias1"
image: "minio/minio"
command: [
"minio",
"server",
"--console-address",
":9001"
]
ports:
- "9000:9000"
- "9001:9001"
environment:
MINIO_CONFIG_ENV_FILE: /etc/config.env
networks:
network-01:
aliases:
- minio-net1
restart: always
volumes:
- "E:/config/minio/config.env:/etc/config.env"
- "E:/data/minio:/mnt/data"
imgproxy-alias1:
container_name: "imgproxy-alias1"
hostname: "imgproxy-alias1"
image: "darthsim/imgproxy"
ports:
- "8080:8080"
environment:
IMGPROXY_BASE_URL: "http://minio-alias1:9000/"
networks:
network-01:
aliases:
- imgproxy-net1
restart: always3、访问测试
# 若配置环境变量 IMGPROXY_BASE_URL,只能访问对应地址的图片,域名可省略
http://localhost:8080/_/rs:auto:300:800/plain/test/001.png
或
http://localhost:8080/_/rs:auto:300:800/plain/http://minio-alias1:9000/test/001.png# 若没有配置环境变量 IMGPROXY_BASE_URL,需输入完整图片地址
http://localhost:8080/_/rs:auto:300:800/plain/https://www.baidu.com/img/001.png
3.1 如果使用签名访问,后面的链接需要使用url编码,例:
http://localhost:8080/_/rs:auto:300:800/plain/test%2F001.png%3FX-Amz-xxx
# Resize
resize:%resizing_type:%width:%height:%enlarge:%extend
rs:%resizing_type:%width:%height:%enlarge:%extend
# This is a meta-option that defines the resizing type, width, height, enlarge, and extend.
# All arguments are optional and can be omitted to use their default values.
# resizing_type Default: fit
fit:调整图像大小,同时保持宽高比以适合给定的大小。
fill:调整图像大小,同时保持纵横比以填充给定大小并裁剪投影部分。
fill-down:与fill相同,但如果调整后的图像小于请求的大小,imgproxy将裁剪结果以保持请求的宽高比。
force:调整图像大小而不保持纵横比。
auto:如果源尺寸和结果尺寸具有相同的方向(纵向或横向),imgproxy将使用fill。否则,它将使用fit。4、可配置nginx转发,配置后访问地址,例:
http://localhost.img/test/001.png
http://localhost.img/test/001.png?x_process=rs:fill:300:500:1:1
# http 内的都需要
http {
# Docker 内置 DNS,供 upstream 在运行时解析域名;valid=30s 表示解析结果缓存 30s,
# 容器重建/IP 变化后最多 30s 生效,无需 reload nginx;
# ipv6=off 避免无意义的 AAAA 查询以及解析失败拖慢请求。
# 127.0.0.11为容器内的 nameserver,可通过 cat /etc/resolv.conf 查看
resolver 127.0.0.11 valid=30s ipv6=off;
resolver_timeout 5s;
# imgproxy 后端集群
upstream imgproxy {
# resolve 依赖共享内存 zone 保存动态解析结果,64k 对少量记录足够
zone imgproxy 64k;
# resolve:按 resolver 动态解析域名,容器 IP 变化时无需 reload
server imgproxy-alias1:8080 resolve;
# 长连接池:复用与 imgproxy 的 TCP 连接,减少握手开销
# 需配合下面 server 段的 proxy_http_version 1.1 与 proxy_set_header Connection "" 才生效
keepalive 32;
# 单条连接最多处理 1000 个请求后关闭,避免长期复用带来的连接状态问题
keepalive_requests 1000;
# 空闲连接保留 60s(应小于上游自身的 keepalive timeout)
keepalive_timeout 60s;
}
# -------------------------------------------------------------------------
# x_process → imgproxy URI 前缀映射(含白名单校验)
# -------------------------------------------------------------------------
# $arg_x_process 自动取自 querystring 中的 x_process 参数;
# 用 map 完成「校验 + 拼前缀」,比在 location 里堆 if 更安全(if 属于 rewrite 模块,易踩坑),
# 且结果可直接被 rewrite 指令引用。
#
# map 的匹配顺序:先精确字符串键,再按书写顺序依次匹配正则,最后落到 default。
# 因此「严格的 rs 语法」必须写在「宽松字符集白名单」之前,否则非法值会被宽松规则放过。
#
# 结果语义:
# /_/.../plain 合法,交给 imgproxy 处理
# "" 非法,location 中据此直接返回 400
map $arg_x_process $imgproxy_lead {
# 未传 x_process:按原图(plain)处理,不做任何处理选项
"" "/_/plain";
# 严格 rs 语法,与 imgproxy 原生的解析规则一致:
# rs:<type>:<width>[:<height>[:<enlarge>[:<extend>]]]
# type auto | fit | fill | force | cover
# width 1-5 位数字,0 表示按比例(0 是合法值,不是缺省)
# height 1-5 位数字,0 表示按比例;可整段省略,省略时由 imgproxy 自动计算
# enlarge 只能是 0 | 1
# extend 只能是 0 | 1
# 合法:rs:auto:300 / rs:auto:300:500 / rs:fit:300:400:1 / rs:fit:300:400:1:0
# 非法:rs:auto1:200(类型拼错)、rs:auto:300:500:600:800(数量/取值超范围)
# 注意:imgproxy 按位置解析,rs:auto:300:1 的 1 是 height 而非 enlarge;
# 要表达“宽度 300、enlarge=1”需写成 rs:auto:300:0:1
"~^rs:(auto|fit|fill|force|cover):[0-9]{1,5}(:[0-9]{1,5}(:[01]){0,2})?$" "/_/$arg_x_process/plain";
# 以 rs: 开头但不符合上面的语法,一律拒绝
# 必须写在下面宽松白名单之前,否则 rs:xxx 会被白名单放行
"~^rs:" "";
# 其它 imgproxy 选项(g:nowe、q:80、bg:ff0000 等),只做字符集校验
# 字符集里没有 / ? & 空格 等字符,天然挡掉了路径穿越与参数注入;
# 长度上限 256 防止超长值污染日志与缓存 key。
# 如需多个选项组合(imgproxy 用 / 分隔,如 rs:fit:300:400/q:80),
# 需自行把 / 加入字符集并逐段校验,当前只支持单个选项
# 已知限制:字符集包含 . 与 -,因此形如 ".." 的值也能通过校验;
# 如需彻底拒绝,可在此行之前插入一条 "~\\.\\." "";
"~^[A-Za-z0-9:._,=+\-]{1,256}$" "/_/$arg_x_process/plain";
# 其余情况(非法字符、超长、Unicode 等)一律判为非法
default "";
}
# -------------------------------------------------------------------------
# 转发时剥离 x_process(imgproxy 不认这个参数,留着只会污染日志与缓存 key)
# -------------------------------------------------------------------------
# 日志里的 $request 仍是原始请求,不受此处影响;只有转发给上游的 args 被清洗。
# 注意顺序:开头 / 结尾 / 中间 / 仅该参数 四种形态,最后 default 原样透传。
# 已知限制:同一请求出现多次 x_process 时只会剔除第一处(正常场景不会这么用)。
map $args $clean_args {
"~^x_process=[^&]*$" ""; # 只有 x_process,清洗后 args 为空
"~^x_process=[^&]*&(.+)$" "$1"; # x_process 在开头
"~^(.+)&x_process=[^&]*$" "$1"; # x_process 在结尾
"~^(.+)&x_process=[^&]*&(.+)$" "$1&$2"; # x_process 在中间
default $args; # 没有该参数,原样保留
}
server {
listen 80;
server_name localhost.img;
# ---- 反向代理通用请求头 ----
# HTTP/1.1 是长连接的前提;Connection "" 清空该头(不发送 close),
# upstream 的 keepalive 连接池才会真正被复用
proxy_http_version 1.1;
# Host 用 $proxy_host(即 imgproxy-alias1:8080),不把外网域名透给后端;
# 若 imgproxy 需要按原始域名做来源校验/签名,应改为 $host
proxy_set_header Host $proxy_host;
proxy_set_header Connection "";
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-Forwarded-Host $host;
# 建连超时:imgproxy 在同一内网,5s 足够,快速失败便于定位问题
proxy_connect_timeout 5s;
proxy_send_timeout 60s;
# imgproxy 处理大图可能偏慢,按需调整
proxy_read_timeout 60s;
# 关闭对上游响应中 Location/Refresh 的重写,保持后端返回原样
proxy_redirect off;
# 图片主路由:/<bucket 或项目前缀>/<对象路径>,至少要求二级路径
# obj_prefix 为命名捕获,当前未使用,可留给日志、缓存 key 或鉴权扩展
# default_type 只作用于本 location 自己生成的响应(400),不影响代理响应
location ~ ^/(?<obj_prefix>[^/]+)/.+$ {
default_type text/plain;
# x_process 非法(非法字符、未知 rs 类型、enlarge/extend 非 0|1、超长等)→ 立刻 400,
# 不把可疑请求透给 imgproxy;if + return 是 rewrite 模块中唯一安全的用法
if ($imgproxy_lead = "") {
return 400 "invalid x_process: $arg_x_process (expected e.g. rs:fit:300:400:1:0)\n";
}
# 剥离 x_process 后再转发;$request 日志仍是原始请求,不受影响
set $args $clean_args;
# 把 URI 改写成 imgproxy 的形态,proxy_pass 保持静态:
# - proxy_pass 不带变量,才能沿用 upstream 的 keepalive 与 zone 动态解析
# - 不触发内部重定向
# - 由 nginx 标准流程做转义(中文/空格正确编码为 %XX)
# 注意:不要换成 $request_uri,会导致 %XX 被二次编码成 %25XX
# 注意:若重写结果里出现 /../,nginx 后续的 URI 解析会做规范化;
# 当前 map 字符集不含 /,仅 ".." 单独出现的极端情况,可在 map 中提前拒绝
# 如果该 server 固定指向 bucket,则加上 /bucket,如:
# rewrite ^ $imgproxy_lead/bucket$uri break;
rewrite ^ $imgproxy_lead$uri break;
proxy_pass http://imgproxy;
}
}
}