nginx: directly send from location to another named location

Solution 1:

We have a pretty complex Nginx configuration and we wanted to use named locations to keep things a bit more DRY. Because of this requirement, we had to do exactly what you are trying to.

The sad part is: we couldn't find any direct way of doing it, but the good news is that you can trick Nginx with a try_files directive without performance penalties, pointing the first argument to /dev/null:

try_files /dev/null @the_named_location;

It's definitely a workaround and usually I would burn any pull request containing this hack in flames and carefully explain the developer the horrors of not following the standards, etc... but after you get convinced and accept this sad reality, it actually makes your configuration look so much nicer. Really nicer!

We decided that this "hack" is "acceptable" as it proved to bring a big improvement, in terms of code quality in general.

(but it still hurts my eyes sometimes...)

Solution 2:

Your scheme will not work. When nginx settles on the final location block to process the request, it will use the the "settings and headers" that are in scope, which may be inherited from a surrounding block, but will not include any "extra headers and settings" from sibling blocks - irrespective of the process taken to find the final location block. See this document for more.

If you have common statements applicable to a number of locations, you can offload them to a separate file and include them where necessary. For example:

location / {
    try_files $uri @app;
}
location /api/ {
    # Extra conf!
    include my/proxy/conf;
}
location @app {
    include my/proxy/conf;
}

See this document for more.

Tags:

Nginx