Documentation Index
Fetch the complete documentation index at: https://mintlify.com/pallets/flask/llms.txt
Use this file to discover all available pages before exploring further.
Overview
WSGI (Web Server Gateway Interface) servers are essential for running Flask applications in production. This guide covers the most popular WSGI servers and their configurations.Gunicorn
Gunicorn (Green Unicorn) is a pure Python WSGI server that’s simple to configure and widely used in production.Features
- Pure Python implementation (easy installation)
- Simple configuration
- Multiple worker types for performance tuning
- Built-in async support with gevent
- Integrates easily with hosting platforms
- Does not support Windows (but works on WSL)
Installation
Create a virtual environment and install Gunicorn:Basic Usage
The syntax for running Gunicorn is{module_import}:{app_variable}:
Configuration Options
Worker Processes The-w or --workers option specifies the number of worker processes:
(CPU cores * 2) + 1
Binding Address
Async Workers with gevent
For applications with many concurrent, long-running connections:Requires
greenlet>=1.0. When using PyPy, requires PyPy>=7.3.7.Configuration File
Creategunicorn.conf.py for more complex configurations:
uWSGI
uWSGI is a fast, compiled server suite with extensive configuration options.Features
- High performance (compiled C program)
- Extensive feature set
- Multiple protocols support
- Built-in caching
- Complex configuration
- Does not support Windows (but works on WSL)
- May require compiler for installation
Installation
Option 1: Pre-compiled wheels (no SSL)Basic Usage
Direct application import:wsgi.py file:
Configuration Options
--http- Start HTTP server at specified address--master- Enable master process-por--processes- Number of worker processes-w- Module and application to load
INI Configuration File
Createuwsgi.ini for persistent configuration:
Async with gevent
Integration with Nginx
uWSGI has optimized integration with Nginx using the uwsgi protocol:Waitress
Waitress is a pure Python WSGI server that’s easy to configure and supports Windows.Features
- Pure Python (easy installation)
- Windows support
- Easy configuration
- Single process with multiple threads
- No streaming request support (buffers full request)
Installation
Basic Usage
The syntax is{module}:{app}:
Configuration Options
Python Configuration
For more control, use the Python API:mod_wsgi
mod_wsgi integrates directly with Apache httpd server.Features
- Tight Apache integration
- Windows support
- No separate reverse proxy needed
- Requires compiler and Apache development headers
Installation
Installation requires Apache httpd and development headers. On Ubuntu/Debian:
sudo apt-get install apache2-devRunning with mod_wsgi-express
Createwsgi.py with an application variable:
Binding to Privileged Ports
Unlike other WSGI servers, mod_wsgi can run as root and drop privileges:Comparison Table
| Feature | Gunicorn | uWSGI | Waitress | mod_wsgi |
|---|---|---|---|---|
| Platform | Unix/Linux | Unix/Linux | Cross-platform | Cross-platform |
| Installation | Easy | Moderate | Easy | Complex |
| Performance | Good | Excellent | Good | Good |
| Configuration | Simple | Complex | Simple | Moderate |
| Worker Types | Sync, Async | Sync, Async, Threaded | Threaded | Sync, Threaded |
| Best For | General production | High-performance | Windows, simple apps | Apache infrastructure |
Best Practices
Choose the Right Worker Count
Choose the Right Worker Count
Start with
(CPU cores * 2) + 1 workers and adjust based on your application’s characteristics:- CPU-bound: Use fewer workers (1-2 per core)
- I/O-bound: Use more workers or async workers
- Mixed: Start with the formula above
Use Timeouts
Use Timeouts
Set appropriate timeouts to prevent workers from hanging:
Enable Access Logging
Enable Access Logging
Enable access logs for monitoring and debugging:
Graceful Reloads
Graceful Reloads
Use graceful reloads to deploy updates without downtime:
Next Steps
Production Configuration
Learn production best practices, security, and monitoring
Deployment Overview
Return to the deployment overview
