nginx cache + imageproxy+minio 支持多级cache的图片缩放处理服务
以前有写过简单的基于imageproxy以及minio 的图片缩放服务,以下是一个集成nginx cache 增强imageproxy 的多级cache 能力
参考图
简单说明:
iamgeproxy可以提供基于s3(minio)、内存、本地磁盘的cache 能力,同时我们的访问入口基于nginx,同时基于nginx 的proxy cache 能力可以提供
多级的cache 处理
环境准备
- docker-compose 文件
version: "3"
services:
pdf-proxy:
image: openresty/openresty:alpine
volumes:
- ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf
- ./cache:/opt/nginx/data/cache
ports:
- "80:80"
minio:
image: minio/minio
command: server /data
volumes:
- "./data:/data"
ports:
- "9000:9000"
environment:
- "MINIO_ACCESS_KEY=minio"
- "MINIO_SECRET_KEY=minio123"
imageproxy:
image: willnorris/imageproxy
command: -addr 0.0.0.0:8080 -cache s3://us-east-1/demo/images?endpoint=minio:9000&disableSSL=1&s3ForcePathStyle=1 -userAgent "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5"
environment:
- "AWS_ACCESS_KEY_ID=minio"
- "AWS_SECRET_KEY=minio123"
ports:
- "8080:8080"
- nginx 配置
主要提供nginx 的rewrite 以及cache,rewrite 是提高imageproxy 请求路径的灵活性,cache 是加速
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
proxy_cache_path /opt/nginx/data/cache keys_zone=wximage:300m;
real_ip_header X-Forwarded-For;
resolver 114.114.114.114 ipv6=off;
real_ip_recursive on;
server {
listen 80;
server_name localhost;
charset utf-8;
default_type text/html;
location /wximage/ {
proxy_redirect off;
rewrite ^/wximage(.*) $1 break;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
proxy_set_header Host $proxy_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_body_buffer_size 10M;
client_max_body_size 10G;
set $agent "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5";
proxy_buffers 1024 4k;
proxy_read_timeout 300;
proxy_connect_timeout 80;
proxy_set_header User-Agent $agent;
proxy_pass http://imageproxy:8080;
proxy_cache wximage;
proxy_cache_key $scheme$proxy_host$uri$is_args$args;
proxy_cache_valid 200 304 302 30d;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- 运行说明
首先启动monio创建相关的bucket,登录browser就可以了 - proxy 百度图片的例子
效果
minio cache
nginx cache
说明
通过imageproxy+minio 以及nginx 的cache 的好处很明显,假如我们的网络异常了,但是我们的图片服务依然还是可以使用的(因为有cache,而且是多级cache)