繁体   English   中英

无法通过uwsgi将Flask模块链接到Nginx

[英]Unable to link Flask module to nginx through uwsgi

我确定这里缺少一些简单的东西。 我可以通过nginx获得python / flask脚本的PART,但是重要的位不起作用。 这是我的python:

#!/usr/bin/python
import flask
from flask import Flask, jsonify, render_template, request
import os

app = flask.Flask(__name__)
app.config['SERVER_NAME']='localhost'


@app.route("/stuff")
def index():
    return render_template('index.html')

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

def application(environ, start_response):

    start_response("200 OK", [("Content-Type", "text/plain")])
    return ["Hello World!"]

这是我的uwsgi启动:

 sudo -u www-data uwsgi -s /tmp/myApp.sock --module MyApp 

该套接字已正确链接,可用于nginx。
这是我的Nginx配置片段:

 location /test {
            uwsgi_pass unix:/tmp/myApp.sock;
            include uwsgi_params;
    }

当我去myserver / test时,我会看到“ Hello World!”。 就像我期望的那样。 但是当我访问myserver / test / stuff时,我也得到了“ Hello World!”。 而不是index.html的内容(这是有效的,我在其他地方使用它)。 如果输入myserver / test / garbage.html,则会得到一个通用的nginx 404,而不是我的自定义代码。

谁能指出我的方向?

谢谢

- 编辑 -

谢谢您的回答,它确实有帮助,但不能解决我的整个问题。
在我的uwsgi启动行中添加“ --callable app”,会将uwsgi服务器链接到nginx。 呀! 我可以通过nginx返回我的自定义404文件来确认这一点。

但是那404是我所能得到的。 我无法访问与404.html位于同一目录中的index.html,它具有相同的所有者和相同的权限。 它实际上是几乎相同的文件,但文本略有不同。

这可能是期望的问题。 我希望在http://(myserver)/test/stuff上找到我的index.html,但是我得到了404。

我看错地方了吗? 或者我的烧瓶,uwsgi或nginx中是否有东西掉? 谢谢

您的应用程序功能不会调用flask应用程序,这就是为什么每条路线都返回200的“ Hello World”的原因。我很确定您有两个简单的选择。

首先是删除应用程序功能,并将其替换为application = app

第二个是将uwsgi行更改为

sudo -u www-data uwsgi -s /tmp/myApp.sock --module MyApp --callable app

无论如何,这会使您的应用程序功能不相关。

您可以在此处阅读有关使用uwsgi的更多信息。

编辑

据我对nginx的了解,您的nginx配置应如下所示

location = /test { rewrite ^ /test/; }
location /test { try_files $uri @test; }
location @test {
  include uwsgi_params;
  uwsgi_param SCRIPT_NAME /test;
  uwsgi_modifier1 30;
  uwsgi_pass unix:/tmp/myApp.sock;
}

与上面链接的uwsgi页上的推荐相同。 您似乎不在根URL上运行,因此基本配置将无法运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM