uwsgi + python + nginx + willy nilly文件执行

eas*_*de5 5 python nginx uwsgi

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

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

这有可能吗?或者我只能在uwsgi中明确指定单个模块/ app /文件?

这是我的init语法:

/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
Run Code Online (Sandbox Code Playgroud)

我认为这将允许/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;

    }
Run Code Online (Sandbox Code Playgroud)

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

wsgi application not found
Run Code Online (Sandbox Code Playgroud)

使用以下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]
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢!

zee*_*kay 10

WSGI与PHP不同.你不能只是将uwsgi指向一个带有一堆.py文件的目录.事实上,永远不要让你的python模块在公共目录中可用,可以从服务器访问.您需要将uwsgi挂钩到WSGI应用程序,最好是框架.在这里阅读有关WSGI的更多信息.检查瓶子是一个小而简单的WSGI框架.它有很棒的文档,很容易上手.实际上,Python有很多很棒的Web框架,所以请随意浏览:)