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

前言

不推荐使用,Windwos环境下载暂时没有找到添加该模块的方式,只支持Linux环境。

我们可能服务器上使用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

查看当前的版本及模块

1
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的路径

1
which nginx

结果如下

/usr/sbin/nginx

备份配置文件

1
cp -r /etc/nginx /etc/nginx.bak

官网下载对应版本的源码

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

下载

1
2
3
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的支持

安装

1
2
3
yum install gd-devel
yum install libxslt-devel
yum -y install perl-devel perl-ExtUtils-Embed

编译

1
cd nginx-1.12.2

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

1
./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

1
make

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

复制

1
2
cp objs/ngx_http_image_filter_module.so /usr/lib64/nginx/modules/
cp -rfp objs/nginx /usr/sbin/

查看模块是否加载

查看

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

1
include /usr/share/nginx/modules/*.conf;

打开对应目录

1
cd /usr/share/nginx/modules/

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

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

1
load_module "modules/ngx_http_image_filter_module.so";

配置

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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://www.psvmc.cn/article/2020-02-11-nginx-config.html

错误示例

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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/;
}

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