uWSGI 使用介绍

发布时间: 更新时间: 总字数:1244 阅读时间:3m 作者: IP上海 分享 网址

uWSGI(Web Server Gateway Interface)服务器实现,通常用于运行Python Web应用程序。

介绍

  • uWSGI 实现了 WSGI、uWSGI、HTTP 等协议
  • 常用于Python应用程序,如Django或Flask等
  • 代理方式
Client <--> Nginx <--> uWSGI <--> Python(Django, Flask, ...)

安装

pip

pip install uwsgi
pip install uwsgi -i https://mirrors.aliyun.com/pypi/simple/

安装脚本

curl http://uwsgi.it/install | bash -s default /usr/local/bin/uwsgi

源代安装

wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
tar zxvf uwsgi-latest.tar.gz
cd uwsgi-latest
make

使用

hello world

  • hello.py
$ cat hello.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World!"]
  • 启动服务,uWSGI 默认加载 application 函数,监听在 8000 端口
# 默认启动一个进程,每个进程一个线程
uwsgi --http :8000 --wsgi-file hello.py

# 启动 4 个进程,每个进程 2 个线程
uwsgi --http :8000 --wsgi-file hello.py --master --processes 4 --threads 2

启动监控任务

# 安装 uwsgitop
pip install uwsgitop

# 启动服务
uwsgi --http :8000 --wsgi-file hello.py --master --processes 4 --threads 2 --stats 127.0.0.1:8001

# 使用 uwsgitop 查看监控数据
uwsgitop --frequency 3 127.0.0.1:8001

监听方式

  • --http HTTP 协议
  • --socket TCP socket 协议
  • --http-socket unix socket 协议

Nginx 代理配置

location / {
    include /etc/nginx/uwsgi_params;
    # uwsgi_pass unix:/run/uwsgi/project.sock;
    uwsgi_pass 127.0.0.1:8000;
    # proxy_pass http://uWSGI_SERVER_IP:8000;
}

部署 Django

  • app.ini
$ cat app.ini
[uwsgi]
socket = 127.0.0.1:8000
# 指定 django 项目路径
chdir = /data/django-foo/
wsgi-file = django-foo/wsgi.py  # module?
processes = 4
threads = 2
stats = 127.0.0.1:8001
  • 启动服务
uwsgi --ini app.ini

部署 Flask

$ cat main.py
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'
  • app.ini
$ cat app.ini
[uwsgi]
socket = 127.0.0.1:8000
# 指定 django 项目路径
chdir = /data/flask-foo/
wsgi-file = main.py
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:8001
  • 启动服务
uwsgi --ini app.ini

配置文件示例

[uwsgi]
# Ubuntu系统下默认用户名
uid = www-data
gid = www-data
# 项目名
project = bar1
# 项目根目录
base = /data/
home = /data/your_project/your_module

# Python 虚拟环境
virtualenv = %(base)/%(project).venv

# 设置工作目录
chdir = %(base)/%(project)

# wsgi文件位置
module = %(project).wsgi:application

# 模块文件名
module = manage.py
# 模块中的应用对象,如 Flask
callable = app

# wsgi-file = main.py

# 主进程
master = true

# 进程数
processes = 2

# 每个进程启动的线程数
threads = 2

# 允许在请求中开启新线程
enable-threads = true

# 服务监听方式,任选一种
# 方式一:使用 unix socket 与 nginx 通信,仅支持 uwsgi 和 nginx 运行在同一主机,与 nginx 配置的 uwsgi_pass 应指向同一 socket 文件
socket = /run/uwsgi/%(project).sock

# 方式二:使用 TCP socket 与 nginx 通信,nginx 配置的 uwsgi_pass 为 ip:port
# socket = 0.0.0.0:8000
# 或
# socket = :8000

# 方式三:使用 http 协议与 nginx 通信,nginx 配置的 proxy_pass 为 ip:port
# http = 0.0.0.0:8000

# socket 权限设置
chown-socket = %(uid):%(gid)
chmod-socket = 777

# 进程文件
pidfile = /tmp/%(project)-master.pid

# 日志输出到文件
logto = /var/log/uwsgi.log
log-maxsize = 102400

# 记录request日志
disable-logging = false

# 以后台守护进程运行
daemonize = /var/log/%(project).log

# 服务停止时,自动移除 unix socket 和 pid 文件
vacuum = True

# 设置工作进程每处理N个进程就会被回收重启
max-requests = 5000

# worker进程工作时长
max-worker-lifetime = 3600

# 设置平滑的重启(直到处理完接收到的请求)的长等待时间(秒)
worker-reload-mercy = 60

# 当一个请求花费的时间超过这个时间,则丢弃该请求
harakiri = 60

# 当一个请求被 harakiri 杀掉会输出一条日志
harakiri-verbose = true

# uWsgi 默认的 buffersize 为 4096,如果请求数据超过这个量会报错,示例为 64k
buffer-size = 65536

# 如果 http 请求体的大小超过指定的限制,打开 http body 缓冲,示例为 64k
post-buffering = 65536

# 开启内存使用情况报告
memory-report = true

# 设置平滑的重启(直到处理完接收到的请求)的长等待时间(秒)
reload-mercy = 10

# 设置工作进程使用虚拟内存超过N MB就回收重启
reload-on-as = 1024

# 设置工作进程使用物理内存超过N MB就回收重启
reload-on-rss = 1024
;evil-reload-on-as = 512
;evil-reload-on-rss = 512

# timeout: http 和 socket
http-timeout = 30
socket-timeout = 30

stop 服务

uwsgi --stop <uwsgi pid file>

reload 服务

uwsgi --reload <uwsgi pid file>
uwsgi --reload /tmp/%(project)-master.pid

多 Thread

启动多 Thread 的方式,选其一即可

  • master = false参考
  • enable-threads = true

F&Q

安装报错 Exception: you need a C compiler to build uWSGI

环境缺少 gcc 依赖

解决方式:

yum install gcc

容器中 uWSGI listen queue of socket full

将 listen 该大些

[uwsgi]
listen = 500

获取文件失败

采用 nginx 代理即可。

扩展

  • Daphne 是一个用于UNIX的纯Python ASGI服务器,由Django项目的成员维护。它充当了ASGI的参考服务器。

参考

  1. https://uwsgi-docs.readthedocs.io/en/latest/
  2. https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数