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

Nginx 使用代理存储存储图像,然后使用lua进行处理,nginx,lua,Nginx,Lua,我正试图用nginx完成类似的事情。首先,我使用proxy_pass下载图像。然后我想用lua来处理这个图像,并为处理后的图像提供服务。我认为最简单的方法是使用proxy\u store将图像下载到文件中: location ~* ^/test/(.*?)/(.*) { alias /some/path/$1_$2; proxy_pass http://$1/$2; proxy_store on; content_by_lua ' -- use i

我正试图用nginx完成类似的事情。首先,我使用proxy_pass下载图像。然后我想用lua来处理这个图像,并为处理后的图像提供服务。我认为最简单的方法是使用
proxy\u 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读取并操作该文件。但是,在下载图像并使用
proxy\u store
保存之前,这将转到
content\u by\u lua
部分。如何使图像在转到
content\u之前由\u lua下载?

解决了:我正在寻找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)
    ';
}