使用UWSGI和NGINX服务器来运行Pyramid应用程序

3
我对NGINX、uWSGI和Pyramid都很陌生,我正在尝试使用nginx作为反向代理通过uWSGI来提供Pyramid应用程序。目前我遇到了困难,希望有人能提出一些解决方案。如果您能稍微解释一下可能发生了什么,那就更好了,因为我的理解非常有限!
目前,当我访问反向代理URL时,uWSGI会返回“内部服务器错误”。在uWSGI错误日志中,我收到以下错误:
--- no python application found, check your startup logs for errors ---

当我仅通过uWSGI服务运行应用程序,使用pserve启动时,它能够正常工作。我可以通过以下方式从我的虚拟信封中启动它:
bin/pserve my-app/uwsgi.ini

但是当我启动nginx并访问代理地址时,我会收到“内部服务器错误”的提示。
我在uwsgi.ini中的设置如下:
[app:main]
use = egg:myapp
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid_debugtoolbar
[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543

[loggers]
keys = root, musiccircle

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = ERROR
handlers = console
[logger_musiccircle]
level = ERROR
handlers =
qualname = musiccircle

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

[uwsgi]
socket = unix://home/usr/env/myapp/myapp.sock
master = true

processes = 48
cpu-affinity = 12

harakiri = 60
post-buffering = 8192
buffer-size = 65535

daemonize = ./uwsgi.log
pidfile = ./pid_5000.pid

listen = 32767

reload-on-as = 512
reload-on-rss = 192
limit-as = 1024
no-orphans = true
reload-mercy = 8
log-slow = true

virtualenv = /home/usr/env

在nginx中对应的myapp.conf文件中,我有以下内容:
upstream myapp {
    server 127.0.0.1:6543;
}

server {
    listen          8080;
    server_name myapp.local www.myapp.local;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/usr/env/myapp;
    }
    access_log      /var/log/nginx/access.log;
    error_log       /var/log/nginx/error.log;

    charset         utf-8;
    location / {
        include         uwsgi_params;
        uwsgi_pass      unix://home/usr/env/myapp/myapp.sock;
    }
}

如果您需要查看其他内容,请告诉我。正如您所看到的,Nginx 配置为在端口 8080 上提供服务(它确实这样做),Pyramid 应用程序由 uWSGI 提供服务到端口 6543(它确实这样做)。
提前感谢。

据我所见,您的配置文件是为Pyramid pserve而不是uwsgi - Mikko Ohtamaa
在你的nginx.conf文件中,尝试将server 127.0.0.1:6543更改为server 0.0.0.0:6543。 - crooksey
在您的Nginx文件中,uwsgi_pass指令应该是uwsgi_pass unix:///home/usr/env/myapp/myapp.sock;(有3个斜杠)。 在您的uwsgi.ini文件中,注释掉以下这些行:use = egg:waitress#main,host = 0.0.0.0,port = 6543。在[uwsgi]部分中,当指定套接字路径时,不需要添加unix://。另一个对我有用的事情是为套接字添加适当的权限。这是我的wsgi部分的配置:http://pastebin.com/PB89jGMM。我使用env/bin/uwsgi --ini-paste uwsgi.ini来启动它。 - webjunkie
Mikko Ohtamaa,你是对的...我一直在使用paster/pserve与waitress来服务我的应用程序,而不是uwsgi。因此,我决定修改我的nginx conf,并继续使用waitress进行服务。这就是我所做的,crooksey,谢谢你,你是对的,我需要修改我的.ini文件以使得2个主机IP匹配。现在使用waitress + nginx配置可以正常工作,虽然我在Chrome中访问网站时遇到问题(但那是另一个问题!)。webjunkie,感谢你的建议,我也会尝试这个选项。 - Sorrel
是的,我认为可以运行Nginx + pserve,尽管大多数人似乎更喜欢uwsgi。 - Mikko Ohtamaa
1个回答

2
似乎Pyramid项目旨在通过setup.py安装,然后使用.ini配置文件和pserve运行。然后,Pserve将这些配置文件详细信息作为**settings传递给您的Pyramid应用程序在运行时。这与Flask不同,Flask通常没有配置文件,可以根据需要由uWSGI运行,所有运行时配置都由uWSGI或环境变量处理。由于Pyramid通常需要在运行时使用配置文件,并且在使用配置文件(即production.ini)时依赖于pserve提供它们,所以我认为您需要运行uwsgi --ini-paste production.ini(如果使用Pypy,则运行uwsgi --pypy-paste production.ini)(感谢@Sorrel)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接