使用proxy_store存储图像,然后使用lua进行处理

时间:2014-08-22 00:17:45

标签: nginx lua

我正试图用nginx来完成这样的事情。首先我使用proxy_pass下载图像。然后我想用lua操纵那个图像并提供被操纵的图像。我认为最简单的方法是使用proxy_store将图像下载到文件中:

location ~* ^/test/(.*?)/(.*) {
    alias /some/path/$1_$2;
    proxy_pass http://$1/$2;
    proxy_store on;
    content_by_lua '
        -- use image at /some/path/$1_$2 here
    ';
}

然后用lua读取并操作该文件。但是,在下载图像并使用content_by_lua保存之前,这会移至proxy_store部分。如何在继续content_by_lua之前下载图像?

1 个答案:

答案 0 :(得分:2)

想出来:我一直在寻找nginx子请求。我的新解决方案是这样的:

location ~* /proxy/(.*?)/(.*) {
  # download and return the image
  proxy_pass http://$1/$2;
}

location ~* ^/test/(.*?)/(.*) {
    # so we can use this url from lua
    set $url $1/$2;
    content_by_lua '
        -- grab the content of url using the /proxy route. This create a subrequest,
        -- which means it fits within nginx's async model.
        response = ngx.location.capture("/proxy/" .. ngx.var.url)
        -- response.body contains the image, do whatever you want with it
        resized = resize_image(response.body)
        -- finally, return the final image
        ngx.say(resized)
    ';
}