【nginx系列】nginx升级到支持http_image_filter_module处理图片

753 阅读2分钟

一、前言

最近在研究nginx在前端中使用最大化,发现了可以很好的处理图片。

二、http_image_filter_module模块

我们先来到软件包的地方看到了configure。

image

我们先看一下这个模块http_image_filter_module,已经是内置模块了,但是需要重新编译一下,添加这个模块。

image

上图知道了nginx在编译时不会自动构建http_image_filter_module和http_v2_module。所以需要重新编译nginx。

注意:下次研究一下动态模块加载

三、加入参数编译

新增加的配置,我们需要重新编译。

./configure --prefix=/usr/local/nginx --with-http_image_filter_module --with-http_v2_module --with-http_ssl_module --with-openssl=/home/soft/openssl-1.1.0f

上面的/usr/local/nginx这个路径是我们编译之后的包路径。

image

发现报错:

image

查找发现:HttpImageFilterModule模块需要依赖gd-devel的支持,可以使用yum或apt-get方便地安装,如果未安装回报“/configure: error: the HTTP image filter module requires the GD library.”错误

yum install gd-devel

或者apt-get install libgd2-xpm libgd2-xpm-dev

执行成功后:

image

配置完成后,运行命令

nake

执行成功之后:

image

这里不要进行make install,否则就是覆盖安装。

四、备份和替换

(1)然后备份原有已安装好的nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_07_24.bak

(2)关闭nginx,然后将刚刚编译好的nginx覆盖掉原有的nginx

关闭nginx

./nginx -s quit

删除nginx文件

image

移动编译好的nginx到原有的nginx

cp ./objs/nginx /usr/local/nginx/sbin/

(3)启动nginx

./nginx

五、图片缩放的nginx配置

location ~* /image/(.+)$ {
    # 图片服务器端存储地址
    alias /home/static/image/$1;  
    # 图片默认宽度
    set $width -;  
    # 图片默认高度
    set $height -;  
    if ($arg_width != "") {
	set $width $arg_width;
    }
    if ($arg_height != "") {
	set $height $arg_height;
    }
    # 设置图片宽高
    image_filter resize $width $height;  
    # 设置nginx读取图片最大buffer
    image_filter_buffer 10M;  
    # 是否开启图片隔行扫描			
    image_filter_interlace on;   
    error_page 404 = error.gif;
}

还可以配合nginx的缓存使用。

给个地址玩玩:

缩放宽为200px:static.chengxinsong.cn/image/1.jpg…

缩放宽为100px:static.chengxinsong.cn/image/1.jpg…

图片隔行扫描:static.chengxinsong.cn/image/DSC_0…