Nginx real-time generation of thumbnails to the hard disk

Source: Internet
Author: User

Original: http://www.ttlsa.com/nginx/nginx-create-image-on-disk/

Now with the advent of the terminal (mobile phones, ipads and other tablets), as well as the various terminals of the phone resolution and size are different, now mobile phone user traffic is treasure, there are a variety of online generation of thumbnail features of the architecture, there is a real-time use of PHP generated thumbnails, but also useful nginx + LUA implementation, I also talked about the use of nginx to generate thumbnails, but the user each access needs to be generated once, will give the CPU and hard drive a lot of pressure, today brought another way, this time using Nginx to generate thumbnails to the hard disk. See my configuration

1. First build the cache directory

# mkdir/data/site_cache/

2. Modify Nginx Configuration

Location ~* ^/Resize {root/data/site_cache/$server _name; Set$width Max; Set$height -; 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 the;        Access_log off; }

The process for generating thumbnails is as follows:
1, the original image in Www.ttlsa.com/image/1.jpg. I need a copy of 100x100 's thumbnail image.
2, Request www.ttlsa.com/resize_100x100/image/1.jpg.
3, this request entered the location ~* ^/resize, and then judge Image_path this directory whether the existence of this picture, if there is directly put back to the user,
4, does not exist so jump to http://www.ttlsa.com/image_resize/image/1.jpg?width=100&height=100;
5. Location/image_resize performs the thumbnail function based on the width and height passed in, and sets the image quality to 75
6. Then generate files to/data/site_cache/www.ttlsa.com/resize_100x100/image/1.jpg
7, and return the picture to the user
8, Nginx generated thumbnails to the function on the hard drive it's over here.

Nginx real-time generation of thumbnails to the hard disk

Related Article

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.