Now with the emergence of the terminal (such as mobile phone, ipad tablet), as well as a variety of terminal mobile phone resolution and size is different, now mobile phone user traffic is treasure, online generated thumbnail appear all sorts of function of architecture, use php real-time generate thumbnails, useful also nginx + lua implementation, on the day I spoke also use nginx generated thumbnail, but every time the user access is need to generate one, will bring cpu and hard disk larger pressure, brought another 1 way today, This time use nginx to generate thumbnails from the original image to the hard disk. See my configuration
1. Create the cache directory first
[root@masterserver ~]# mkdir -p /data/stie_cache
[root@masterserver ~]#
2. Modify the nginx configuration and 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 $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;
}
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;
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]#
Tip:
When compiling nginx, you should add parameters — with-http_image_filter_module. To be on the safe side, you should also include the module ngx_image_thumb-master, so the final nginx compilation parameter is:
./configure —add-module=../ngx_image_thumb-master/ —with-http_image_filter_module
The process of generating thumbnails is as follows:
1, the original image in http: / / 10.0.0.10 image / 1. jpg. I need a thumbnail of 100x100.
2, request http: / / 10.0.0.10 resize_100x100 / image / 1. jpg.
3. This request enters location ~* ^/resize, and then determines whether this image exists in the directory image_path. If so, it is directly put back to the user.
4, there is no so jump to http: / / 10.0.0.10 image_resize/image / 1. jpg & # 63; width = 100 & height = 100;
5. location /image_resize performs the thumbnail function according to the incoming width and height, and sets the image quality to 75
6, then generate the file to/data/site_cache / 10.0.0.10 / resize_100x100 / image / 1. jpg
7. And return the picture to the user
8. That’s the end of nginx’s ability to generate thumbnails to your hard drive
The above is the site to introduce you Nginx thumbnail generation and storage on the hard disk of the relevant knowledge, I hope to help you!