简体   繁体   English

运行Flask应用程序时找不到404 Not

[英]Get 404 Not Found when running Flask app

I want to setup Flask on Apache. 我想在Apache上设置Flask。 I tested my Apache configuration successfully, but I cannot run it with the following Flask application. 我成功测试了我的Apache配置,但我无法使用以下Flask应用程序运行它。 Therefore I didn't post this question to server fault but to Stack Overflow. 因此,我没有将此问题发布到服务器故障,而是发布到Stack Overflow。

Calling http://54.68.62.180 throws the following error: 调用http://54.68.62.180会引发以下错误:

Not Found 未找到

The requested URL was not found on the server. 在服务器上找不到请求的URL。 If you entered the URL manually please check your spelling and try again. 如果您手动输入了URL,请检查拼写,然后重试。

For the records, my Apache configuration AppName.conf file looks like this: 对于记录,我的Apache配置AppName.conf文件如下所示:

<VirtualHost *:80>
               ServerName 54.68.62.180
               WSGIDaemonProcess AppName threads=5
               WSGIScriptAlias / /var/www/AppName/appname.wsgi
               <Directory /var/www/AppName>
                       WSGIProcessGroup AppName
                       WSGIApplicationGroup %{GLOBAL}
                       Order allow,deny
                       Allow from all
               </Directory>
               ErrorLog ${APACHE_LOG_DIR}/error.log
               LogLevel info
               CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

My appname.wsgi looks like this: 我的appname.wsgi看起来像这样:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/AppName")

from appname import application
application.secret_key = 'my_key'

In my Flask application, the application.py file looks like this: 在我的Flask应用程序中, application.py文件如下所示:

from appname import application
from appname.socket_interface import socketio

if __name__ == '__main__':
    socketio.run(application)
    #application.run()

I tried both, socketio.run(application) and application.run(), in both cases I get the error. 我试过两个,socketio.run(应用程序)和application.run(),在这两种情况下我都得到了错误。 Some other posts like Flask tutorial - 404 Not Found seem to hint that the app is "executed before any of your routes are registered" but my application.run() is already at the bottom of the file. 其他一些帖子,如Flask教程 - 404 Not Found似乎暗示应用程序是“在任何路由注册之前执行的”但我的application.run()已经在文件的底部。

The routing is specified in appname/appname/ init .py through: 路由在appname / appname / init .py中指定:

application.register_blueprint(frontend, url_prefix='/app')

and in 并在

appname/appname/frontend/__init__.py

the html source is routed through: html源通过以下方式路由:

@frontend.route('/')
def frontend_test():
    return frontend.send_static_file('index.html')

What am I doing wrong here and why do I get the 404 error? 我在这里做错了什么,为什么我会收到404错误?

Assuming the application.py script you posted is not redacted, you're missing the actual route. 假设您发布的application.py脚本没有编辑,那么您将错过实际路由。 When requesting the base url ( http://54.68.62.180 in your case), Flask simply doesn't know what to serve, hence the 404. Add the following to your application.py file: 在请求基本URL时(在您的情况下为http://54.68.62.180 ),Flask根本不知道要提供什么,因此404.将以下内容添加到您的application.py文件中:

@app.route('/')
def hello_world():
    return 'hello world!'

See the quickstart guide . 请参阅快速入门指南

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

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