in Web server by (13.8k points)
0 like 0 dislike

How I can to directly send handling from location to another named location directive in virtual host configuration file of Nginx web server?

There, this is not really right because it has a single argument:

location /api/ {
	try_files @app;

But it communicates well what I'm trying to achieve. I suppose I could get try_files to work by adding a non-esitent file before the last argument. Is try_files the only way to do it, or is there another more idiomatic directive?

nginx go to location

1 Answer

0 like 0 dislike
by (13.8k points)

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

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

See documentation for more.

You can trick Nginx with a try_files directive without performance penalties, pointing the first argument to /dev/null:

try_files /dev/null @named_location;

or vice versa:

try_files @named_location /dev/null;

Also you can just specify two same parameters in try_files directive:

try_files @name @name;

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.