uwsgi 配置解析

Flask + uwsgi

简要说明

  1. wsgi 是一个python中的 web协议
  2. 如何使用wsgi写出一个简单的服务器
  3. flask 自带 一个简单的wsgi服务,使用werkzeug 库,但性能很差
    在这里插入图片描述
  4. 使用uwsgi 能够提供足够的并发性能,且不占用过多的系统资源

主要配置

  1. 访问官网
  2. 项目路径如下(使用linux tree显示)
project/
|-- manage.py
`-- scripts
    |-- uwsgi.ini
    |-- uwsgi.log
    `-- uwsgi.pid

  1. 配置见下
[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

配置简要介绍

配置分为基础配置,优化配置

  1. uwsgi 支持直接使用命令行运行,命令行支持的参数,配置文件也支持
  2. 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
......
  1. 主要使用 ini 格式的配置,uwsgi配置使用 ; 作为注释
[uwsgi]
; 注释
# 注释无效

基础配置(保证基础运行)

  • 基础配置(配置项目路径,启动方式)
[uwsgi]
http=0.0.0.0:9000
pythonpath=/root/project/
module=manage
wsgi-file=/root/project/manage.py
callable=app
  1. 绑定ip 和端口,有两种基础方式,http 和 socket
    1. socket 模式,一般与nginx apache 等http服务器进行搭配使用,无法直接访问
    2. http 模式,可直接作为http服务器进行使用
  2. 以下是项目整体路径
project/
|-- manage.py
`-- scripts
    |-- uwsgi.ini
    |-- uwsgi.log
    `-- uwsgi.pid

  1. pythonpath 是整个项目的项目路径,一般是文件夹路径
  2. module 是项目的启动文件名称
  3. wsgi-file 可直接使用启动文件路径,也可作为拓展,单独写一个wsgi文件
  4. callable 是项目启动对象的名称

优化配置,平衡性能和资源消耗

processes=10
threads=10
listen=100
daemonize=/root/project/scripts/uwsgi.log
pidfile=/root/project/scripts/uwsgi.pid
http-timeout=100
  1. processes 启动的进程数
  2. threads 启动的线程数
  3. listen最大监听请求数, 大一点比较好, 与linux中的内核的socket数有关
  4. daemonize / logto 都是日志文件的路径,但daemonize可以使得uwsgi后台运行
  5. pidfile保存 uwsgi 的pid,用于停止或重启
  6. http-timeout http 请求超时时间

uwsgi 如何修改日志输出

  1. 使用 log-format 进行控制
  2. 默认输出格式
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))
  1. 可用参数
参数名说明示例
%(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 code200 / 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 requestFri Mar 13 17:11:52 2020
%(epoch)the current time in Unix formatunix 时间格式
%(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)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值