0
0
Djangoframework~10 mins

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

Choose your learning style9 modes available
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.