uwsgi 快速入门

uwsgi 快速入门

一、 概述

1、 简单介绍

WSGI(Web Server Gateway Interface),定义了web服务器(nginx、apache、iis等)和 web应用(或者将web框架,flask、django等)之间的接口规范。也就是说,只要 web服务器和 web应用都遵守WSGI协议,那么 web服务器和 web应用就可以随意的组合。

uwsgi的启动可以把参数加载命令行中,也可以是配置文件 .ini, .xml, .yaml 配置文件中,个人用的比较多得是 .ini 文件。

通过uwsgi --help可以查看得到:

-x|--xmlconfig                         load config from xml file
-x|--xml                               load config from xml file
--ini                                  load config from ini file
-y|--yaml                              load config from yaml file
-y|--yml                               load config from yaml file

2、 环境配置

使用 pip 进行安装

pip install uwsgi

二、 第一个 WSGI 应用

1、 运行

让我们从一个简单的 "Hello World" 开始,创建文件 index.py,代码如下:

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

WSGI Python 加载器将会搜索的默认函数 application

接下来我们启动 uWSGI 来运行一个 HTTP 服务器,将程序部署在HTTP端口 8080上:

uwsgi --http :8080 --wsgi-file index.py

2、 添加并发

默认情况下,uWSGI 启动一个单一的进程和一个单一的线程。

你可以用 --processes 选项添加更多的进程,或者使用 --threads 选项添加更多的线程 ,也可以两者同时使用。

uwsgi --http :9090 --wsgi-file index.py --master --processes 4 --threads 2

以上命令将会生成 4 个进程, 每个进程有 2 个线程。

如果你要执行监控任务,可以使用 stats 子系统,监控的数据格式是 JSON:

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

我们可以安装 uwsgitop(类似 Linux top 命令) 来查看监控数据:

pip install uwsgitop

三、 结合 Web 服务器使用

1、 Flask

这里可以使用配置文件来支持 Flask 运行。

[uwsgi]
# 监听的端口号
socket = localhost:5000   
# 开启主进程
master = true
# 项目文件路径
chdir = /root/www/          
# 项目启动文件
wsgi-file = app.py      
# flask 实例名称
callable = app 
# 项目的进场数量
processes = 4 
# 项目的线程数量
threads = 2   
# 查看监控数据
stats = localhost:9191    
# 主进程的 pid  # 如:uwsgi --reload uwsgi
pidfile = uwsgi.pid        
# 日志文件
daemonize = /var/log/uwsgi/uwsgi.log 
# 允许用内嵌的语言启动线程,这将允许你在app程序中产生一个子线程
enable-threads = true
# socket 权限
chmod-socket = 666
# 指定虚拟环境位置
home = .venv

启动运行:

uwsgi -d --ini yourfile.ini  # 开启服务

2、 Django

uwsgi 的配置文件

[uwsgi]
socket = localhost:5000
master = true
chdir = /root/temp/django/CRM
wsgi-file = /root/temp/django/CRM/CRM/wsgi.py
processes = 4
threads = 2
daemonize = /var/log/uwsgi/uwsgi.log 
pidfile = uwsgi.pid
chmod-socket = 666
enable-threads = true
stats = localhost:9191  
home = .venv

启动运行:

uwsgi -d --ini yourfile.ini  # 开启服务

3、 Nginx配置

这里使用 flask 的为一个示例

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
    upstream flaskservice{
    	server localhost:5000;
	}
    	 
    server {
		#SSL 默认访问端口号为 443
        listen 443 ssl;
        #请填写绑定证书的域名
        server_name steve1.cn;
        #请填写证书文件的相对路径或绝对路径
        ssl_certificate  cert/steve1.cn_bundle.crt;           
        #请填写私钥文件的相对路径或绝对路径
        ssl_certificate_key cert/steve1.cn.key;           
        ssl_session_timeout 5m;
        #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        #请按照以下协议配置
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
		# 实现下载站点
        location /download {
            root /root/www;
            autoindex on;
            autoindex_localtime on;
        }
        # 静态资源通过 Nginx 来分发
        location ~/.*\.(html|htm|js|css|ico|png|jpg|gif|svg|txt) {
           root /root/www/static;

        }
        # 动态资源通过 Flask 来分发,同时使用了 uwsgi
        location / {
            include uwsgi_params;
            uwsgi_pass flaskservice; 
        }

        # 错误页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen 80;
        #请填写绑定证书的域名
        server_name steve1.cn; 
        # 把http的域名请求转成https
        location / {
            rewrite ^/ https://$host$request_uri permanent;
        }
    }
}
posted @ 2023-01-12 19:31  Kenny_LZK  阅读(197)  评论(0编辑  收藏  举报