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 run in prod
  • Nginx for TLS/static files
  • ProxyFix behind proxies
  • Worker type matches workload

More most-asked Flask questions

← All Flask questions