stolli
stolli

Reputation: 5926

Nginx Location directives, what is the effect of trailing or prepended slashes?

I.e., what is the difference between

location somefolder {

}

and

location somefolder/ {

}

and

location /somefolder {

}

and

location /somefolder/ {

}

I know it's kind of a silly question but honestly sometimes I get myself confused, a concise answer would be nice!

Upvotes: 0

Views: 161

Answers (1)

stolli
stolli

Reputation: 5926

Found my own answer.

The first two configurations will not match anything, because every location starts with a "/". So they are basically invalid locations. A commenter on Server Overflow mentioned this and it proved to be correct.

The second two, for most purposes, are equivalent, in that NGINX will 301 redirect the third to the fourth. So a request for http://somedomain/somefolder will be redirected to http://somedomain/somefolder/. If you really have a location called /somefolder, such as a file literally called that, then you could create a location with the "=" operator, which forces an exact match, so

location = /somefolder {

}

For best practice just always use the /somefolder/ format unless you really need the exception I just mentioned.

Upvotes: 1

Related Questions