Flask interview question · Most asked
How do you deploy a Flask app to production?
Summary
Run it with a production WSGI server such as Gunicorn, uWSGI or Waitress (never the development server), behind a reverse proxy like Nginx that handles TLS, static files and buffering, typically in containers.
Longer Answer
Gunicorn: gunicorn -w 4 -b 0.0.0.0:8000 'myapp:create_app()'. Choose workers based on CPU and workload (sync workers for CPU/simple apps; gevent workers or threads for I/O-bound apps). Use ProxyFix so request.remote_addr and URL scheme are correct behind proxies. Set debug=False and a strong SECRET_KEY.
Code Example
from werkzeug.middleware.proxy_fix import ProxyFix
app = create_app()
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
# gunicorn -w 4 --threads 2 -b 0.0.0.0:8000 'wsgi:app'
Key Points
- Gunicorn/uWSGI/Waitress, never
flask runin prod - Nginx for TLS/static files
ProxyFixbehind proxies- Worker type matches workload
More most-asked Flask questions
- How do you access request data and build responses in Flask?
- How do Flask sessions work and what are their security implications?
- What is the application factory pattern in Flask and why use it?
- What is Flask and what does 'microframework' mean?
- Explain Flask's application context and request context.
- What are Blueprints in Flask?
- How does routing work in Flask, including variable rules and HTTP methods?
- How do you use SQLAlchemy with Flask (Flask-SQLAlchemy 3.x)?