nginx系列nginx升级到支持httpimagefiltermodule处理图片

6次阅读

共计 1418 个字符,预计需要花费 4 分钟才能阅读完成。

一、前言

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

二、http_image_filter_module 模块

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

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

上图知道了 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 这个路径是我们编译之后的包路径。

发现报错:

查找发现: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

执行成功后:

配置完成后,运行命令

nake

执行成功之后:

这里不要进行 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 文件

移动编译好的 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:http://static.chengxinsong.cn…

缩放宽为 100px:http://static.chengxinsong.cn…

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

正文完
 0