Home > Software engineering >  What is difference between using $uri and $uri$args in try_files directive
What is difference between using $uri and $uri$args in try_files directive

Time:05-11

For my other Angular apps, I am using the below config and everything seems to work fine.

location / {
     try_files $uri$args $uri$args/ /index.html;
}

Now the one which I am working has nested folders within the dist folder.

And folder structure is something like:

dist \
  -- assets
  -- folder1
     -- folder2
        -- folder3
        -- folder4
        -- folder5
        index.html
  index.html

And the inner index.html is called with query params, and the url will be like - <ip>/folder1/folder2/index.html?a=1&b=2&c=3. But this is returning the fallback index.html at the root location. Later, I changed the above location block like this and it started working properly.

location / {
     try_files $uri $uri/ /index.html;
}

So I am not clear why the first location block didn't work. And I can't find anything in docs - try_files

CodePudding user response:

All of the parameters of a try_files statement except the last parameter are looking for filenames in the local file system.

So given the URI /foo/bar, the term $uri$args will search for a local file at /path/to/root/foo/bar, and if it does not exist will move on to the next term or the default clause at the end of the statement.

In the above case, $args is empty. However, given the URI /foo/bar?baz, the term $uri$args will search for a local file at /path/to/root/foo/barbaz.

I don't know why anyone would use $uri$args or $uri$args/ as file terms on a try_files statement, but there may well be a legitimate use case.

The last parameter of a try_files statement is special. It can be a status code (for example =404), a named location to branch to, or a URI.

In the case of a URI, Nginx will internally redirect to that URI. A typical example might be /index.php$isargs$args - in this case it is perfectly legitimate to append the $args parameter, as we are creating a new URI and keeping the original argument string.

See this document for details.

  • Related