简体   繁体   English

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

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

I'm sure I am missing something simple here. 我确定这里缺少一些简单的东西。 I can get PART of a python/flask script to be available through nginx, but the important bits are just not working. 我可以通过nginx获得python / flask脚本的PART,但是重要的位不起作用。 Here is my python: 这是我的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!"]

Here is my uwsgi start up: 这是我的uwsgi启动:

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

The socket is correctly linked and available to nginx. 该套接字已正确链接,可用于nginx。
And here is my nginx config snippet: 这是我的Nginx配置片段:

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

When I go to myserver/test I get the "Hello World!" 当我去myserver / test时,我会看到“ Hello World!”。 like I Would expect. 就像我期望的那样。 But when I go to myserver/test/stuff, I ALSO get "Hello World!" 但是当我访问myserver / test / stuff时,我也得到了“ Hello World!”。 rather than the contents of my index.html(Which is valid, I use it elsewhere). 而不是index.html的内容(这是有效的,我在其他地方使用它)。 And if I enter myserver/test/garbage.html, I get a generic nginx 404, rather than my custom one. 如果输入myserver / test / garbage.html,则会得到一个通用的nginx 404,而不是我的自定义代码。

Can anyone point me in a direction? 谁能指出我的方向?

Thanks 谢谢

--edit-- - 编辑 -

Thank you for the answer, it does help, but does not solve my entire issue. 谢谢您的回答,它确实有帮助,但不能解决我的整个问题。
Adding "--callable app" to my uwsgi startup line DOES link the uwsgi server to nginx. 在我的uwsgi启动行中添加“ --callable app”,会将uwsgi服务器链接到nginx。 Yah! 呀! I can confirm this by the fact that my customized 404 file IS being returned by nginx. 我可以通过nginx返回我的自定义404文件来确认这一点。

But that 404 is ALL I can get. 但是那404是我所能得到的。 I cannot get to my index.html, which is present in the same directory as the 404.html, has the same owner, and the same rights. 我无法访问与404.html位于同一目录中的index.html,它具有相同的所有者和相同的权限。 It is almost the same file really with slightly different text. 它实际上是几乎相同的文件,但文本略有不同。

This MAY be a problem with expectations. 这可能是期望的问题。 I am expecting to find my index.html at http://(myserver)/test/stuff But I get a 404. 我希望在http://(myserver)/test/stuff上找到我的index.html,但是我得到了404。

Am I looking in the wrong place? 我看错地方了吗? Or is there something off in my flask, uwsgi, or nginx? 或者我的烧瓶,uwsgi或nginx中是否有东西掉? Thanks 谢谢

You're application function does not call your flask app, which is why every route returns "Hello World", 200. I'm pretty sure you have two easy options. 您的应用程序功能不会调用flask应用程序,这就是为什么每条路线都返回200的“ Hello World”的原因。我很确定您有两个简单的选择。

The first is to drop the application function and replace it with application = app . 首先是删除应用程序功能,并将其替换为application = app

The second would be to change the uwsgi line to 第二个是将uwsgi行更改为

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

which renders your application function irrelevant anyway. 无论如何,这会使您的应用程序功能不相关。

You can read more about using uwsgi here . 您可以在此处阅读有关使用uwsgi的更多信息。

edit 编辑

As far as my knowledge about nginx goes, your nginx config should look like this 据我对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;
}

It is the same as the recommended one on the linked uwsgi page above. 与上面链接的uwsgi页上的推荐相同。 You seem to be running not on the root url, so the basic config will not work. 您似乎不在根URL上运行,因此基本配置将无法运行。

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

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