0

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:

  • http://localhost:8090/fruits serves fruits/index.html, which is OK
  • http://localhost:8090/fruitsalad results in 404 Not Found

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.

2
  • 1
    http://localhost:8090/fruitsalad results in 404 Not Found - no wonder since you don't have the fruitsalad file (or directory) under your web server root. The answer you referring already mentions the relevant documentation part regarding ..._pass directives used as a location content handler; however, locations that used static content handler behaves the same way, as I described here. From my point of view, you should not use locations without trailing slash (unless you really need to for some very specific reason). Commented Mar 14 at 7:32
  • 1
    Additionally, for SEO considerations, you should not use two different URLs with and without trailing slash to serve same content, so this automatic 301 redirection is only for good. Commented Mar 14 at 7:34

1 Answer 1

0

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:

  • for directories nginx will always append the trailing / 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 case
  • for proxy passes the behavior described above doesn't apply - http://localhost:8090/fruits will not have the trailing / automatically added, so http://localhost:8090/fruitsalad will be also matched
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.