Nginx generates thumbnails and stores them on the hard drive _nginx

Source: Internet
Author: User

Now with the advent of each terminal (cell phone, ipad and other tablets, as well as the various terminals of the mobile phone resolution and size are different, and now the mobile phone user traffic is treasure, the emergence of a variety of web-generated thumbnail function of the architecture, the use of PHP real-time generation of thumbnails, but also useful nginx + LUA implementation, In the last section I also talked about the use of nginx to generate thumbnails, but the user needs to generate one time per visit, the CPU and hard disk will bring more pressure, today brings another way, this time using Nginx to generate thumbnails to the hard disk. Look at my configuration.

1, first create a good cache directory

[Root@masterserver ~]# mkdir-p/data/stie_cache 

2, modify the Nginx configuration, add the following content

Location ~* ^/resize {
root/data/site_cache/$server _name;
Set $width;
Set $height;
Set $dimens "";
if ($uri ~* "^/resize_ (\d+) x (\d+)/(. *)") {
set $width $;
Set $height $;
Set $image _path $;
Set $demins "_$1x$2";
}
if ($uri ~* "^/resize/(. *)") {
set $image _path $
}
The 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;
Access_log off;

The complete nginx configuration file is as follows:

[Root@masterserver conf]# cat nginx.conf worker_processes 1; Events {worker_connections 1024} http {include mime.types; Default_type application/octet-stream; sendfile on; Keepali
Ve_timeout 65;
server {listen 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; set $height set $dimens ""; if ($uri ~* "^/resize_ (\d+) x (\d+)/(. *)") {Set $width $ set $height $ set $image _path $ set $demins "_$1x$2";} if ($uri ~* "^/resize/(. *)")
{Set $image _path $}
The 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 502 503 504/50x.html;
Location =/50x.html {root html;}}  } [Root@masterserver conf]#

Tips:

Nginx compile the time to add parameters--with-http_image_filter_module, insurance to the module Ngx_image_thumb-master also with, so the final Nginx compilation parameters are:

./configure--add-module=. /ngx_image_thumb-master/--with-http_image_filter_module

The process for generating thumbnails is as follows:

1, the original image in Yun_qi_img/1.jpg. I need a thumbnail of the 100x100.

2, Request http://10.0.0.10/resize_100x100/image/1.jpg.

3, this request entered the location ~* ^/resize, and then Judge Image_path This directory is the existence of this picture, if there is a direct return to the user,

4, does not exist so jump to yun_qi_img/1.jpg?width=100&height=100;

5, Location/image_resize according to the incoming width and height to perform the thumbnail function, and set the image quality of 75

6, then generate files to/data/site_cache/10.0.0.10/resize_100x100/image/1.jpg

7, and return the picture to the user

8, Nginx generated thumbnails to the hard disk function to the end of Here

The above is a small set to introduce the Nginx to generate thumbnails and stored on the hard disk related knowledge, I hope to help!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.