Welcome! Log In Create A New Profile

Advanced

Lua, memc and image_filter, image not resized via lua

Markus Jelsma
September 12, 2012 11:00AM
Hello,

We attempt to make a simple image resizer and cacher using Nginx en storing in Memcache and the image_filter. First we attempted to do it via eval but we quickly ran into an old issue [1] i had, i can either store a not-existing image in the cache or return it, never both. Instead we tried using Lua scripting and now we can retrieve items from the cache and store remote images in the cache and return them at the same time!

But we have one problem left, the images are not passed through the image filter at all when we do it via Lua.

I'll share the config. The location below is called from outside and is the only location externally called. If it's in the cache, return it and it it's not in the cache, try to fetch it, store it in the cache and return it.

location /img/cache {
set $memc_key $args;
set $memc_cmd 'get';
error_page 404 = /img/populate?$args;
memc_pass 127.0.0.1:11211;
}

The following location populates the cache and returns the (hopefully resized) image. It uses a location to fetch the image (and resize it) and another location to write the fetched and resized image to the cache. It finally prints it.

location /img/populate {
content_by_lua '
local args = ngx.req.get_uri_args(5)
local image = ngx.location.capture("/img/crop?u=" .. args["u"] .. "&w=" .. args["w"] .."&h=" .. args["h"])
-- Use anoter location to store the image in the cache (cannot use memc_pass in this location it seems)
local res = ngx.location.capture("/img/put?u=" .. args["u"] .. "&w=" .. args["w"] .."&h=" .. args["h"] .. "&m=" .. args["m"], { body = image["body"] } )
ngx.print(image["body"])
';
}

The following 2 simple locations resp. fetch and resize the image and store it in memcache.

location /img/crop {
image_filter crop $arg_w $arg_h;
proxy_pass $arg_u;
}
location /img/put {
set $memc_key $args;
set $memc_cmd 'set';
memc_pass 127.0.0.1:11211;
}

The problem is, the image is not resized! If we set the error_page in the first location to /img/crop the image is properly resized so it doesn't work if called from Lua. I would be very grateful for any tips and hints. I am curious to what i'm doing wrong.

[1]: http://nginx.2469901.n2.nabble.com/Issue-with-3rd-party-memc-and-eval-modules-td4579623.html

Many thanks,
Markus






_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Lua, memc and image_filter, image not resized via lua

Markus Jelsma September 12, 2012 11:00AM

RE: Lua, memc and image_filter, image not resized via lua

Markus Jelsma September 14, 2012 06:44AM

Re: Lua, memc and image_filter, image not resized via lua

agentzh September 14, 2012 02:12PM

Re: Lua, memc and image_filter, image not resized via lua

agentzh September 14, 2012 06:24PM

Re: Lua, memc and image_filter, image not resized via lua

agentzh September 15, 2012 02:16AM

Re: Lua, memc and image_filter, image not resized via lua

Ilja Razinkov September 15, 2012 02:56AM

RE: Lua, memc and image_filter, image not resized via lua

Markus Jelsma September 15, 2012 08:20AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 181
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready