大多数可用的教程都展示了如何使用上游HTTP服务器(如NGINX)设置uWSGI。但是,uWSGI本身可以作为路由器/代理/负载均衡器美妙地运作-请参考此处。
对于我的项目,我不想在此时设置NGINX,因此我开始探索通过uWSGI提供网页的选项。这里的答案展示了如何使用Pyramid进行设置。
我正在使用pyramid_mongodb脚手架,我已将其修改以使其在python3上运行。有关详细信息,请参见此处。
假设我们有一个由pcreate -s pyramid_mongodb MyProject
创建的Pyramid项目。
以下是开发/production.ini中所需的uWSGI配置。
[uwsgi]
http = 0.0.0.0:8080
#http-to /tmp/uwsgi.sock - use this for standalone mode
#socket = :9050
master = true
processes = 2
harakiri = 60
harakiri-verbose = true
limit-post = 65536
post-buffering = 8192
daemonize = ./uwsgi.log
pidfile = ./orange_uwsgi.pid
listen = 128
max-requests = 1000
reload-on-as = 128
reload-on-rss = 96
no-orphans = true
#logto= <log file>
log-slow = true
virtualenv = <path to virtual environment>
#file = /path/to/pyramid.wsgi
#callable = application
need-app = true
另外,由于我们使用的是uWSGI,因此可以从ini文件中注释掉server
部分。
#[server:main]
#use = egg:waitress#main
#host = 0.0.0.0
#port = 6544
使用以下命令运行服务器:uwsgi --ini-paste development.ini
更加简单!不需要修改“development.ini”文件。 在存放“development”和“production”ini文件的应用程序文件夹中,创建一个名为“wsgi.app”的文件,并将以下内容添加进去:
from pyramid.paster import get_app,setup_logging
ini_path = '/pathto/myapp/development.ini'
setup_logging(ini_path)
application = get_app(ini_path,'main')
[uwsgi]
socket = 127.0.0.1:3053
uid = daemon
gid = daemon
venv = /pathto/myenv
project_dir = /pathto/myapp
chdir = %(project_dir)
master = true
plugins = plugins/python/python
check-static = %(project_dir)
static-skip-ext = .py
static-skip-ext = .pyc
static-skip-ext = .inc
static-skip-ext = .tpl
pidfile2 = /var/run/uwsgi/myinfo.pid
disable-logging = true
processes = 8
cheaper = 2
enable-threads = true
offload-threads = N
py-autoreload = 1
wsgi-file = /pathto/myapp/wsgi.py
NGINX的配置非常简单:
server {
listen [xxxx:xxxx:xxxx:xxx:xxxx:xxxx]:80; #for IPv6
listen xxx.xxx.xxx.xxx:80; #for IPv4
server_name myapp.domain.com;
location / {
try_files $uri @uwsgi;
}
location @uwsgi {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3053;
}
}