【nginx系列】nginx升級到支持http_image_filter_module處理圖片

一、前言

最近在研究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:http://static.chengxinsong.cn...

縮放寬爲100px:http://static.chengxinsong.cn...

圖片隔行掃描:http://static.chengxinsong.cn...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章