0
0
Flaskframework~30 mins

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

Choose your learning style9 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
Need a 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
Need a 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
Need a 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
Need a hint?

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