Bird
Raised Fist0
Djangoframework~10 mins

Nginx as reverse proxy in Django - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Nginx as reverse proxy
Client sends HTTP request
Nginx receives request
Nginx forwards request to Django app
Django app processes request
Django app sends response back to Nginx
Nginx sends response to client
The client sends a request to Nginx, which forwards it to the Django app. The app processes it and sends the response back through Nginx to the client.
Execution Sample
Django
server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}
Nginx listens on port 80 and forwards all requests to the Django app running on localhost port 8000.
Execution Table
StepActionRequest URLNginx BehaviorDjango ResponseClient Receives
1Client sends request/homeReceives requestNo response yetNo response yet
2Nginx forwards request/homeForwards to http://127.0.0.1:8000/homeProcesses requestNo response yet
3Django processes/homeWaiting for responseGenerates HTML pageNo response yet
4Django sends response/homeReceives responseSends HTML backNo response yet
5Nginx sends response/homeSends HTML to clientResponse sentReceives HTML page
6Client renders page/homeIdleIdlePage displayed
7End----
💡 Request cycle completes when client receives the response and renders the page.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
Request URLNone/home/home/home/home
Nginx StateIdleForwarding requestWaiting for responseSending responseIdle
Django StateIdleProcessing requestSending responseIdleIdle
Client StateIdleWaitingWaitingReceiving responsePage displayed
Key Moments - 3 Insights
Why does Nginx forward the request instead of handling it directly?
Nginx acts as a reverse proxy to efficiently manage connections and serve static files, while Django handles the application logic. See execution_table step 2 where Nginx forwards the request to Django.
When does the client actually receive the response?
The client receives the response only after Django processes it and Nginx sends it back, as shown in execution_table step 5.
What happens if Django is not running on port 8000?
Nginx will fail to forward the request, causing errors or timeouts. This is because proxy_pass points to http://127.0.0.1:8000, so Django must be listening there.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is Nginx doing at step 3?
AForwarding the request to Django
BSending response to client
CWaiting for Django's response
DIdle
💡 Hint
Check the 'Nginx Behavior' column at step 3 in the execution_table.
At which step does the client receive the HTML page?
AStep 5
BStep 4
CStep 2
DStep 3
💡 Hint
Look at the 'Client Receives' column in the execution_table.
If Django was listening on port 9000 instead of 8000, what would change in the execution_table?
AClient would receive response earlier
BNginx would forward to port 9000 instead of 8000
CNo change needed in Nginx config
DDjango would not process requests
💡 Hint
Check the proxy_pass URL in the execution_sample code and relate to step 2 in execution_table.
Concept Snapshot
Nginx as reverse proxy:
- Nginx listens on a public port (e.g., 80)
- Forwards client requests to Django app (e.g., localhost:8000)
- Django processes and returns response
- Nginx sends response back to client
- Improves performance and security by separating concerns
Full Transcript
This visual trace shows how Nginx acts as a reverse proxy for a Django application. The client sends an HTTP request to Nginx, which listens on port 80. Nginx forwards the request to the Django app running on localhost port 8000. Django processes the request and sends back a response. Nginx then sends this response to the client, which renders the page. Variables like request URL and states of Nginx, Django, and client change step-by-step. Key moments include understanding why Nginx forwards requests, when the client receives the response, and the importance of correct port configuration. The quiz tests understanding of Nginx's role at different steps and configuration impact.

Practice

(1/5)
1. What is the main role of Nginx when used as a reverse proxy for a Django application?
easy
A. Replace the Django application with a static website
B. Directly execute Django Python code
C. Forward client requests to the Django application server
D. Store the Django database backups

Solution

  1. Step 1: Understand Nginx reverse proxy function

    Nginx acts as a middleman that receives client requests and forwards them to the Django app server.
  2. Step 2: Identify what Nginx does not do

    Nginx does not replace Django, execute Python code, or store backups; it only forwards requests and can serve static files.
  3. Final Answer:

    Forward client requests to the Django application server -> Option C
  4. Quick Check:

    Nginx forwards requests [OK]
Hint: Nginx forwards requests to Django, it doesn't run Django code [OK]
Common Mistakes:
  • Thinking Nginx runs Django code
  • Confusing Nginx with a database
  • Assuming Nginx replaces Django app
2. Which Nginx configuration snippet correctly sets it as a reverse proxy forwarding requests to a Django app running on port 8000?
easy
A. location / { proxy_pass http://127.0.0.1:8000; }
B. location / { root /var/www/html; }
C. location / { fastcgi_pass 127.0.0.1:8000; }
D. location / { proxy_redirect off; }

Solution

  1. Step 1: Identify correct proxy directive

    To forward requests, Nginx uses proxy_pass with the Django app address and port.
  2. Step 2: Check other options for correctness

    root serves static files, fastcgi_pass is for FastCGI apps, and proxy_redirect controls redirects but doesn't forward requests.
  3. Final Answer:

    location / { proxy_pass http://127.0.0.1:8000; } -> Option A
  4. Quick Check:

    Use proxy_pass for reverse proxy [OK]
Hint: proxy_pass forwards requests to backend server [OK]
Common Mistakes:
  • Using root instead of proxy_pass for proxying
  • Confusing fastcgi_pass with proxy_pass
  • Omitting the backend address in proxy_pass
3. Given this Nginx config snippet, what happens when a client requests /static/css/style.css?
location /static/ {
    alias /home/user/myproject/static/;
}
location / {
    proxy_pass http://127.0.0.1:8000;
}
medium
A. Nginx serves the static file directly from /home/user/myproject/static/css/style.css
B. Nginx forwards the request to Django app on port 8000
C. Nginx returns a 404 error because alias is incorrect
D. Nginx redirects the request to /static/css/style.css on Django

Solution

  1. Step 1: Understand alias directive for static files

    The location /static/ block uses alias to serve files directly from the filesystem path.
  2. Step 2: Analyze request routing

    Requests to /static/ are served by Nginx from the alias path; other requests go to Django via proxy_pass.
  3. Final Answer:

    Nginx serves the static file directly from /home/user/myproject/static/css/style.css -> Option A
  4. Quick Check:

    Static files served by alias [OK]
Hint: alias serves static files directly, proxy_pass forwards others [OK]
Common Mistakes:
  • Thinking all requests go to Django
  • Confusing alias with root directive
  • Assuming Nginx redirects instead of serving static files
4. You configured Nginx as a reverse proxy for Django but get a 502 Bad Gateway error. Which fix is most likely correct?
medium
A. Change Nginx config to use root instead of proxy_pass
B. Start the Django app server on the port Nginx proxies to
C. Remove the location block for static files
D. Disable firewall on the client machine

Solution

  1. Step 1: Understand 502 Bad Gateway cause

    502 means Nginx cannot connect to the backend server, often because it is not running or listening on the expected port.
  2. Step 2: Identify the correct fix

    Starting the Django app server on the port Nginx expects resolves the connection issue.
  3. Final Answer:

    Start the Django app server on the port Nginx proxies to -> Option B
  4. Quick Check:

    502 error means backend not reachable [OK]
Hint: 502 means backend server not running or unreachable [OK]
Common Mistakes:
  • Changing root instead of fixing backend server
  • Removing static files block unrelated to 502
  • Disabling client firewall won't fix server connection
5. You want Nginx to serve static files directly and forward all other requests to Django on port 8000. Which combined Nginx config snippet achieves this correctly?
hard
A. location /static/ { proxy_pass http://127.0.0.1:8000; } location / { alias /var/www/myproject/static/; }
B. location / { alias /var/www/myproject/static/; } location /static/ { proxy_pass http://127.0.0.1:8000; }
C. location /static/ { root /var/www/myproject/static/; } location / { proxy_redirect http://127.0.0.1:8000; }
D. location /static/ { alias /var/www/myproject/static/; } location / { proxy_pass http://127.0.0.1:8000; }

Solution

  1. Step 1: Serve static files with alias

    The location /static/ block uses alias to serve static files from the filesystem path.
  2. Step 2: Forward other requests with proxy_pass

    The location / block forwards all other requests to Django running on port 8000 using proxy_pass.
  3. Final Answer:

    location /static/ { alias /var/www/myproject/static/; } location / { proxy_pass http://127.0.0.1:8000; } -> Option D
  4. Quick Check:

    Static files alias + proxy_pass for others [OK]
Hint: Use alias for static, proxy_pass for app requests [OK]
Common Mistakes:
  • Swapping alias and proxy_pass locations
  • Using proxy_redirect instead of proxy_pass
  • Using root instead of alias causing path issues