Fastdfs and nginx to compress picture ratios _nginx

Source: Internet
Author: User
Tags lua

The first to do in front of the function in this share, is a imitation of Taobao, I hope the great God has any ideas can not hesitate to enlighten:

By using the previous reference configuration Lua and Nginx, using the powerful features of the scripting language Lua and Nginx features to implement this feature, add the following code to the nginx.conf configuration file:

server {Listen 22222;

  server_name localhost;

  # server_name somename alias Another.alias;

  location/images/{alias/root/images;

  Set $image _root/root;

  Set $file $image _root$uri;

    Content_by_lua ' Ngx.header.content_type = "Text/plain";

   Ngx.say (Ngx.var.file);

  ';

   location/lua{set $test "Hello, World."

     Content_by_lua ' Ngx.header.content_type = "Text/plain";

   Ngx.say (ngx.var.test);

  ';

   } location/group1/m00 {alias/usr/local/servers/data/fdfs/data;

   Set $image _root "/usr/local/servers/data/fdfs/data";

   # alias/root/images;

   # set $image _root "/root/images"; if ($uri ~)/([a-za-z0-9]+)/([a-za-z0-9]+)/([a-za-z0-9]+)/([a-za-z0-9]+)/(. *)] {set $image _dir $image _root/$3/$

     4 ";

     Set $image _name "$";

    Set $file "$image _dir/$image _name";

   # content_by_lua ' # ngx.header.content_type = "Text/plain";

   # Ngx.say (Ngx.var.image_dir); # Ngx.say (ngx.var.image_name);

   # Ngx.say (ngx.var.file);

    # ';

    if (!-f $file) {# Closes the LUA code cache to easily debug the Lua script #lua_code_cache off;

    Content_by_lua_file "/usr/local/servers/lua/convert.lua";

  } Ngx_fastdfs_module;

   # location ~/group[1-3]/m00{# root/usr/local/servers/data/fdfs/data; #/fdfs/storage/data;

   # ngx_fastdfs_module;

 # }

  }

This takes advantage of the nginx regular expression, which is pretty tough to get the value of the URI you need to access.

Then write the following code in Convert.lua:

 local area = Nil local Originaluri = Ngx.var.uri;

Local originalfile = Ngx.var.file;

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

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

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

 index1 = String.find (Area, "([.])");

 AREA1 = string.sub (area, 0, index1-1);

 Local index2 = String.find (Originalfile, "([0-9]+) x ([0-9]+)");

OriginalFile1 = string.sub (originalfile, 0, index2-2) End of local image_sizes = {"80x80", "800x600", "40x40", "60x60"}; function table.contains (table, Element) for _, value in pairs (table) do if value = = Element then return True E nd end return False End If Table.contains (image_sizes, area1) then the local command = "/usr/bin/gm convert". OriginalFile1.. "-thumbnail". Area1.. "-background gray-gravity center-extent". Area1.. " " ..

 Ngx.var.file;

Os.execute (command);

End 

~ Almost this can achieve the function, through access to achieve the ratio of compression, because in the work time to write down temporarily, the current one has a lot of functions need to be realized, such as have to read can leave a message, the great God can advise, thank you!

Reference: http://www.v2ex.com/t/113845

Http://blog.sina.com.cn/openresty

Http://write.blog.csdn.net/postedit

Https://github.com/azurewang/lua-resty-fastdfs/blob/master/lib/resty/fastdfs/storage.lua

Http://www.jb51.net/sys/CentOS/55070.htm

Http://wiki.nginx.org/HttpLuaModule

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.