uwsgi +蟒蛇 + nginx +随意执行文件
我在 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 根目录(即 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]
有什么想法吗?
谢谢!
I'm using uwsgi on Nginx to run some Python code.
I'd like to bind uwsgi to a directory and make it render any .py file that I call from the server in the browser. I'm thinking like PHP, here (/index.php executes that file, /login.php executes that file).
Is this a possibility? Or am I only able to explicitly specify a single module/app/file in uwsgi?
Here is my init syntax:
/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
I thought that would allow /srv/www
to act as the folder where any .py files are executed.
Here is my nginx config:
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;
}
As it stands, when I try to call web root (ie www.site.com/) I get a:
wsgi application not found
With the following index.py file:
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]
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WSGI 与 PHP 不同。您不能只将 uwsgi 指向包含一堆 .py 文件的目录。事实上,永远不要让你的 python 模块在公共目录中可用,可以从服务器访问。您需要将 uwsgi 连接到 WSGI 应用程序,最好是框架。 此处了解有关 WSGI 的更多信息。查看 bottle,它是一个小型、简单的 WSGI 框架。它有很棒的文档,而且很容易上手。实际上,Python 有很多很棒的 Web 框架,所以请随意看看:)
WSGI is not like PHP. You can't just point uwsgi to a directory with a bunch of .py files. In fact, never, ever make your python modules available in a public directory, accessible from the server. You need to hook uwsgi up to a WSGI application, preferably a framework. Read more about WSGI here. Check out bottle which is small, simple WSGI framework. It has great docs, and it's easy to get started with. There are actually tons of great web frameworks for Python though, so feel free to look around :)
您可能想阅读此帖子:
http://lists.unbit.it /pipermail/uwsgi/2011-3月/001657.html
You may want to read this thread:
http://lists.unbit.it/pipermail/uwsgi/2011-March/001657.html