What’s the difference
root /var/www/site;insidelocation /files/looks up/files/report.txtat/var/www/site/files/report.txt.alias /srv/archive/;insidelocation /files/looks up/files/report.txtat/srv/archive/report.txt(Static & SPA).
Before and after
# Unsafe (off-by-slash): /files../private/key.pem -> /srv/archive/../private/key.pem
location /files {
alias /srv/archive/;
}
# Safe: trailing slash on both location and alias
location /files/ {
alias /srv/archive/;
}
When the directory name on disk matches the URL path, pick root — it isn’t affected by off-by-slash.
If it doesn’t work
With alias, every file returns 404 and error.log shows a glued path like /srv/archivereport.txt
- Cause
location /files/ends with a slash, whilealias /srv/archive;is missing one.- Fix
- Add a trailing slash to
alias:alias /srv/archive/;.
Requesting /files../secret.env returned a file from the sibling directory
- Cause
- Classic off-by-slash:
location /files(no/) paired withalias /srv/archive/;(with/). - Fix
- Put a trailing slash on both:
location /files/andalias /srv/archive/;.