nginx包含了一個ngx_http_image_filter_module 模塊,我們可以方便的進行圖片的縮略圖,平時一些簡單的功能
已經夠用了
環境准備
為了簡單使用docker-compose 運行,因為openresty 已經默認集成了這個模塊,就不用安裝了
- docker-compose 文件
version: "3"
services:
nginx:
image: openresty/openresty:alpine-fat
volumes:
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
- "./images/:/opt/img/"
ports:
- "8080:8080"
- nginx conf
主要是加載模塊以及配置模塊參數,為了簡單測試,直接寫了固定參數
load_module "modules/ngx_http_image_filter_module.so";
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
lua_code_cache off;
lua_need_request_body on;
gzip on;
resolver 127.0.0.11 ipv6=off;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
gzip_min_length 2k;
gzip_buffers 4 16k;
gzip_comp_level 4;
gzip_types text/plain text/css image/png application/javascript image/jpeg image/gif;
server {
listen 8080;
server_name app;
charset utf-8;
default_type text/html;
location / {
default_type text/plain;
index index.html index.htm;
}
location = /favicon.ico {
root /opt/app/static;
}
location /img/ {
root /opt;
image_filter resize 600 400;
error_page 415 = /empty;
image_filter_jpeg_quality 95;
image_filter_buffer 20M;
}
location = /empty {
empty_gif;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
測試
為了簡單,使用數據卷掛載了一個簡單的圖片目錄
- 啟動
docker-compose up -d
- 效果
http://localhost:8080/img/index.png
原圖信息
縮略圖信息
說明
測試使用了很簡單的配置,實際上我們可以用這個做好多強大的功能,類似的可以集成thumbor 實現更強大的功能
參考資料
https://www.cnblogs.com/rongfengliang/p/8650784.html
https://github.com/rongfengliang/openresty-image-filter-demo
https://nginx.org/en/docs/http/ngx_http_image_filter_module.html
https://github.com/rongfengliang/mino-thumbor-openresty