uwsgi概述和配置

uwsgi概述

WSGI协议概述

  • WSGI是为Python语言定义的Web服务器和Web应用程序或框架之间的一种简单而通用的接口。
  • 这是把服务器和服务程序分开
  • uwsgi是服务器,调用python写的web应用程序或flask的python框架代码
  • WSGI协议是对http协议的封装,是基于http协议的。
    • 比如env[“REQUEST_METHOD”]就是http的method。
    • env[‘wsgi.input’]是消息体(如果是POST方法)
    • env就含义http中的所有信息

uwsgi配置

命令行配置

  • 直接使用uwsgi作为http服务器
    • uwsgi --http :9090 --wsgi-file test_wsgi.py --processes 2 --threads 3
    • 这会启动三个进程。一个http router进程,作为http服务器。两个worker进程(每个含有三个线程)
  • Do not use --http when you have a frontend webserver or you are doing some form of benchmark, use --http-socket.
    • 如果不直接作为http服务器,前面有服务器,比如有nginx作为前端服务器,就可以使用–http-socket(前端服务器使用http与uwsgi通信时)或者–socket(前端服务器直接通过tcp通信)参数。
  • 添加monitoring(监控)
    • uwsgi --http :9090 --wsgi-file test_wsgi.py --master --processes 2 --threads 2
      • 这会多启动一个master进程,去监控worker,当worker挂掉,会自动拉起worker进程。
    • uwsgi --http :9090 --wsgi-file test_wsgi.py --master --processes 2 --threads 2 --stats 127.0.0.1:9191
      • 这里同步会在监听9191端口,访问这个端口可以查看uwsgi状态信息

uwsig.ini配置文件参数详解

  • 参数多时,使用命令行不方便,可以直接使用配置文件启动uwsgi
    • uwsgi uwsgi.ini
  • uwsgi官方中文参考资料, 所有配置详解
  • uWSGI参考资料(1.0版本的配置选项列表):https://www.cnblogs.com/ddcoder/articles/7398891.html
  • uwsgi配置详解(含有stats字段解释):https://www.cnblogs.com/mensiler/p/11889546.html
  • 常用ini文件配置项
    • master = true 使用master监控进程
    • processes = 12 启动的worker进程数量
    • threads=2 每个worker进程中的线程数量
    • wsgi-file test_wsgi.py 入口文件(默认入口函数时application)。可替代的配置
      • module = test_wsgi 入口文件(不要.py,模块的形式给出,有目录的情况下,用xx.xx的这种模块形式给出)
      • callable = application 入口文件中的函数
      • 或者module = test_wsgi:application,也是一样的效果
    • http = :9090 直接使用uwsgi作为http服务器,监听的端口。
      • 单独创建一个进程去监听此http端口,作为一个http网关。不是worker去处理
    • 如果前面有前端服务器(如Nginx配置了反向代理)。可以如下配置
      • http-socket :80 (前端服务器使用http与uwsgi通信时)
      • socket /tmp/xxx.sock 前端服务器使用tcp,通过本地socket文件和前端服务器通信
    • stats = 0.0.0.0:9191 开启统计端口
    • buffer-size=65536 请求体大小限制
    • reload-on-as=1024 设置工作进程使用虚拟内存超过多少MB就回收重启
    • gevent=100 使用gevent开启协程的数量。这个参数不和threads参数同时使用
      • 利用gevent来配置uwsgi提高django项目并发量
      • gevent=100应该要和下面两个参数一起用比较好(处理请求和在请求中对发起的io事件在一个事件循环中),这样就是全异步的服务器了0.0
        • gevent-early-monkey-patch=true
        • gevent-monkey-patch=true
    • map-socket = 0:1
      • map-socket 是一个 uWSGI 配置选项,用于将特定的 uWSGI 套接字映射到一个或多个应用程序。这在你需要在同一个 uWSGI 实例中运行多个应用程序时非常有用(单应用程序也可以)。
      • map-socket = 0:1,表示 uWSGI 就会将索引为 0 的套接字映射到索引为 1 的工作进程。这种映射机制可以让你更灵活地控制哪个工作进程处理哪个套接字的请求,从而可以根据应用程序的需求来优化资源的使用。
      • uwsgi日志mapped socket 0 (/tmp/api.sock) to worker 1 是什么意思?
        • 这条日志信息表示 uWSGI 将 Unix 套接字 /tmp/api.sock 映射到了工作进程 1。
        • 在 uWSGI 中,工作进程(worker)是处理请求的实体。每个工作进程都可以独立地处理一个请求。当 uWSGI 启动时,它会创建一定数量的工作进程(这个数量可以在配置文件中设置)。
        • “mapped socket 0 (/tmp/api.sock) to worker 1” 这条日志信息的含义是,uWSGI 将索引为 0 的套接字(即 /tmp/api.sock)映射到了索引为 1 的工作进程。这意味着,当一个请求到达 /tmp/api.sock 套接字时,它将由工作进程 1 来处理。
        • 这种映射通常是基于 map-socket 配置选项进行的。例如,如果你在 uWSGI 配置文件中设置了 map-socket = 0:1,那么 uWSGI 就会将索引为 0 的套接字映射到索引为 1 的工作进程。
        • 这种映射机制可以让你更灵活地控制哪个工作进程处理哪个套接字的请求,从而可以根据应用程序的需求来优化资源的使用。
    • log-date = true
      • uwsgi日志前面加时间
    • 等等,其他的可以网上查看uwsgi配置吧。要看全量配置,可以使用uwsgi --help

uwsgi和nginx配合配置

  • Flask应用示例2 - 通过nginx+uwsgi+flask搭建web服务:https://www.jianshu.com/p/42b91d26baa4
  • nginx+uwsgi 配置的三种方式: https://www.cnblogs.com/lianghui-lianghui/articles/10415186.html
  • https://www.jianshu.com/p/2de9f3907184

uwsgi日志轮转(切割)

  • uwsgi 日志按天切割
  • 注意:uwsgi.ini修改后,使用pkill -1 uwsgi重启uwsgi没用,还是得kill -9 之后,重启执行命令启动。

uwsgi使用python版本

参考

  • https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html
  • 让uWSGI选择使用不同版本的Python:http://www.dannysite.com/blog/94/

查看uwsgi使用的python版本

  • uwsgi --python-version

uwsgi如何选择使用的python版本

  • 通过在编译uwsgi时,链接对应的python库
  • 下面是编译uwsgi过程中,链接的日志。
*** uWSGI linking ***
gcc -pthread -o uwsgi -L/usr/lib64 -Wl,-rpath,/usr/lib64 core/utils.o core/protocol.o core/socket.o core/logging.o core/master.o core/master_utils.o core/emperor.o core/notify.o core/mule.o core/subscription.o core/stats.o core/sendfile.o core/async.o core/master_checks.o core/fifo.o core/offload.o core/io.o core/static.o core/websockets.o core/spooler.o core/snmp.o core/exceptions.o core/config.o core/setup_utils.o core/clock.o core/init.o core/buffer.o core/reader.o core/writer.o core/alarm.o core/cron.o core/hooks.o core/plugins.o core/lock.o core/cache.o core/daemons.o core/errors.o core/hash.o core/master_events.o core/chunked.o core/queue.o core/event.o core/signal.o core/strings.o core/progress.o core/timebomb.o core/ini.o core/fsmon.o core/mount.o core/metrics.o core/plugins_builder.o core/sharedarea.o core/rpc.o core/gateway.o core/loop.o core/cookie.o core/querystring.o core/rb_timers.o core/transformations.o core/uwsgi.o proto/base.o proto/uwsgi.o proto/http.o proto/fastcgi.o proto/scgi.o proto/puwsgi.o lib/linux_ns.o core/zlib.o core/regexp.o core/routing.o core/yaml.o core/ssl.o core/legion.o core/dot_h.o core/config_py.o plugins/python/python_plugin.o plugins/python/pyutils.o plugins/python/pyloader.o plugins/python/wsgi_handlers.o plugins/python/wsgi_headers.o plugins/python/wsgi_subhandler.o plugins/python/web3_subhandler.o plugins/python/pump_subhandler.o plugins/python/gil.o plugins/python/uwsgi_pymodule.o plugins/python/profiler.o plugins/python/symimporter.o plugins/python/tracebacker.o plugins/python/raw.o plugins/gevent/gevent.o plugins/gevent/hooks.o plugins/ping/ping_plugin.o plugins/cache/cache.o plugins/nagios/nagios.o plugins/rrdtool/rrdtool.o plugins/carbon/carbon.o plugins/rpc/rpc_plugin.o plugins/corerouter/cr_common.o plugins/corerouter/cr_map.o plugins/corerouter/corerouter.o plugins/fastrouter/fastrouter.o plugins/http/http.o plugins/http/keepalive.o plugins/http/https.o plugins/http/spdy3.o plugins/ugreen/ugreen.o plugins/signal/signal_plugin.o plugins/syslog/syslog_plugin.o plugins/rsyslog/rsyslog_plugin.o plugins/logsocket/logsocket_plugin.o plugins/router_uwsgi/router_uwsgi.o plugins/router_redirect/router_redirect.o plugins/router_basicauth/router_basicauth.o plugins/zergpool/zergpool.o plugins/redislog/redislog_plugin.o plugins/mongodblog/mongodblog_plugin.o plugins/router_rewrite/router_rewrite.o plugins/router_http/router_http.o plugins/logfile/logfile.o plugins/router_cache/router_cache.o plugins/rawrouter/rawrouter.o plugins/router_static/router_static.o plugins/sslrouter/sslrouter.o plugins/spooler/spooler_plugin.o plugins/cheaper_busyness/cheaper_busyness.o plugins/symcall/symcall_plugin.o plugins/transformation_tofile/tofile.o plugins/transformation_gzip/gzip.o plugins/transformation_chunked/chunked.o plugins/transformation_offload/offload.o plugins/router_memcached/router_memcached.o plugins/router_redis/router_redis.o plugins/router_hash/router_hash.o plugins/router_expires/expires.o plugins/router_metrics/plugin.o plugins/transformation_template/tt.o plugins/stats_pusher_socket/plugin.o -lpthread -lm -rdynamic -ldl -lz -lpcre -lssl -lcrypto -lpthread -ldl -lutil -lm -lpython3.6m -lcrypt
################# uWSGI configuration #################

uwsgi如何使用python

  • uwsgi是c程序,使用c语言开发。uwsgi是c调用python,需要使用python的开发包,如:Python.h和libpython3.6m.so.1.0(或者应该可以使用静态库,将静态库直接编译进uwsgi里面)来创建python解释器进程,去调用py代码。
  • uwsgi使用python貌似是直接静态编译进uwsgi了。gevent(gevent是使用greenlet+libevent去使用c语言实现的python协程库)貌似需要libpython3.6m.so.1.0这种动态库。

概述

As we have seen, uWSGI is composed of a small core and various plugins. Plugins can be embedded in the binary or loaded dynamically. When you build uWSGI for Python, a series of plugins plus the Python one are embedded in the final binary.

This could be a problem if you want to support multiple Python versions without building a binary for each one.

The best approach would be having a little binary with the language-independent features built in, and one plugin for each Python version that will be loaded on-demand.

  • uWSGI是由一个小的core合各种plugin组成。plugins可以嵌入uwsgi二进制中,或者动态加载。要实现uwsgi能加载多版本python,就单独编译uwsgi core和python plugins

更多详细信息,使用 -H 参数试试

  • 17
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uWSGI是一个Web服务器,它支持多种Web服务器协议和Python应用程序的部署。uWSGI还支持多线程和异步请求处理,可以提高Web应用程序的性能。下面是uwsgi模式_uwsgi配置详解: 1. 安装uWSGI 在Linux系统中,可以使用以下命令安装uWSGI: ```bash pip install uwsgi ``` 2. 编写uWSGI配置文件 uWSGI配置文件是一个INI文件,可以指定uWSGI服务器的各种参数。以下是一个简单的uWSGI配置文件示例: ```ini [uwsgi] socket = 127.0.0.1:8000 chdir = /path/to/project module = myapp.wsgi:application processes = 4 threads = 2 ``` 以上配置文件中,指定了uWSGI服务器监听的IP地址和端口号、应用程序的根目录、应用程序的入口模块和函数、启动的进程数和线程数。 3. 启动uWSGI服务器 可以使用以下命令启动uWSGI服务器: ```bash uwsgi --ini uwsgi.ini ``` 以上命令会读取当前目录下的uwsgi.ini文件,并使用其中的配置启动uWSGI服务器。 4. 配置Nginx服务器 uWSGI服务器通常和Nginx服务器一起使用,可以通过Nginx代理请求到uWSGI服务器。以下是一个简单的Nginx配置文件示例: ```nginx server { listen 80; server_name example.com; location / { uwsgi_pass 127.0.0.1:8000; include uwsgi_params; } } ``` 以上配置文件中,指定了Nginx服务器监听的IP地址和端口号、请求的域名、代理请求到的uWSGI服务器地址和端口号、以及uWSGI服务器的参数。 总结: 以上是uwsgi模式_uwsgi配置详解。通过uWSGI服务器和Nginx服务器的配合,可以快速部署Python Web应用程序,并提高Web应用程序的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值