0
0
Flaskframework~10 mins

Nginx as reverse proxy in Flask - 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 Flask app
Flask app processes request
Flask app sends response
Nginx receives response
Nginx sends response back to client
The client sends a request to Nginx, which forwards it to the Flask app. Flask processes and replies, then Nginx sends the response back to the client.
Execution Sample
Flask
server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:5000;
    }
}
Nginx listens on port 80 and forwards all requests to the Flask app running on localhost port 5000.
Execution Table
StepActionRequest/ResponseSourceDestination
1Client sends HTTP GET /GET /ClientNginx
2Nginx receives requestGET /NginxNginx
3Nginx forwards requestGET /NginxFlask app (127.0.0.1:5000)
4Flask app processes requestGET /Flask appFlask app
5Flask app sends response200 OK with contentFlask appNginx
6Nginx receives response200 OK with contentNginxNginx
7Nginx sends response back200 OK with contentNginxClient
8Client receives response200 OK with contentClientClient
💡 Request cycle completes after client receives the response.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
Request URL////
Request SourceClientNginxFlask appClient
Response StatusNoneNone200 OK200 OK
Key Moments - 3 Insights
Why does Nginx forward the request to 127.0.0.1:5000?
Because the proxy_pass directive tells Nginx to send incoming requests to the Flask app running locally on port 5000, as shown in execution_table step 3.
Does the client communicate directly with the Flask app?
No, the client only talks to Nginx. Nginx acts as a middleman forwarding requests and responses, as seen in execution_table steps 1, 3, and 7.
What happens if the Flask app is down?
Nginx will fail to get a response and usually returns an error to the client. This is because Nginx depends on Flask to handle the request after forwarding it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Nginx forward the request to Flask?
AStep 1
BStep 3
CStep 5
DStep 7
💡 Hint
Check the 'Action' column for forwarding requests in execution_table row 3.
According to variable_tracker, what is the Response Status after Flask app processes the request?
ANone
B404 Not Found
C200 OK
D500 Internal Server Error
💡 Hint
Look at the 'Response Status' row after Step 5 in variable_tracker.
If Nginx did not send the response back to the client, which step would be missing in execution_table?
AStep 7
BStep 2
CStep 4
DStep 6
💡 Hint
Step 7 shows Nginx sending the response back to the client.
Concept Snapshot
Nginx as reverse proxy:
- Nginx listens on a public port (e.g., 80)
- Forwards client requests to Flask app (proxy_pass)
- Flask processes and replies
- Nginx sends Flask's response back to client
- Keeps client unaware of Flask's internal address
- Improves security and load management
Full Transcript
This visual trace shows how Nginx acts as a reverse proxy for a Flask app. The client sends a request to Nginx, which listens on port 80. Nginx forwards the request to the Flask app running locally on port 5000. The Flask app processes the request and sends back a response. Nginx receives this response and sends it back to the client. Variables like request URL and response status change as the request moves through these steps. Key points include that the client never talks directly to Flask, and Nginx manages the communication. If Flask is down, Nginx cannot fulfill the request. This setup helps keep the Flask app hidden and secure behind Nginx.