I have below directories structure:
.
├── fruits
│ └── index.html
├── index.html
├── styles.css
and the content of my /etc/nginx/nginx.conf
file is:
http {
include mime.types;
server {
listen 8090;
root /srv/www/mysite;
location /fruits {
root /srv/www/mysite;
}
}
}
events {}
The nginx version is 1.24.0 (Ubuntu).
In modern nginx versions is there any difference in defining location with and without trailing slashes? For example:
location /fruits { ... }
vs location /fruits/ { ... }
I tried both of them and no matter which I choose, the result is always the same:
http://localhost:8090/fruits
serves fruits/index.html
, which is OKhttp://localhost:8090/fruitsalad
results in 404 Not FoundI have mentioned the second example because I have searched through some threads here on SO and some other places, and the information contained there is different comparing to what I have observed on my nginx version. For example the answer with 25 upvotes (its first paragraph) from this thread (2014): https://serverfault.com/a/607731 - product
-production
and fruits
-fruitsalad
follows the same pattern, but my results and the result described by the user in mentioned thread are different.
I have also noticed that even if I choose the definition without trailing slash (location /fruits
) nginx somehow adds the trailing slash, so in my web browser when I hit Enter
button after typing http://locahost:8090:fruits
this address is redirected to http://locahost:8090:fruits/
. curl
also shows that redirection 301 has taken place, so this is another thing that confuses me and makes me thinking that nowadays in nginx these trailing slashes aren't important at all.
I couldn't see the difference between location /fruits { ... }
and location /fruits/ { ... }
because fruits
was the directory in my root
directory. So, to make the thing clear:
/
to the URL, so if we type in the browser http://localhost:8090/fruits
it will be always rewritten to http://localhost:8090/fruits/
, because the directory fruits
exists in the root
directory and nothing except fruits
can be matched in that casehttp://localhost:8090/fruits
will not have the trailing /
automatically added, so http://localhost:8090/fruitsalad
will be also matched