nginxnginx-location

Defining locations with and without trailing slash in modern nginx


INFO

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).

QUESTION

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:

I 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.


Solution

  • 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: