Enabling NGINX to forward static file request to origin server if the file is absent

rebaca nginx-forum at forum.nginx.org
Wed Jun 21 07:00:21 UTC 2017

Currently NGINX supports static file caching wherein if the file is present
in the location (derived from the config), then it will serve the client
directly. Else it just intimates the client that the file is not present.

There is no capability to forward the request to origin server, get the the
file, save it and serve it to the client AFAIK. I am not sure. Please
correct me if I am wrong.

I need to achieve above capability along with few more additions as
described below.


Currently I have a requirement with the following conditions:
- Both Nginx server and origin server should be on the same machine
- Nginx server should provide the file statically from the static cache when
the client requests it
- if Nginx server does not find the static file in the static file location
(generally the location path prepended by root), then it has to forward the
request to the origin server asking for the file 
- once it gets the file, it has to save it in the said location (static file
- after saving the file, it has to serve the same file back to the client

 - care should be taken so that when multiple client requests arrive
simultaneously and file cache is not present, it has to hold all the
requests get the file from the origin server and then provide the files for
all the clients (just like NGINX proxy handler does with proxy_cache_lock)
 - purging support should also be provided.

- earlier I planned to write an NGINX module by myself but I had to take
care of all the housekeeping and other stuffs already supported by static
cache hander (ngx_http_static_module.c). This method seemed to be bit
- then I planned to modify the static cache module itself so that whenever
it does not find the file in the said location, I can modify the code so
that I can forward the request to upstream server. In the same module, once
the response is obtained from the upstream server, modify the code so that
the file is saved in the said location and also served to the clients.
- NOTE: I am not enabling NGINX proxy handler.
- I also need to support the functionality similar to proxy_cache_lock where
multiple client requests are held in the queue and served 

- Please let me know if the approach I am planning serves the purpose ?
- Do you have any other alternative approach. Please do let me know.
- Is there any way to delegate the functionality to default handler ? (for
example, if the static file is already present, in my handler can I delegate
further processing to default static file cache module of NGINX)

Looking forward for your valuable input

Thank you

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,275008,275008#msg-275008

More information about the nginx mailing list