Nginx Lua_nginx+graphicsmagick Real-time Thumbnail example

Source: Internet
Author: User
Tags lua openssl


1. Installation GraphicsMagick

Cd/usr/local/src

wget http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.20/GraphicsMagick-1.3.20.tar.gz/download

TAR-ZXVF graphicsmagick-1.3.20.tar.gz

CD GraphicsMagick-1.3.20

./configure--prefix=/usr/local/graphicsmagick

Make && make install

2, download Luajit

wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz

TAR-ZXVF luajit-2.0.2.tar.gz

CD LuaJIT-2.0.2

Make && make install Prefix=/usr/local/lj2

LN-SF Luajit-2.0.2/usr/local/lj2/bin/luajit

3. Download Ngx_devel_kit Module

wget Https://github.com/simpl/ngx_devel_kit/archive/master.zip Unzip Master.zip
4. Download Lua-nginx-module Module
Https://github.com/openresty/lua-nginx-module/archive/master.zip Unzip Master.zip

5. Download echo

Https://github.com/agentzh/echo-nginx-module/zipball/master Unzip Echo-nginx-module.zip

6. Download Cache

wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz

TAR-ZXVF ngx_cache_purge-2.1.tar.gz

CD ngx_cache_purge-2.1

...

7, download Nginx

Wget http://nginx.org/download/nginx-1.7.5.tar.gz ...

8. Set environment variable

Export Luajit_lib=/usr/local/lj2/lib

Export luajit_inc=/usr/local/lj2/include/luajit-2.0

Export ld_library_path=/usr/local/lib/: $LD _library_path

Export Pkg_config_path=/usr/local/lib/pkgconfig: $PKG _config_path

Gm_home=/usr/local/graphicsmagick;

Path= $GM _home/bin: $PATH;

Export PATH

Export Gm_home

Source/etc/profile

9, Nginx compile installation

--prefix=/usr/local/nginx--with-http_stub_status_module--add-module=/usr/src/simpl-ngx_devel_kit--with-ld-opt= -wl,-rpath,/usr/local/lj2/lib--add-module=/usr/src/ngx_cache_purge-2.1--with-http_perl_module--add-module=/usr /local/src/lua-nginx-module--add-module=/usr/local/src/echo-nginx-module-master

If the compilation is wrong, please attach the pcre extension

10, Nginx Configuration


11, virtual directory Vhost


server {

Listen 80;

server_name image.host.com;

Root/usr/local/nginx/html/test;

Index index.html index.htm index.php;

LOCATION/LUA1 {

Default_type ' Text/plain ';

Content_by_lua ' Ngx.say ("Hello, lua") ';

}

Location/image {

Set $image _root/usr/local/nginx/html/test;

Set $file "$image _root$uri";

if (!-f $file) {

Rewrite_by_lua '

Local index = String.find (Ngx.var.uri, "([0-9]+) x ([0-9]+)");

if (index = = nil) Then

Ngx.exit (404);

Local Originaluri = string.sub (Ngx.var.uri, 0, index-2);

Local area = String.sub (Ngx.var.uri, index);

index = string.find (area, "([.])");

Area = string.sub (area, 0, index-1);

Local image_sizes = {"160x160", "400x300"};

Local Image_big = {"80x60^", "120x90^", "160x120^"};

function table.contains (table, Element)

For _, value in pairs (table) do

if value = = Element Then

return True

End

End

return False

End

# Print (Table.contains (image_big, area))

If Table.contains (Image_big, area) then

Local command = "/usr/local/graphicsmagick/bin/gm convert".  Ngx.var.image_root.. Originaluri.. "-thumbnail". Area.. "-background gray-gravity center-extent". Area.. " " .. Ngx.var.image_root.. Ngx.var.uri

Os.execute (command);

Else

If Table.contains (image_sizes, area) then

Local command = "/usr/local/graphicsmagick/bin/gm convert".  Ngx.var.image_root.. Originaluri.. "-thumbnail". Area.. " " .. Ngx.var.image_root.. Ngx.var.uri;

Os.execute (command);

Else

Ngx.exit (404);

End

End

';

}

# alias/usr/local/nginx/html/test/image;

Expires 7d;

}

}


12. Give permission

chmod o+w/usr/local/nginx/html/test/image

13, visit

Original address: Yun_qi_img/2.jpg

Thumbnail Address: yun_qi_img/2.jpg.400x300.jpg

Error

1

./configure--prefix=/usr/local/nginx--with-http_stub_status_module

--add-module=/usr/local/src/ngx_devel_kit-0.2.19--with-ld-opt=-wl,-rpath,/usr/local/lj2/lib

--add-module=/usr/local/src/ngx_cache_purge-2.1--with-http_perl_module

--add-module=/usr/local/src/lua-nginx-module-master

--add-module=/usr/local/src/openresty-echo-nginx-module-a736511

--with-pcre=/usr/local/src/pcre-8.35--with-zlib=/usr/local/src/zlib-1.2.8

--with-openssl=/usr/local/openssl

Make

Make install

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.