Bird
0
0

How should you configure Nginx to serve static files from /var/www/static/ and proxy all other requests to a Django app on port 8000?

hard📝 Application Q8 of 15
Django - Deployment and Production
How should you configure Nginx to serve static files from /var/www/static/ and proxy all other requests to a Django app on port 8000?
A<pre>location /static/ { root /var/www; } location / { proxy_pass http://localhost:8000; }</pre>
B<pre>location /static/ { root /var/www/static/; } location / { proxy_pass http://localhost:8000; }</pre>
C<pre>location /static/ { proxy_pass http://localhost:8000/static/; } location / { root /var/www/static/; }</pre>
D<pre>location /static/ { alias /var/www/static/; } location / { proxy_pass http://localhost:8000; }</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Serve static files correctly

    Use alias /var/www/static/; inside location /static/ to map URL path to filesystem directory properly.
  2. Step 2: Proxy other requests

    Use proxy_pass http://localhost:8000; inside location / to forward all other requests to Django.
  3. Step 3: Avoid root misuse

    Using root with /static/ location can cause path duplication issues.
  4. Final Answer:

    location /static/ {
        alias /var/www/static/;
    }
    location / {
        proxy_pass http://localhost:8000;
    }
    correctly configures static serving and proxying.
  5. Quick Check:

    Alias for static, proxy_pass for backend [OK]
Quick Trick: Use alias for static, proxy_pass for Django backend [OK]
Common Mistakes:
MISTAKES
  • Using root instead of alias for static files
  • Proxying static files instead of serving directly
  • Incorrect path mappings causing 404 errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes