nginx ngx_http_image_filter_module 简单试用

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

posted on 2019-04-15 12:37  荣锋亮  阅读(2064)  评论(0编辑  收藏  举报

导航