Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx detached subrequest

Tags:

nginx

lua

I need to do some time consuming processing on images that are served by NGinx, and I'd like to be able to respond quickly with partially processed images from cache.

Here are the steps of I'd like:

  • User make first request for image A
  • User get image A without any processing
  • Connection is freed
  • image A is put on cache (A0)
  • A "detached" subrequest is started (S1) [first image transformation]
  • Until subrequest S1 is done, all request for image A get A0
  • When subrequest S1 is done, cache value is replaced with its results (A1)
  • From now on all request for image A get A1
  • A "detached" subrequest is started (S2) [second image transformation]
  • Until subrequest S2 is done, all request for image A get A1
  • When subrequest S2 is done, cache value is replaced with its results (A2) . . . and so on

I'm using NGinx Lua module to process the images, and I'd like to be able to use proxy_cache functionality (LRU clean up, TTL, etc)

I have tried using proxy_pass, post_action and ngx.location.capture, but all of them wait for the subrequest to finish to close the connection. I've seen some solutions like Drupal Cache Warmer that issue a OS call to curl, but if possible I'd like not to do that.

This is my test case so far

server {
    listen       8080;

    location / {

        rewrite_by_lua '
            ngx.say(".")
            --res = ngx.location.capture("/internal")
            ngx.exit(ngx.OK)
        ';

        proxy_pass http://127.0.0.1:8080/internal;

    }

    location /internal {
        content_by_lua '
            ngx.log(ngx.ERR, "before")
            ngx.sleep(10)
            ngx.say("Done")
            ngx.log(ngx.ERR, "after")
            ngx.exit(ngx.OK)
        ';
    }
}
like image 382
José Montiel Avatar asked Oct 04 '22 18:10

José Montiel


1 Answers

I have tried using post_action and ngx.location.capture, but both of them wait for the subrequest to finish to close the connection.

Take a look at ngx.eof() documentation.

Update: http://wiki.nginx.org/HttpLuaModule#ngx.eof

like image 62
Alexander Altshuler Avatar answered Oct 13 '22 12:10

Alexander Altshuler