Bird
Raised Fist0
Flaskframework~30 mins

Nginx as reverse proxy in Flask - Mini Project: Build & Apply

Choose your learning style10 modes available
Nginx as Reverse Proxy for Flask App
📖 Scenario: You want to make your Flask web app accessible through a friendly web address and improve security by using Nginx as a reverse proxy. This means Nginx will receive web requests and forward them to your Flask app running on a local port.
🎯 Goal: Set up a simple Flask app and configure Nginx to act as a reverse proxy that forwards requests to the Flask app.
📋 What You'll Learn
Create a basic Flask app with one route
Define the port number where Flask app will run
Write the Nginx server block configuration to proxy requests to Flask
Include the final Nginx configuration with proxy settings
💡 Why This Matters
🌍 Real World
Using Nginx as a reverse proxy is common in web hosting to improve security, performance, and manageability of web applications.
💼 Career
Many web developer and DevOps roles require knowledge of configuring Nginx to serve and proxy web applications like Flask.
Progress0 / 4 steps
1
Create a basic Flask app
Create a Flask app in a file called app.py. Import Flask from flask, create an app instance called app, and define a route / that returns the text "Hello from Flask!".
Flask
Hint

Remember to import Flask, create the app, and use the @app.route decorator for the home page.

2
Set the Flask app port
Add code to run the Flask app on port 5000 by calling app.run(host='127.0.0.1', port=5000) inside the if __name__ == '__main__': block.
Flask
Hint

Use the if __name__ == '__main__': guard to run the app on localhost port 5000.

3
Write Nginx reverse proxy config
Create an Nginx server block configuration that listens on port 80 and proxies all requests to http://127.0.0.1:5000. Use proxy_pass http://127.0.0.1:5000; inside the location / block.
Flask
Hint

Use proxy_pass inside location / to forward requests to Flask.

4
Complete Nginx config with proxy headers
Add the proxy headers proxy_set_header Host $host;, proxy_set_header X-Real-IP $remote_addr;, proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, and proxy_set_header X-Forwarded-Proto $scheme; inside the location / block to complete the Nginx reverse proxy configuration.
Flask
Hint

These headers help pass client info to the Flask app through Nginx.

Practice

(1/5)
1. What is the main role of Nginx when used as a reverse proxy for a Flask application?
easy
A. Compile Flask Python code into machine code
B. Run the Flask application code directly
C. Forward client requests to the Flask app server
D. Store the Flask app's database

Solution

  1. Step 1: Understand Nginx's role

    Nginx acts as a middleman that receives client requests first.
  2. Step 2: Forward requests to Flask app

    Nginx sends these requests to the Flask app running on a backend server or port.
  3. Final Answer:

    Forward client requests to the Flask app server -> Option C
  4. Quick Check:

    Nginx forwards requests = D [OK]
Hint: Nginx passes requests, it does not run Flask code [OK]
Common Mistakes:
  • Thinking Nginx runs Flask code
  • Confusing Nginx with database storage
  • Believing Nginx compiles Python code
2. Which Nginx directive is used to specify the Flask app server URL in the reverse proxy configuration?
easy
A. proxy_pass
B. server_name
C. listen
D. root

Solution

  1. Step 1: Identify directive for backend URL

    The directive that tells Nginx where to send requests is proxy_pass.
  2. Step 2: Confirm other directives' roles

    server_name sets domain, listen sets port, root sets file path.
  3. Final Answer:

    proxy_pass -> Option A
  4. Quick Check:

    Backend URL = proxy_pass [OK]
Hint: proxy_pass points to Flask app URL in Nginx config [OK]
Common Mistakes:
  • Using server_name instead of proxy_pass for backend URL
  • Confusing listen with proxy_pass
  • Setting root for proxy backend
3. Given this Nginx snippet, what happens when a client requests http://example.com/api/data?
location /api/ {
    proxy_pass http://127.0.0.1:5000/;
}
medium
A. Request is served from Nginx's local files
B. Request is forwarded to http://127.0.0.1:5000/data
C. Request results in 404 error
D. Request is forwarded to http://127.0.0.1:5000/api/data

Solution

  1. Step 1: Understand proxy_pass with trailing slash

    Since proxy_pass ends with a slash, Nginx appends the full request URI after the location prefix.
  2. Step 2: Combine location and proxy_pass

    Request /api/data is forwarded as http://127.0.0.1:5000/api/data.
  3. Final Answer:

    Request is forwarded to http://127.0.0.1:5000/api/data -> Option D
  4. Quick Check:

    Trailing slash keeps full path = C [OK]
Hint: Trailing slash in proxy_pass keeps full path [OK]
Common Mistakes:
  • Removing /api/ from forwarded URL incorrectly
  • Assuming Nginx serves files locally
  • Expecting 404 without backend
4. You configured Nginx as below but get a 502 Bad Gateway error. What is the likely cause?
location / {
    proxy_pass http://127.0.0.1:8000;
}
Your Flask app runs on port 5000.
medium
A. Nginx is forwarding to wrong port 8000 instead of 5000
B. proxy_pass missing trailing slash causes error
C. Flask app must run on port 8000 for Nginx
D. Nginx cannot proxy to localhost

Solution

  1. Step 1: Check proxy_pass target port

    proxy_pass points to port 8000, but Flask runs on 5000, so no backend listens on 8000.
  2. Step 2: Understand 502 Bad Gateway cause

    502 means Nginx can't connect to backend server at specified address and port.
  3. Final Answer:

    Nginx is forwarding to wrong port 8000 instead of 5000 -> Option A
  4. Quick Check:

    Port mismatch causes 502 error = A [OK]
Hint: Match proxy_pass port to Flask app port exactly [OK]
Common Mistakes:
  • Blaming missing trailing slash for 502
  • Thinking Flask must run on 8000
  • Assuming Nginx can't proxy localhost
5. You want Nginx to forward requests from /app/ to your Flask app at http://127.0.0.1:5000/, but remove the /app prefix before forwarding. Which configuration achieves this?
hard
A.
location /app/ {
    proxy_pass http://127.0.0.1:5000/app/;
}
B.
location /app/ {
    proxy_pass http://127.0.0.1:5000;
}
C.
location /app/ {
    proxy_pass http://127.0.0.1:5000/;
}
D.
location /app {
    proxy_pass http://127.0.0.1:5000/;
}

Solution

  1. Step 1: Understand proxy_pass without trailing slash

    When proxy_pass URL has no trailing slash, Nginx replaces the matching location prefix with the proxy_pass path.
  2. Step 2: Apply to /app/ location

    With location /app/ and proxy_pass http://127.0.0.1:5000, the /app/ prefix is removed before forwarding.
  3. Step 3: Check other options

    The configuration with proxy_pass http://127.0.0.1:5000/; keeps /app/ prefix, the one with proxy_pass http://127.0.0.1:5000/app/; adds /app/ twice, and location without trailing slash causes mismatch.
  4. Final Answer:

    location /app/ { proxy_pass http://127.0.0.1:5000; } -> Option B
  5. Quick Check:

    proxy_pass without slash removes location prefix = A [OK]
Hint: No trailing slash on proxy_pass removes location prefix [OK]
Common Mistakes:
  • Adding trailing slash keeps prefix in forwarded URL
  • Using location without trailing slash causes mismatch
  • Duplicating /app/ in proxy_pass URL
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard