Nginx real-time generated thumbnails stored on the hard disk

Source: Internet
Author: User

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 create a good cache directory

[Email protected] ~]# mkdir-p/data/stie_cache

[Email protected] ~]#


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/6B/wKiom1bn2ZmjCRdCAAANZU6kfVU558.png "title=" Cachedir.png "alt=" Wkiom1bn2zmjcrdcaaanzu6kfvu558.png "/>

2, modify the Nginx configuration, add the following content

Location ~* ^/resize {

root/data/site_cache/$server _name;

Set $width 150;

Set $height 100;

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 $;

}

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;

}

The complete nginx configuration file is as follows:

[email protected] 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 $;

Set $height $;

Set $image _path $;

Set $demins "_$1x$2";

}

if ($uri ~* "^/resize/(. *)") {

Set $image _path $;

}

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;

}

}

}

[Email protected] conf]#


Tips:

Nginx compile time to add parameter--with-http_image_filter_module, the Ngx_image_thumb-master also take the module, so the final nginx compilation parameters:

./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 Http://10.0.0.10/image/1.jpg. I need a copy of 100x100 's thumbnail image.

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 whether the existence of this picture, if there is directly put back to the user,

4, does not exist so jump to http://10.0.0.10/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/10.0.0.10/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.


This article from "Flat Light is true" blog, please be sure to keep this source http://ucode.blog.51cto.com/10837891/1751605

Nginx real-time generated thumbnails stored on the hard disk

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.