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

1、前言

最近在研究nginx在前端中使用最大化,發現了能夠很好的處理圖片。前端

2、http_image_filter_module模塊

咱們先來到軟件包的地方看到了configure。nginx

image

咱們先看一下這個模塊http_image_filter_module,已是內置模塊了,可是須要從新編譯一下,添加這個模塊。緩存

image

上圖知道了nginx在編譯時不會自動構建http_image_filter_module和http_v2_module。因此須要從新編譯nginx。服務器

注意:下次研究一下動態模塊加載ui

3、加入參數編譯

新增長的配置,咱們須要從新編譯。spa

./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這個路徑是咱們編譯以後的包路徑。code

image

發現報錯:blog

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

執行成功後:ssl

image

配置完成後,運行命令

nake

執行成功以後:

image

這裏不要進行make install,不然就是覆蓋安裝。

4、備份和替換

(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

5、圖片縮放的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...

相關文章
相關標籤/搜索