前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx添加模块http_image_filter_module实现图片缩略图功能

Nginx添加模块http_image_filter_module实现图片缩略图功能

作者头像
码客说
发布2020-06-15 10:34:24
3.6K0
发布2020-06-15 10:34:24
举报
文章被收录于专栏:码客码客

前言

我们可能服务器上使用yum安装的Nginx,这时要新增模块的思路大致如下

  1. 官网下载相同版本的nginx源码包
  2. 编译安装并指定需要的模块(第三方模块需要单独下载对应的包)
  3. 注意只编译make,不要编译安装make install. 编译完成会在objs目录下生成可执行文件nginx
  4. 复制nginx 可执行文件cp objs/nginx /usr/sbin/
  5. 复制模块objs/ngx_http_image_filter_module.so
  6. 配置模块

具体步骤

查看原Nginx

查看当前的版本及模块

代码语言:javascript
复制
nginx -V

会看到如下

nginx version: nginx/1.12.2 configure arguments: –prefix=/usr/share/nginx –sbin-path=/usr/sbin/nginx –modules-path=/usr/lib64/nginx/modules –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –http-client-body-temp-path=/var/lib/nginx/tmp/client_body –http-proxy-temp-path=/var/lib/nginx/tmp/proxy –http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi –http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi –http-scgi-temp-path=/var/lib/nginx/tmp/scgi –pid-path=/run/nginx.pid –lock-path=/run/lock/subsys/nginx –user=nginx –group=nginx –with-file-aio –with-ipv6 –with-http_auth_request_module –with-http_ssl_module –with-http_v2_module –with-http_realip_module –with-http_addition_module –with-http_xslt_module=dynamic –with-http_image_filter_module=dynamic –with-http_geoip_module=dynamic –with-http_sub_module –with-http_dav_module –with-http_flv_module –with-http_mp4_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_random_index_module –with-http_secure_link_module –with-http_degradation_module –with-http_slice_module –with-http_stub_status_module –with-http_perl_module=dynamic –with-mail=dynamic –with-mail_ssl_module –with-pcre –with-pcre-jit –with-stream=dynamic –with-stream_ssl_module –with-google_perftools_module –with-debug –with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic’ –with-ld-opt=’-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E’

查找

–with-http_image_filter_module=dynamic

我这里就已经包含该模块了如果没有则继续看下文

编译安装

如果本地已有该模块 这一步则跳过

查看Nginx的路径

代码语言:javascript
复制
which nginx

结果如下

/usr/sbin/nginx

备份配置文件

代码语言:javascript
复制
cp -r /etc/nginx /etc/nginx.bak

官网下载对应版本的源码

比如我的是1.12.2 下载地址http://nginx.org/download/nginx-1.12.2.tar.gz

下载

代码语言:javascript
复制
cd /usr/local
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -xzvf nginx-1.12.2.tar.gz

HttpImageFilterModule模块需要依赖gd-devel的支持

安装

代码语言:javascript
复制
yum install gd-devel
yum install libxslt-devel
yum -y install perl-devel perl-ExtUtils-Embed

编译

代码语言:javascript
复制
cd nginx-1.12.2

在上面的命令里面加上 --with-http_image_filter_module=dynamic 开始执行编译,编译的时候依赖的模块没有安装导致错误,只需安装对应的模块即可。

代码语言:javascript
复制
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug

编译

执行 make,切记不需要执行make install,此处只需要得到nginx重新编译的可执行文件,并不需要重新安装替换掉原来的nginx

代码语言:javascript
复制
make

执行完成会在 objs 目录下生成对应的可执行文件nginx

复制

代码语言:javascript
复制
cp objs/ngx_http_image_filter_module.so /usr/lib64/nginx/modules/
cp -rfp objs/nginx /usr/sbin/

查看模块是否加载

查看

/etc/nginx/nginx.conf中如果有

代码语言:javascript
复制
include /usr/share/nginx/modules/*.conf;

打开对应目录

代码语言:javascript
复制
cd /usr/share/nginx/modules/

如果包含mod-http-image-filter.conf就不用再加载了。

如果没有/etc/nginx/nginx.conf中加载模块

代码语言:javascript
复制
load_module "modules/ngx_http_image_filter_module.so";

配置

/etc/nginx/conf.d/mysite.conf 配置图片处理

代码语言:javascript
复制
location ~* /(.+)\.(jpg|jpeg|gif|png)!(\d+)x(\d+)$ {
        set $w $3;
        set $h $4;
        image_filter resize  $w $h;
        image_filter_buffer  10M;
        image_filter_jpeg_quality 75;
        try_files /$1.$2  /notfound.jpg;
        expires 1d;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        root  /data/wwwjarapi/8908schoolfiletest/;  
    }

location ~* /static {
		add_header 'Access-Control-Allow-Origin' '*';
		add_header 'Access-Control-Allow-Credentials' 'true';
		root  /data/wwwjarapi/8908schoolfiletest/;  
    }

如果原图的地址为

www.psvmc.cm/abc/1.jpg

那么缩放后的图片地址为

www.psvmc.cm/abc/1.jpg!200x200

Nginx配置生效的优先级

https://cloud.tencent.com/developer/article/1625630

错误示例

比如我按照如下配置就不行

代码语言:javascript
复制
location ~* /(.+)\.(jpg|jpeg|gif|png)!(\d+)x(\d+)$ {
        set $w $3;
        set $h $4;
        image_filter resize  $w $h;
        image_filter_buffer  10M;
        image_filter_jpeg_quality 75;
        try_files /$1.$2  /notfound.jpg;
        expires 1d;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        root  /data/wwwjarapi/8908schoolfiletest/;  
    }

location ^~ /static {
		add_header 'Access-Control-Allow-Origin' '*';
		add_header 'Access-Control-Allow-Credentials' 'true';
		root  /data/wwwjarapi/8908schoolfiletest/;  
    }

因为后面的配置优先级高 导致第一个配置不生效

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 具体步骤
    • 查看原Nginx
      • 编译安装
        • 配置
        相关产品与服务
        图片处理
        图片处理(Image Processing,IP)是由腾讯云数据万象提供的丰富的图片处理服务,广泛应用于腾讯内部各产品。支持对腾讯云对象存储 COS 或第三方源的图片进行处理,提供基础处理能力(图片裁剪、转格式、缩放、打水印等)、图片瘦身能力(Guetzli 压缩、AVIF 转码压缩)、盲水印版权保护能力,同时支持先进的图像 AI 功能(图像增强、图像标签、图像评分、图像修复、商品抠图等),满足多种业务场景下的图片处理需求。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档