Flask + uwsgi
简要说明
- wsgi 是一个python中的 web协议
- 如何使用wsgi写出一个简单的服务器
- flask 自带 一个简单的wsgi服务,使用werkzeug 库,但性能很差
- 使用uwsgi 能够提供足够的并发性能,且不占用过多的系统资源
主要配置
- 访问官网
- 项目路径如下(使用linux tree显示)
project/
|-- manage.py
`-- scripts
|-- uwsgi.ini
|-- uwsgi.log
`-- uwsgi.pid
- 配置见下
[uwsgi]
http=0.0.0.0:9000
pythonpath=/root/project/
module=manage
wsgi-file=/root/project/manage.py
callable=app
processes=10
threads=10
listen=100
daemonize=/root/project/scripts/uwsgi.log
pidfile=/root/project/scripts/uwsgi.pid
http-timeout=100
master=true
logformat-strftime=true
log-date=%%Y-%%m-%%d %%H:%%M:%%S
log-format=[%(ftime)] pid: %(pid) %(addr) => host: %(host)%(uri)(%(method)) in %(secs)s %(status) total-size: %(size) bytes
配置简要介绍
配置分为基础配置,优化配置
- uwsgi 支持直接使用命令行运行,命令行支持的参数,配置文件也支持
uwsgi --help
, 忘记配置的时候可以使用帮助进行查看
Usage: /usr/local/bin/uwsgi [options...]
-s|--socket bind to the specified UNIX/TCP socket using default protocol
-s|--uwsgi-socket bind to the specified UNIX/TCP socket using uwsgi protocol
--http-socket bind to the specified UNIX/TCP socket using HTTP protocol
--http-socket-modifier1 force the specified modifier1 when using HTTP protocol
--http-socket-modifier2 force the specified modifier2 when using HTTP protocol
--http11-socket bind to the specified UNIX/TCP socket using HTTP 1.1 (Keep-Alive) protocol
--fastcgi-socket bind to the specified UNIX/TCP socket using FastCGI protocol
--fastcgi-nph-socket bind to the specified UNIX/TCP socket using FastCGI protocol (nph mode)
--fastcgi-modifier1 force the specified modifier1 when using FastCGI protocol
--fastcgi-modifier2 force the specified modifier2 when using FastCGI protocol
--scgi-socket bind to the specified UNIX/TCP socket using SCGI protocol
--scgi-nph-socket bind to the specified UNIX/TCP socket using SCGI protocol (nph mode)
--scgi-modifier1 force the specified modifier1 when using SCGI protocol
--scgi-modifier2 force the specified modifier2 when using SCGI protocol
--raw-socket bind to the specified UNIX/TCP socket using RAW protocol
--raw-modifier1 force the specified modifier1 when using RAW protocol
......
- 主要使用 ini 格式的配置,uwsgi配置使用
;
作为注释
[uwsgi]
; 注释
# 注释无效
基础配置(保证基础运行)
- 基础配置(配置项目路径,启动方式)
[uwsgi]
http=0.0.0.0:9000
pythonpath=/root/project/
module=manage
wsgi-file=/root/project/manage.py
callable=app
- 绑定ip 和端口,有两种基础方式,http 和 socket
- socket 模式,一般与nginx apache 等http服务器进行搭配使用,无法直接访问
- http 模式,可直接作为http服务器进行使用
- 以下是项目整体路径
project/
|-- manage.py
`-- scripts
|-- uwsgi.ini
|-- uwsgi.log
`-- uwsgi.pid
- pythonpath 是整个项目的项目路径,一般是文件夹路径
- module 是项目的启动文件名称
- wsgi-file 可直接使用启动文件路径,也可作为拓展,单独写一个wsgi文件
- callable 是项目启动对象的名称
优化配置,平衡性能和资源消耗
processes=10
threads=10
listen=100
daemonize=/root/project/scripts/uwsgi.log
pidfile=/root/project/scripts/uwsgi.pid
http-timeout=100
processes
启动的进程数threads
启动的线程数listen
最大监听请求数, 大一点比较好, 与linux中的内核的socket数有关daemonize / logto
都是日志文件的路径,但daemonize可以使得uwsgi后台运行pidfile
保存 uwsgi 的pid,用于停止或重启http-timeout
http 请求超时时间
uwsgi 如何修改日志输出
- 使用 log-format 进行控制
- 默认输出格式
log-format = [pid: %(pid)|app: -|req: -/-] %(addr) (%(user)) {%(vars)
vars in %(pktsize) bytes} [%(ctime)] %(method) %(uri) => generated
%(rsize) bytes in %(msecs) msecs (%(proto) %(status)) %(headers)
headers in %(hsize) bytes (%(switches) switches on core %(core))
- 可用参数
参数名 | 说明 | 示例 |
---|---|---|
%(uri) | REQUEST_URI | 访问完整路径 |
%(method) | REQUEST_METHOD | 请求方法 GET / POST |
%(user) | REMOTE_USER | 远程访问用户 |
%(addr) | REMOTE_ADDR | 远程访问地址 |
%(host) | HTTP_HOST | 服务地址 |
%(proto) | SERVER_PROTOCOL | 服务的协议 |
%(uagent) | HTTP_USER_AGENT (starting from 1.4.5) | http agent |
%(referer) | HTTP_REFERER (starting from 1.4.5) | |
%(status) | HTTP response status code | 200 / 404 |
%(micros) | response time in microseconds | 处理时间 微秒 |
%(msecs) | response time in milliseconds | 毫秒 |
%(secs) | response time in seconds | 秒 |
%(time) | timestamp of the start of the request | 开始时间 |
%(ctime) | ctime of the start of the request | Fri Mar 13 17:11:52 2020 |
%(epoch) | the current time in Unix format | unix 时间格式 |
%(size) | response body size + response headers size (since 1.4.5) | 请求字节大小 |
%(ltime) | human-formatted (Apache style) request time (since 1.4.5) | 请求时间 |
%(hsize) | response headers size (since 1.4.5) | 响应头大小 |
%(rsize) | response body size (since 1.4.5) | 响应体大小 |
%(cl) | request content body size (since 1.4.5) | 请求体大小 |
%(pid) | pid of the worker handling the request (since 1.4.6) | 处理进程号 |
%(wid) | id of the worker handling the request (since 1.4.6) | 请求 work_id |
%(switches) | number of async switches (since 1.4.6) | |
%(vars) | number of CGI vars in the request (since 1.4.6) | |
%(headers) | number of generated response headers (since 1.4.6) | |
%(core) | the core running the request (since 1.4.6) | |
%(vsz) | address space/virtual memory usage (in bytes) (since 1.4.6) | |
%(rss) | RSS memory usage (in bytes) (since 1.4.6) | |
%(vszM) | address space/virtual memory usage (in megabytes) (since 1.4.6) | |
%(rssM) | RSS memory usage (in megabytes) (since 1.4.6) | |
%(pktsize) | size of the internal request uwsgi packet (since 1.4.6) | |
%(modifier1) | modifier1 of the request (since 1.4.6) | |
%(modifier2) | modifier2 of the request (since 1.4.6) | |
%(metric.XXX) | access the XXX metric value (see The Metrics subsystem) | |
%(rerr) | number of read errors for the request (since 1.9.21) | |
%(werr) | number of write errors for the request (since 1.9.21) | |
%(ioerr) | number of write and read errors for the request (since 1.9.21) | |
%(tmsecs) | timestamp of the start of the request in milliseconds since the epoch (since 1.9.21) | |
%(tmicros) | timestamp of the start of the request in microseconds since the epoch (since 1.9.21) | |
%(var.XXX) | the content of request variable XXX (like var.PATH_INFO or var.HTTP_X_MY_HEADER for headers from request, available from 1.9.21) |