大多数可用的教程都展示了如何使用上游HTTP服务器(如NGINX)设置uWSGI。但 uWSGI 单独作为路由器/代理/负载均衡器可以很好地行动 - 请参考this 对于我的项目,我现在不想设置NGINX,所以我开始探索通过uWSGI提供网页服务的选项。这里的答案显示了如何使用 Pyramid 进行设置。
答案 0 :(得分:10)
我正在使用pyramid_mongodb脚手架,我已修改它以使其在python3上运行。有关详细信息,请参阅here。
假设我们有一个Pyramid项目(使用pcreate -s pyramid_mongodb MyProject
创建)。
以下是development / production.ini
[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
答案 1 :(得分:2)
更容易!无需修改“development.ini”文件。 在“开发”和“生产”ini文件所在的App文件夹中创建一个名为“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')
使用它的内容创建让我们说“myapp.conf”:
[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;
}
}