How to Deploy Python WSGI Applications Using uWSGI Web Server with Nginx
uWSGI广泛的功能、相对容易的配置使其成为许多部署需求的极佳选择——尤其是与Nginx结合时。
Nginx + uWSGI + Flask/Django 的架构兼具性能与拓展性。以下简洁地给出了能使用的uWSGI和Nginx的配置示例。以一个wsgi.py代表应用接口(Django或Flask)。详细的还是推荐深入阅读官方文档。
安装
sudo apt-get update
sudo apt-get -y upgrade #更新
sudo apt-get -y install python3-dev
sudo apt-get -y install python3-pip
sudo pip3 install virtualenv
mkdir my_app
cd my_app
virtualenv my_app_venv
mkdir app
source my_app_venv/bin/activate
sudo pip3 install uwsgi
sudo apt-get install nginx
sudo service nginx start # 启动nginx
# sudo service nginx stop # 暂停nginx
# sudo service nginx restart # 重启nginx
touch wsgi.py
文件结构
my_app # 包含所有东西的主文件夹
|
|=== my_app_venv # python虚拟环境
|=== app # app应用
|
|--- wsgi.py # 包含可调用的 application 的文件
|..
|.
- wsgi.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
配置uWSGI
- 我们可以直接令uWSGI
uwsgi --socket 127.0.0.1:8080 --protocol http --wsgi-file wsgi.py
或者
uwsgi --http :8080 --wsgi-file wsgi.py
使用http协议绑定侦听8080端口调用wsgi.py中的application,不需要额外的东西就行,它已经相当稳定和高性能,不过一般我们倾向于使用一个完全功能的web服务器,如Nginx。
- 此外,更普遍的做法是创建一个.ini配置文件而不是以命令行参数的形式启动uWSGI。使用
uwsgi yourfile.ini
命令运行
Web应用程序的示例配置:
[uwsgi]
# -------------
# 设置规则:
# key = value
# 注释 : #
# -------------
socket = 127.0.0.1:8080
# bind to the specified UNIX/TCP socket using default protocol
# 绑定端口。默认情况下,uWSGI使用它自己的uwsgi协议,使用protocol参数修改 。
chdir = /my_app
# 转移到wsgi.py所在目录,app根目录
wsgi-file = wsgi.py
# pass requests to your WSGI application
# 将请求传递给你的WSGI应用
# 或者使用 module = [wsgi_module_name]:[application_callable_name]
# 如 module = wsgi:application
master=true
# a master process (will respawn your processes when they die)
# 拥有一个主进程(会在进程死亡时重新启动进程)
processes = 4
threads = 2
# spawn 4 processes (each with 2 threads)
# 产生4个进程(每个包含2个线程)
stats = 127.0.0.1:9191
# 状态检测端口
# Make some request to your app and then telnet to the port 9191, you’ll get lots of fun information.You may want to use “uwsgitop” (just pip install it), which is a top-like tool for monitoring instances.
max-requests = 1001
# 如果你担心内存泄漏,并且不能想到一个更加坚实的处理它的方法,这个选项将允许你在处理设置的请求数量后自动重启你的进程。
procname = My Application
# 设置进程名称
更详细的配置参看
uWSGI Quickstart for Python/WSGI applications
How to use Django with uWSGI
Starting your app with uwsgi - Flask
使用Nginx做反向代理
- 典型架构
Client Request ----> Nginx (Reverse-Proxy)
|
/|\
| | `-> App. Server I. 127.0.0.1:8081 <--> Application
| `--> App. Server II. 127.0.0.1:8082 <--> Application
`----> App. Server III. 127.0.0.1:8083 <--> Application
注意: 当应用程序被设置为在127.0.0.1上侦听传入连接时,只能在本地访问它。如果使用0.0.0.0,它会也接受来自外部的连接。
- Nginx配置
sudo gedit /etc/nginx/nginx.conf
# 配置完成后重启服务
sudo service nginx stop
sudo service nginx start
Web应用程序的示例配置:
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream uwsgicluster {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
# ..
# .
}
# Configuration for Nginx
server {
# Running port
listen 80;
# Settings to by-pass for static files
location ^~ /static/ {
# Example:
# root /full/path/to/application/static/file/dir;
root /app/static/;
}
# Serve a static file (ex. favico) outside static dir.
location = /favico.ico {
root /app/favico.ico;
}
# Proxying connections to application servers
location / {
include uwsgi_params;
uwsgi_pass uwsgicluster;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
Creating an SSL certificate on Nginx.
How to Configure Nginx Web Server on a VPS.
网友评论