Opened 5 years ago
Closed 5 years ago
#2019 closed defect (invalid)
location = / is very tricky
| Reported by: | Дилян Палаузов | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | |
| Component: | nginx-module | Version: | 1.18.x |
| Keywords: | location | Cc: | |
| uname -a: | Linux 4.14.176 | ||
| nginx -V: | My browser is broken and I cannot paste text (Firefox under Wayland). | ||
Description
After reading http://nginx.org/en/docs/http/ngx_http_core_module.html#location I created a contiguration:
server {
server_name a.local;
root /a;
location = / {
root /b;
}
}
My understanding from the documentation is, that calling http://a.local/z will return /a/z, but calling http://a.local/ will return /b/index.html. This does not happen. I changed the configuration to
server {
server_name a.local;
root /a;
location = /index.html {
root /b;
}
}
Now, if /a/index.html exists, calling http://a.local/ returns /b/index.html, but if /a/index.html does not exist, then /b/index.html is not returned.
The documentation at nginx.org/en/docs/http/ngx_http_index_module.html makes things even more complicated. The value of the index directive is by default "index.html" and it seems impossible to delete the default value. Having this into account (the index-directive always has a value), location = / never matches
- clarify how to change the
rootfor the =/ request, when the file /index.html does not exist, but the request http://a.local/ is valid - Unify the documentation for the
indexandlocationdirectives, say how to remove the value forindexfor a specificserver{}and how to match exactly only theserver_name/request.

Your understanding is wrong. Requesting
http://a.local/will trigger an internal redirect to/index.htmlas long as/b/index.htmlexists. Since your configuration does not contain a location configured for/index.html, after the redirection the request will processed in the default location and will try to return/a/index.htmlas perrootin the server block.Consider reading the How nginx processes a request introductory article, hopefully it will help you to better understand how to configure nginx in various non-trivial cases like the one you are trying to configure. If you still have question, consider using support options available.