Lua, memc and image_filter, image not resized via lua

Markus Jelsma markus.jelsma at openindex.io
Wed Sep 12 15:02:45 UTC 2012


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








More information about the nginx mailing list