Article directory
uwsgi quick start
I. Overview
1. Brief introduction
WSGI (Web Server Gateway Interface) defines the interface specification between web servers (nginx, apache, iis, etc.) and web applications (or web frameworks, flask, django, etc.). In other words, as long as the web server and web application comply with the WSGI protocol, the web server and web application can be combined at will.
When starting uwsgi, you can load parameters into the command line, or you can use the configuration file .ini, .xml, .yaml. Among the configuration files, the .ini file is often used by individuals.
uwsgi --help
You can view through :
-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. Environment configuration
Install using pip
pip install uwsgi
2. The first WSGI application
1. Run
Let's start with a simple "Hello World" and create the file index.py with the following code:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
The default function application that the WSGI Python loader will search for .
Next we start uWSGI to run an HTTP server and deploy the program on HTTP port 8080:
uwsgi --http :8080 --wsgi-file index.py
2. Add concurrency
By default, uWSGI starts a single process and a single thread.
You can add more processes with the --processes option, or add more threads with the --threads option, or both at the same time.
uwsgi --http :9090 --wsgi-file index.py --master --processes 4 --threads 2
The above command will generate 4 processes, each process has 2 threads.
If you want to perform monitoring tasks, you can use the stats subsystem. The monitoring data format is JSON:
uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
We can install uwsgitop (similar to Linux top command) to view monitoring data:
pip install uwsgitop
3. Use in combination with Web server
1、 Flask
Configuration files can be used here to support Flask operation.
[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
Start running:
uwsgi -d --ini yourfile.ini # 开启服务
2、 Django
uwsgi configuration file
[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
Start running:
uwsgi -d --ini yourfile.ini # 开启服务
3. Nginx configuration
Here is an example using 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;
}
}
}