使用openresty && minio && thumbor 构建稳定高效的图片服务器
备注:
minio 是一个开源的s3 协议兼容的分布式存储,openresty nginx+lua 高性能可扩展的nginx 衍生版,thumbor 基于python
的图片处理服务器,支持图片的裁剪,识别。。。。
代码为使用docker 构建的一个一体化的参考代码,实际使用中,大家可以基于自己的业务场景修改,对于简单的场景,直接
使用也是可以的。
1. 项目结构
├── Dockerfile-nginx
├── Dockerfile-thumbor
├── README.md
├── docker-compose.yml
└── nginx.conf
2. 代码说明
备注:主要是对于docker-compose.yml的说明
a. docker-compose.yml
version: '3'
services:
nginx:
image: mynginx-minio
build:
context: .
dockerfile: Dockerfile-nginx
ports:
- 8087:80
depends_on:
- thumbor
thumbor:
image: mythumbor
build:
context: .
dockerfile: Dockerfile-thumbor
minio:
image: minio/minio
volumes:
- ./data:/data
- ./config:/root/.minio
ports:
- 9091:9000
command: server /data
b. Dockerfile-nginx 很简单,就是进行反向代理的配置
FROM openresty/openresty:alpine
LABEL name="rongfengliang"
LABEL version="v1.0.0"
ENV name=rongfengliang
ENV version=v1.0.0
COPY nginx.conf usr/local/openresty/nginx/conf/
EXPOSE 80
EXPOSE 443
EXPOSE 88
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
resolver 127.0.0.11; # dns 配置
gzip on;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
server {
listen 80;
server_name localhost;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
charset utf-8;
location /mydemo{
root html;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 1024 4k;
proxy_read_timeout 300;
proxy_pass http://minio:9000;
}
location /{
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 1024 4k;
proxy_read_timeout 300;
proxy_pass http://thumbor/unsafe/300x200/http://minio:9000$request_uri; // 图片处理为300*200 的大小
}
location /alert {
default_type text/html;
content_by_lua_block{
ngx.say([[<script>alert("error")</script>]])
}
}
location /ip {
default_type text/html;
content_by_lua_block{
ngx.say(ngx.var.remote_addr)
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 88;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
c. Dockerfile-thumbor thumbor 的镜像
FROM alpine:3.6
EXPOSE 80
MAINTAINER dalong@qq.com
CMD ["thumbor", "-l", "info", "-p", "80"]
ENV THUMBOR_VERSION 6.3.2
RUN apk add --no-cache \
python \
libcurl \
ca-certificates \
libjpeg-turbo \
openjpeg \
libwebp \
tiff \
gcc \
py-pip \
curl-dev \
python-dev \
musl-dev \
libjpeg-turbo-dev \
openjpeg-dev \
libwebp-dev \
tiff-dev \
libressl-dev \
&& export LIBRARY_PATH=/lib \
&& pip install thumbor==$THUMBOR_VERSION \
&& apk del gcc *-dev \
&& rm -rf /var/cache/apk/* \
&& rm -rf /root/.cache
3. 构建
docker-compose build
4. 运行
docker-compose up -d
5. 准备运行数据(minio 文件,以及policy 配置)
a. 获取minio key
dokcer-compose logs minio
b. 输入minio 登陆需要的key
c. 创建bucket
d. 设置bucket policy * read&&write
6. 参考界面
7. 参考资料
http://thumbor.org/
https://www.minio.io/
https://github.com/rongfengliang/mino-thumbor-openresty