Nginx生成縮略圖并存儲到硬盤上
現(xiàn)在隨著各終端的出現(xiàn)(手機,ipad等平板),以及各種終端的手機分辨率和尺寸都不同,現(xiàn)在手機用戶流量都是寶,網(wǎng)上出現(xiàn)了各種各樣的生成縮略圖功能的架構(gòu),有使用php實時生成縮略圖的,也有用nginx + lua實現(xiàn)的,上節(jié)我也講到了使用nginx生成縮略圖,但是用戶每次訪問都需要生成一次,會給cpu和硬盤帶來比較大的壓力,今天帶來了另外一種方式,這次使用nginx將原圖生成縮略圖到硬盤上.看我的配置
1、首先創(chuàng)建好cache目錄
[root@masterserver ~]# mkdir -p /data/stie_cache [root@masterserver ~]#
2、修改nginx配置,增加如下內(nèi)容
location ~* ^/resize { root /data/site_cache/$server_name; set $width 150; set $height 100; set $dimens ""; if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" ) { set $width $1; set $height $2; set $image_path $3; set $demins "_$1x$2"; } if ($uri ~* "^/resize/(.*)" ) { set $image_path $1; } set $image_uri image_resize/$image_path?width=$width&height=$height; if (!-f $request_filename) { proxy_pass http://127.0.0.1/$image_uri; break; } proxy_store /data/site_cache/$server_name/resize$demins/$image_path; proxy_store_access user:rw group:rw all:r; proxy_set_header Host $host; expires 30d; access_log off; } location /image_resize { alias /data/site/$server_name/; image_filter resize $arg_width $arg_height; image_filter_jpeg_quality 75; access_log off; }
完整的nginx配置文件如下:
[root@masterserver conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; image on; image_output on; } location ~* ^/resize { root /data/site_cache/$server_name; set $width 150; set $height 100; set $dimens ""; if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" ) { set $width $1; set $height $2; set $image_path $3; set $demins "_$1x$2"; } if ($uri ~* "^/resize/(.*)" ) { set $image_path $1; } set $image_uri image_resize/$image_path?width=$width&height=$height; if (!-f $request_filename) { proxy_pass http://127.0.0.1/$image_uri; break; } proxy_store /data/site_cache/$server_name/resize$demins/$image_path; proxy_store_access user:rw group:rw all:r; proxy_set_header Host $host; expires 30d; access_log off; } location /image_resize { alias /data/site/$server_name/; image_filter resize $arg_width $arg_height; image_filter_jpeg_quality 75; access_log off; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@masterserver conf]#
提示:
nginx編譯的時候要添加參數(shù)--with-http_image_filter_module,保險起見將模塊ngx_image_thumb-master也帶上,所以最終nginx編譯參數(shù)為:
./configure --add-module=../ngx_image_thumb-master/ --with-http_image_filter_module
生成縮略圖流程如下:
1、原圖在http://10.0.0.10/image/1.jpg。我需要一份100x100的縮略圖。
2、請求http://10.0.0.10/resize_100x100/image/1.jpg.
3、這個請求進入了location ~* ^/resize,接著判斷image_path這個目錄下是否存在這張圖片,如果存在直接放回給用戶,
4、不存在那么跳轉(zhuǎn)到http://10.0.0.10/image_resize/image/1.jpg?width=100&height=100;
5、location /image_resize根據(jù)傳入的width和height執(zhí)行縮略功能,并且設置圖像質(zhì)量為75
6、接著生成文件到/data/site_cache/10.0.0.10/resize_100x100/image/1.jpg
7、并且返回圖片給用戶
8、nginx生成縮略圖到硬盤上的功能到這里就結(jié)束了
以上所述是小編給大家介紹的Nginx生成縮略圖并存儲到硬盤上的相關(guān)知識,希望對大家有所幫助!
- CentOS下編譯、安裝與配置nginx
- CentOS 7.2 下編譯安裝PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法詳解(mini版本)
- CentOS 7.2.1511 編譯安裝Nginx1.10.1+MySQL5.6.33+PHP5.6.26運行環(huán)境
- CentOS 7.2.1511 編譯安裝Nginx1.10.1+MySQL5.7.14+PHP7.0.11
- CentOS 6.6服務器編譯安裝lnmp(Nginx1.6.2+MySQL5.6.21+PHP5.6.3)
- Centos下編譯安裝Nginx教程詳解
- Nginx配合php實現(xiàn)生成實時縮略圖功能
- Nginx服務器中用于生成縮略圖的模塊配置教程
- 在Nginx中配置image filter模塊來實現(xiàn)動態(tài)生成縮略圖
- CentOS下編譯安裝nginx及配置縮略圖插件的方法教程
相關(guān)文章
Nginx根據(jù)不同瀏覽器語言配置頁面跳轉(zhuǎn)的方法
這篇文章主要介紹了Nginx根據(jù)不同瀏覽器語言配置頁面跳轉(zhuǎn)的方法,包括一個簡體繁體的基本判斷方法及實際根據(jù)中英文跳轉(zhuǎn)的例子,需要的朋友可以參考下2016-04-04kubernetes啟用PHP+Nginx網(wǎng)頁環(huán)境教程
這篇文章主要介紹了kubernetes啟用PHP+Nginx網(wǎng)頁環(huán)境教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10Nginx 轉(zhuǎn)發(fā)匹配規(guī)則的實現(xiàn)
這篇文章主要介紹了Nginx 轉(zhuǎn)發(fā)匹配規(guī)則的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03Nginx出現(xiàn)403?Forbidden的幾種簡單解決方式
這篇文章主要介紹了Nginx出現(xiàn)403?Forbidden的幾種解決思路,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12