waitress 是一个纯 Python 编写的 WSGI 服务器,专为生产环境设计,可用于部署 Django、Flask 等 Python Web 应用。相比 Django 自带的开发服务器(runserver),它更安全、高效,且支持多线程处理请求。
使用命令:waitress-serve --port=8001 your_project.wsgi:application
waitress-serve
--host=0.0.0.0 # 监听所有网络接口(允许外部访问)
--port=8080 # 指定端口
--threads=8 # 设置线程数(默认4)
your_project.wsgi:application
与 Nginx/Apache 结合(生产环境)
在生产环境中,通常将 Waitress 作为后端服务器,前端使用 Nginx/Apache 作为反向代理:
Nginx 配置示例
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8080; # Waitress运行的地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
注意事项
关闭 DEBUG 模式:在生产环境中,确保settings.py中的DEBUG = False
静态文件处理:静态文件应由 Nginx/Apache 直接提供,而非 Waitress
进程管理:使用systemd(Linux)或nssm(Windows)将 Waitress 作为服务运行