仅限 Flask App returns 404,不管它如何 运行

Flask App only returns 404's regardless of how it is run

我似乎无法在 uWSGI 上正确获取我的应用程序 运行。我已经从命令行中通过 运行 uWSGI 将 nginx 排除在等式之外,它表现出与 运行 on nginx.

完全相同的行为
uwsgi -s 0.0.0.0:5050 -w app:app --uid www-data --gid www-data --protocol=http

而uwsgi处理请求如下:

[pid:0625|app: 0|req: 1/1] 192.168.1.219 () {34 vars in 737 bytes} [Tue Mar 31 11:10:30 2015] GET /admin => generated 233 bytes in 25 msecs (HTTP/1.1 404) 3 headers in 249 bytes (1 switches on core 0)

我的文件结构如下

/srv/www/cc/app/
               static/
               templates/
               __init__.py
               views.py
               models.py
               forms.py

根据新证据表明它可能是我的应用程序,这是我的 init.py 文件:

import os
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand

app = Flask(__name__)
app.config.from_object('config')

db = SQLAlchemy(app)
migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

from flask.ext.mail import Mail
mail = Mail(app)

from app import models, forms

#setup flask-security
from flask.ext.security import Security, SQLAlchemyUserDatastore

user_datastore = SQLAlchemyUserDatastore(db, models.User, models.Role)
security = Security(app, user_datastore,
    confirm_register_form = forms.ExtendedConfirmRegisterForm
    )

from app import views

Link 我整个 views.py: http://pastebin.com/0jMTarEe

我有过同样的应用程序,在开发的早期阶段,运行 在我拥有的不同 nginx+uwsgi 服务器上,并准确复制了它们的配置文件,但无济于事。我真的不知道为什么这行不通。但是根据 Flask 文档,以上就是我应该需要的。

为了咯咯笑,我继续将 uwsgi 从等式中删除。在 cc 文件夹中创建了一个 run.py 文件... python run.py:

from app import app
app.run('0.0.0.0', port=5050, debug=True)

请求:

* Running on http://0.0.0.0:5050/
* Restarting with reloader
192.168.1.219 - - [31/Mar/2015 11:56:24] "GET / HTTP/1.1" 404 -

所以这似乎是我的应用程序的问题,而不是关于 nginx 或 uwsgi 的任何问题。不过,我在任何地方都没有收到任何错误...可能出了什么问题??

编辑

根据评论中的建议进行了更多挖掘。我编辑了 init.py:

的底部
import . import views

print views
print app.url_map

这是我启动服务器时的输出:

* Running on http://0.0.0.0:5050/
* Restarting with reloader
<module 'app.views' from '/srv/www/cc/app/views.py'>
Map([<Rule '/' (HEAD, POST, OPTIONS, GET) -> index>,
<Rule '/admin/' (HEAD, POST, OPTIONS, GET) -> admin>,
<Rule '/test/' (HEAD, POST, OPTIONS, GET) -> test>
...
Lots more ... ])

如果您设置 SERVER_NAME 配置值,它 必须 匹配应用程序在外部提供服务的主机和端口。要么把它注释掉,要么让它与服务器的域或 ip 匹配。

app.config['SERVER_NAME'] = 'example.com:80'

当 运行 开发服务器时,名称将是 localhost:5000,尽管在这种情况下通常不需要设置它。