[PATCH v3 00/12] Static: Implement new "index" option

Valentin V. Bartenev vbart at nginx.com
Sun Dec 19 15:47:48 UTC 2021

On Sunday, 19 December 2021 04:30:18 MSK Alejandro Colomar wrote:
> Hi Valentin,
> I have implemented part of the last step for adding this feature,
> as you proposed them:
>  *(1) Add new "index" option, with minimal support (a string).
>  *(2) Allow variables in the "index" option.
>  +(3) Allow multiple filenames in the "index" option.
> However, there's something that I couldn't yet fully understand,
> which I need for this:  how does the iteration of shares exactly
> work, how do other variables (e.g., chroot, fallback) intervene,
> and how is index expected to intervene in that mix.  Since instead
> of an explicit loop there are quite some nested function calls,
> I couldn't manage to understand it.  Could you please share some
> brief description of the purpose of each static function in
> <src/nxt_http_static.c> and how all this iteration works?

Variable strings intrepolation is asynchronous, so it may take any amount
of time, that's why we don't interpolate them until they are needed.

And that's also why it can't be just a loop, as we can't block the processing
thread on waiting for variable resolution.  Instead we schedule an event, and
provide a callback, that will be called once the variable string interpolation

"share" works exactly as it's documented:

 - https://unit.nginx.org/configuration/#static-files

it takes the first value from an array in nxt_http_static_iterate() and
schedules interpolation query (by nxt_var_query_resolve() call) with all
the other options, that will be needed to process.

Once they are interpolated, the nxt_http_static_send_ready() callback is
called, which tries to open the configured file path.

If opening fails, then nxt_http_static_next() is called, which switches to the
next value of "share" option by incrementing index, and starts interpolation
of it by calling nxt_http_static_iterate() again.

When "share" and "index" both configured as arrays, then it interates over
"share" trying to serve file.  If the file path points to a directory, then
it iterates over "index" trying to serve index file.  If no index file is
found for a particular "share" path, it continues to interate over "share"
array.  If it's a directory path again, it iterates over "index" array for
that path again.

So each time, when the "share" value points to a directory, it should interate
over indexes until the working one will be found.


More information about the unit mailing list