basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    root vs alias — pair the trailing slash or hit off-by-slash

    root appends the full URI to the directory, while alias replaces the matched location prefix. Putting a trailing slash on alias without one on location opens a path-traversal bug one directory up (off-by-slash).

    What’s the difference

    • root /var/www/site; inside location /files/ looks up /files/report.txt at /var/www/site/files/report.txt.
    • alias /srv/archive/; inside location /files/ looks up /files/report.txt at /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, while alias /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 with alias /srv/archive/; (with /).
    Fix
    Put a trailing slash on both: location /files/ and alias /srv/archive/;.