Have you seen this code? 

    https://github.com/agentzh/srcache-nginx-module/blob/master/src/ngx_http_srcache_fetch.c#L205

It's tricky.