简体   繁体   中英

Get 404 Not Found when running Flask app

I want to setup Flask on Apache. I tested my Apache configuration successfully, but I cannot run it with the following Flask application. Therefore I didn't post this question to server fault but to Stack Overflow.

Calling http://54.68.62.180 throws the following error:

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

For the records, my Apache configuration AppName.conf file looks like this:

<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:

#!/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:

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. 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.

The routing is specified in appname/appname/ init .py through:

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

and in

appname/appname/frontend/__init__.py

the html source is routed through:

@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?

Assuming the application.py script you posted is not redacted, you're missing the actual route. 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:

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

See the quickstart guide .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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