Welcome! Log In Create A New Profile

Advanced

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

June 21, 2017 03:00AM
BACKGROUND:
-----------------------
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.

REQUIREMENT:
-------------------------

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 location)
- after saving the file, it has to serve the same file back to the client

also
- 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.

IMPLEMENTATION:
----------------------------
- 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 cumbersome
- 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

QUESTION
------------------
- 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
Subject Author Posted

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

rebaca June 21, 2017 03:00AM

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

Reinis Rozitis June 21, 2017 04:50AM

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

rebaca June 21, 2017 09:23AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 290
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready