5

我在 Nginx 上使用 uwsgi 来运行一些 Python 代码。

我想将 uwsgi 绑定到一个目录,并让它呈现我在浏览器中从服务器调用的任何 .py 文件。我在这里像 PHP 一样思考(/index.php 执行该文件,/login.php 执行该文件)。

这是一种可能吗?还是我只能在 uwsgi 中明确指定一个模块/应用程序/文件?

这是我的初始化语法:

/opt/uwsgi/uwsgi -s 127.0.0.1:9001 -M 4 -t 30 -A 4 -p 4 -d /var/log/uwsgi.log --pidfile /var/run/uwsgi.pid --pythonpath /srv/www

我认为这将允许/srv/www充当执行任何 .py 文件的文件夹。

这是我的 nginx 配置:

server {
    listen       80;
    server_name  DONT_NEED_THIS;

    access_log  /srv/www/logs/access.log;
    error_log   /srv/www/logs/error.log;

    location / {
        root  /srv/www;

        # added lines    
        include        uwsgi_params;
        uwsgi_pass     127.0.0.1:9001;

    }

就目前而言,当我尝试调用 web root(即 www.site.com/)时,我得到:

wsgi application not found

使用以下 index.py 文件:

import sys
import os

sys.path.append(os.path.abspath(os.path.dirname(__file__)))

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

有任何想法吗?

谢谢!

4

2 回答 2

10

WSGI 不像 PHP。您不能只将 uwsgi 指向包含一堆 .py 文件的目录。事实上,永远,永远不要让你的 python 模块在公共目录中可用,可以从服务器访问。您需要将 uwsgi 连接到一个 WSGI 应用程序,最好是一个框架。在此处阅读有关 WSGI的更多信息。查看瓶子,它是一个小型、简单的 WSGI 框架。它有很棒的文档,而且很容易上手。不过,实际上有很多很棒的 Python 网络框架,所以请随意看看 :)

于 2011-06-06T04:35:00.177 回答
3

您可能想阅读此线程:

http://lists.unbit.it/pipermail/uwsgi/2011-March/001657.html

于 2011-06-06T04:44:13.480 回答