一、WSGI是什么?

WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。描述的是Web服务器如何与Web应用间进行通信

它不是服务器、python模块、框架、API或者任何软件,只是一种描述web服务器(如nginx,uWSGI等服务器)如何与web应用程序(如用Django、Flask框架写的程序)通信的规范。

WSGI 的官方定义是,the Python Web Server Gateway Interface。从名字就可以看出来,这东西是一个Gateway,也就是网关。网关的作用就是在协议之间进行转换。

在这里插入图片描述

二、uWSGI是什么?

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。它要做的就是把HTTP协议转化成语言支持的网络协议。比如把HTTP协议转化成WSGI协议,让Python可以直接使用。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。

  • WSGI,是一种描述web服务器(如nginx,uWSGI等服务器)如何与web应用程序(如用Django、Flask框架写的程序)通信协议。
  • uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,用于与nginx等代理服务器通信,它与WSGI相比是两样东西。
  • uWSGI是实现了uwsgi和WSGI两种协议的Web服务器
    在这里插入图片描述
    为什么有了uWSGI为什么还需要nginx?因为nginx具备优秀的静态内容处理能力,然后将动态内容转发给uWSGI服务器,这样可以达到很好的客户端响应。

#FastCgi协议, uwsgi协议, http协议有什么用?

nginx 和 uWSGI交互就必须使用同一个协议,而上面说了uwsgi支持fastcgi,uwsgi,http协议,这些都是nginx支持的协议,只要大家沟通好使用哪个协议,就可以正常运行了。

uwsgi是服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回

Django,Flask项目部署

windows开发环境下打包本地依赖版本打到requirements.txt文件中

1
pip freeze > requirements.txt

Linux环境下,进入虚拟环境中,然后进入到项目所在目录,安装好相应的包

1
pip install -r requirements.txt

django 服务器

  • runserver
  • wsgi

uwsgi : web服务器,多线程处理的不错

  1. pip install uwsgi

  2. 工程目录下创建uwsgi.ini 配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    [uwsgi]
    # 使用nginx连接时 使用
    socket = 0.0.0.0:8080

    # 直接作为web服务器使用
    #http=0.0.0.0:8080
    # 配置工程目录
    chdir = /usr/local/Python_RomoteWorkspace/flaskblog

    # 配置项目的wsgi目录。相对于工程目录
    # wsgi-file=OnlineStore/wsgi.py

    # 适用于flask项目部署
    wsgi-file = app.py
    # router
    callable = app


    #配置进程,线程信息
    processes = 4

    threads = 10

    enable-threads = True

    master = True

    pidfile = uwsgi.pid

    daemonize = uwsgi.log
  3. 书写配置信息

  4. 使用uwsgi服务器

    • 启动:uwsgi --ini uwsgi.ini
    • 停止:uwsgi --stop uwsgi.pid
  5. 结合nginx反向代理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # 查看进程
    ps -ef |grep nginx

    # 控制Nginx
    nginx -s signal
    stop 快速关闭
    quit 优雅的关闭
    reload 重新加载配置
    # 通过系统管理
    systemctl status nginx 查看nginx状态
    systemctl start nginx 启动nginx服务
    systemctl stop nginx 关闭nginx服务
    systemctl enable nginx 设置开机自启
    systemctl disable nginx 禁止开机自启

    nginx -t # 不运行,仅测试配置文件
    nginx -c configpath # 从指定路径加载配置文件
    nginx -t -c configpath # 测试指定配置文件

nginx.conf主要配置

1
2
3
4
5
6
7
location /static{   # nginx动静分离配置
alias xxx/static/; # 项目静态资源的绝对路径
}
location / {
include uwsgi_params;
uwsgi_pass localhost:8000;
}

完整配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
user  root;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
root /usr/local/Python_RomoteWorkspace/flaskblog; # 项目根目录
location /static { # nginx动静分离配置
alias /usr/local/Python_RomoteWorkspace/flaskblog/static;
}
location / { # 反向代理uwsgi
include /usr/local/nginx/conf/uwsgi_params;
uwsgi_pass 192.168.40.142:8080; # 必须和uwsgi.ini中ip和端口一致
}
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}

问题

安装uwsgi 报错:plugins/python/uwsgi_python.h:2:20: 致命错误:Python.h:没有那个文件或目录

解决:

需要安装gccpython-dev

1
2
3
yum -y install gcc
yum -y install python-devel
# python 2用python-devel,python3用 python3-dev