我在 Nginx 上使用 uwsgi 来运行一些 Python 代码。
我想将 uwsgi 绑定(bind)到一个目录,并让它在浏览器中呈现我从服务器调用的任何 .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;
}
就目前而言,当我尝试调用网络根目录(即 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]
有什么想法吗?
谢谢!
最佳答案
WSGI 不像 PHP。你不能只将 uwsgi 指向一个包含一堆 .py 文件的目录。事实上,永远不要让你的 python 模块在公共(public)目录中可用,可以从服务器访问。您需要将 uwsgi 连接到 WSGI 应用程序,最好是框架。阅读有关 WSGI 的更多信息 here .查看bottle这是一个小而简单的 WSGI 框架。它有很棒的文档,而且很容易上手。不过,实际上有很多很棒的 Python 网络框架,所以请随意四处看看:)
关于python - uwsgi + python + nginx + willy nilly 文件执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6247667/