Alias path must end with '/' if location ends with '/' = B [OK]
Hint: Alias path needs trailing slash if location ends with slash [OK]
Common Mistakes:
Using root instead of alias incorrectly
Omitting trailing slash on alias path
Thinking location path cannot end with slash
5. You want nginx to serve static files from /var/www/app/static when users request URLs starting with /static/, but you want to avoid duplicating the /static/ part in the file path. Which location block correctly achieves this?
hard
A. location /static/ { root /var/www/app/static; }
B. location /static/ { alias /var/www/app/static/; }
C. location /static/ { alias /var/www/app/static; }
D. location /static/ { root /var/www/app; }
Solution
Step 1: Understand alias vs root behavior
Alias replaces the location prefix with the alias path exactly, avoiding duplication.
Step 2: Check trailing slashes for alias
Alias path must end with a slash to match location ending with slash, ensuring correct path mapping.
Step 3: Evaluate options
location /static/ { alias /var/www/app/static/; } uses alias with trailing slash, correctly mapping /static/file to /var/www/app/static/file. Others either duplicate path or miss slash.
Final Answer:
location /static/ { alias /var/www/app/static/; } -> Option B
Quick Check:
Alias with trailing slash avoids duplication = A [OK]
Hint: Use alias with trailing slash to avoid path duplication [OK]