WSGI、uwsgi、uWSGI与Nginx

一、写在前

WSGI、uwsgi与uWSGI

  • WSGI (web server gateway interface 网络服务网关接口) 只能用于python,是一个接口标准协议,django、flask自带有
  • uwsgi 是WSGI的通信协议
  • uWSGI是实现了uwsgi协议和WSGI服务的web服务器

他们是为了将web服务器与web框架连接起来

Nginx、uWSGI 与 Python后端框架的关系


数据交换流程图

数据交换流程图

每个服务处理自己擅长的事情:

  • Nginx:负载均衡,静态资源(css,img,html)直接返回,动态资源反向代理到upstream(uwsgi_pass,fastcgi_pass,proxy_pass)

  • uWSGI:数据交换桥梁

  • Python后端框架:业务逻辑

二、uWSGI

安装

pip install uwsgi

配置

[uwsgi]
# 设置0.0.0.0表示可以接收不同服务器的nginx发送过来的请求,127.0.0.1表示只接收同一服务器发送过来的请求,8000为应用内的监听端口
socket = 0.0.0.0:8000 

# http = 0.0.0.0:8000 # http为直接作为web服务器启动
# http = :5000#启动程序时所使用的地址和端口,通常在本地运行f1ask项目

# 项目目录
chdir = /home/flaskproiect/   

# flask程序的启动文件,通常在本地是通过运行 python manage.py runserver 来启动项目的
wsgi-file = manage.py

# 程序内启用的application变量名
callable = app

# 处理器个数,进程个数
processes = 4

# 线程个数
threads =2

#获取uwsgi统计信息的服务地址
stats =127.0.0.1:9191

# 保存pid信息,方便停止服务和重启的时候用
pidfile = uwsgi.pid

# 后台运行时记录uwsgi的运行日志
daemonize=./1og/uwsgi.log

# 当需要连接cassandra时,uwsgi无法启动服务,可以使用该选项设置
lazy-apps = true

# 使用chain-reloading 逐个work重启,服务不中断,命令是 echoc > mfifo 
master-fifo = /opt/mt-search/web-service/mfifo
touch-chain-reload =true

Nginx反向代理uWSGI

location / {
	include uwsgi_params;
    uwsgi_pass 127.0.0.1:8000;
}

拓展

  • 还可以使用http协议代理uWSGI,这个取决于uwsgi服务使用何种方式启动的 socket 对应 uwsgihttp对应http

    location / {
        include uwsgi_params;
        http_pass 127.0.0.1:8000;
    }
    
  • 在测试时,可以使用uwsgiNginx可以使用http,方便使用postman等工具测试,生产环境中由于使用到了Nginx所以最好使用uwsgi,实现高并发。

三、Nginx
Nginx安装配置详解

猜你喜欢

转载自blog.csdn.net/qq_40851623/article/details/143309105