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   荣锋亮  阅读(2076)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2015-04-15 TaffyDB:开源JavaScript数据库
2014-04-15 html 滚动固定显示js脚本
2014-04-15 asp.net 导出excel的一种方法
2014-04-15 微软通过.NET Native为Windows Store应用提速
2014-04-15 .NET基金会成立

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示