nginx 配置圖片模塊image_filter和nginx 平滑升級


#nginx配置簡單圖片系統

* 用到的nginx模塊有image_filter模塊
* 該模塊主要的配置參數為

```
image_filter size; // 以json的方式輸出圖片格式信息
image_filter rotate 90 | 180 | 270; // 圖片逆時針旋轉指定度數,該度數只能是90, 180, 270
image_filter resize width height // 縮小圖片到指定的規格,如果想讓一個維度縮放,另外一個維度等比例縮放,則另外維度參數值為'-'.如果配合rotate指令使用,那么執行順序是先縮放,后旋轉。
image_filter crop width height // 從左上角按照進行裁剪
image_filter off; // 關掉image_filter的應用
image_filter test; // 確保圖片格式為jpeg,gif,png,webp等格式,否則返回415錯誤(Unsupported Media Type)



image_filter_buffer 10M // 設置圖片最大緩存,如果超過,則返回415(Unsupported Media Type)
image_filter_interlace on|off
image_filter_jepg_quality 1~100;
image_filter_sharpen percent;
image_filter_transparency on|off;
image_filter_webp_quality 1~100

```

###配置可訪問原圖和格式尺寸的圖片

```
server {
listen 80;
server_name static.mys.com;
access_log /var/log/nginx/host.access.log;
error_log /var/log/nginx/host.error.log;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* /(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ { // 含有圖片規格的請求方式 root /rootdir;
set $h $3;
set $w $2;
if ($h = '0') {
rewrite /(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ /$1.$4 last; // 如果高度為0那么跳轉訪問原圖,注意rewrite指令中last的用法
}
if ($w = '0') {
rewrite /(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ /$1.$4 last; // 如果寬度為0那么跳轉訪問原圖,注意rewrite指令中last的用法
}
rewrite /(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ /$1.$4 break; // 改寫訪問的url地址,注意rewrite指令中break的用法
image_filter resize $w $h; // 配置image_filte模塊並使用
image_filter_buffer 2M;
image_filter_interlace on;
}

location ~* /.*\.(jpg|gif|png)$ { // 訪問原圖
root /rootdir;
}
}
```

### 對於nginx測試配置文件出現的問題
* unknown directive "image_filter" 問題

主要是因為Nginx中沒有編譯加入image_filter模塊,因此需要重新編譯加入該模塊。

### Nginx的升級(centos下)
* 下載需要升級的版本,比如nginx1.17.4

```
wget http://nginx.org/download/nginx-1.17.4.tar.gz

```
* 進入下載目錄,解壓下載好的壓縮包,比如/home/work/nginx目錄下:

```
tar -zxf nginx-1.17.4.tar.gz // 解壓壓縮包
cd ./nginx-1.17.4/ // 進入解壓目錄

```
* 查看當前nginx版本的編譯參數

```
nginx -V // 查看當前版本的編譯參數

which nginx // 查看當前的nginx執行文件路徑
```
* 重新配置nginx

```
./configure 配置參數 (通過nginx -V 找到的配置參數,同時加上 --with-http_image_filter_module 模塊,注意修改--prefix 為通過which nginx查看到的路徑
```
* 如果上一步報錯,出現

```
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
```
* 需要安裝gd庫,centos下的安裝命令為:

```
yum install gd gd-devel
```
* 安裝完成,重新執行一遍./configure即可安裝完成。
* 之后執行make命令進行編譯,不要執行make install.

```
make
```
* 平滑替換舊的nginx執行文件

```
// 假如which nginx 得到的執行文件路徑為:/usr/local/nginx
mv /usr/local/nginx /usr/local/nginx.old
cp ./objs/nginx /usr/local/nginx
```
* 然后在源碼目錄下執行:

```
make upgrade
```
* 確認image_filter安裝完成,通過

```
nginx -V
```

* 完成之后需要重新啟動nginx一次

```
nginx -s stop
nginx
```

* 即完成了nginx的平滑升級以及image_filter模塊的安裝。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM