Dockerfile 部署 nginx + lua + GraphicsMagick 实现缩略图功能
1.dockerfile 源码
FROM hub.c.163.com/library/alpine:3.6
MAINTAINER peilong <991233068@qq.com>
RUN apk add --no-cache make gcc libc-dev gcc-objc ca-certificates zlib-dev openssh wget libressl2.5-libssl
RUN cd /usr/local/share/ && \
wget http://www.ijg.org/files/jpegsrc.v9c.tar.gz && \
tar zxvf ./jpegsrc.v9c.tar.gz && \
cd ./jpeg-9c && \
./configure && make && make install
RUN cd /usr/local/share/ && \
wget https://nchc.dl.sourceforge.net/project/libpng/libpng16/1.6.37/libpng-1.6.37.tar.gz && \
tar zxvf libpng-1.6.37.tar.gz && \
cd ./libpng-1.6.37 && \
./configure && make && make install
COPY ./GraphicsMagick-1.3.33.tar.gz /usr/local/share/GraphicsMagick-1.3.33.tar.gz
RUN cd /usr/local/share/ && \
tar -zxvf GraphicsMagick-1.3.33.tar.gz && \
cd ./GraphicsMagick-1.3.33 && \
./configure --prefix=/usr/local/GraphicsMagick --enable-shared --without-prel --enable-shared --disable-openmp && \
make && make install
RUN apk add --no-cache nginx-mod-http-lua
# Create folder for PID file
RUN mkdir -p /run/nginx
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]
2.在dockerfile同级目录下执行build命令
docker build -t nginx-lua-graphicsmagick .
3.nginx配置
server {
listen 8888;
server_name location;
error_log logs/images_error.log error;
location /test_lua {
default_type text/html;
content_by_lua_block {
ngx.say("Hello Lua!")
}
}
location ~* (.*\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ {
if (!-f $request_filename) {
set $request_filepath /www/images/$1;
set $width $3;
set $height $4;
set $ext $5;
content_by_lua_file /usr/local/nginx/lua/ImageResizer.lua;
}
# 注意:改配置必须放在 if 之后
root /www/images/;
}
}
4./usr/local/nginx/lua/ImageResizer.lua 代码
local command = "/usr/local/GraphicsMagick/bin/gm convert -auto-orient -strip " .. ngx.var.request_filepath ..
" -resize " .. ngx.var.width .. "x" .. ngx.var.height .. " +profile \"*\" " .. ngx.var.request_filepath .. "_"
.. ngx.var.width .. "x" .. ngx.var.height .. "." .. ngx.var.ext;
os.execute(command);
ngx.exec(ngx.var.request_uri);
5.启动容器
docker run -it -d --name nginx-lua-alpha -p 8888:8888 -v /Users/xxx/web/nginx-lua-config:/etc/nginx/conf.d -v /Users/xxx/web/www:/www -v /Users/xxx/web/nginx-lua-alpine/ImageResizer.lua:/usr/local/nginx/lua/ImageResizer.lua nginx-lua-alpha-magick-jpg-png:0.1
6.访问
原地址:http://127.0.0.1/images.jpg
缩略图地址:http://127.0.0.1/images.jpg_400x300.jpg