Getting Started with Flask Deployment: Gunicorn and Nginx Configuration Tutorial

This article introduces a solution for deploying Flask applications in a production environment: since Flask's built-in development server does not support high concurrency and is insecure, it needs to be paired with Gunicorn (a WSGI server) and Nginx (a reverse proxy). The steps are as follows: First, install Gunicorn (`pip install gunicorn`) and Nginx (for Ubuntu/Debian, use `apt install nginx`). When starting Gunicorn, use `gunicorn -w 4 -b 127.0.0.1:8000 app:app` (where `-w` sets the number of workers, `-b` binds the address and port, and `app:app` specifies the application entry). Next, configure Nginx by creating a configuration file (e.g., `flask_app`), set up reverse proxy to Gunicorn (`proxy_pass`), and handle static resources (`location /static`). Enable the configuration and restart Nginx. For verification, access `http://localhost` or test with `curl`. For advanced use, configure Gunicorn to start automatically on boot via systemd. Common issues include port occupation and incorrect static file paths, which require checking Gunicorn status or firewall rules. The core is running the application through Gunicorn and using Nginx.

Read More