45

I have a website that uses Flask. It used to work well, but since recently, every request returns a 404, and it seems it can't find the right endpoints. However:

  • Locally, the site still works, only on my VPS it shows this strange behaviour.
  • url_for works and app.view_functions contains all the routes as well.
  • And yet, I keep getting 404s on the VPS, even for / and anything under /static/.

Here's part of the code, it's a bit much to show all of it and it's not all relevant:

#snip

from flask import Flask, render_template, abort, request, redirect, url_for, session
from flask.ext.babelex import Babel
from flask.ext import babelex

#snip

app = Flask(__name__)
app.secret_key = #snip

#snip

#just one of the routes
@app.route('/')
def about():
    return render_template('about.html')

#snip

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

#snip

if __name__ == '__main__':
    app.run(debug=True)
else:
    app.config.update(
        SERVER_NAME='snip.snip.com:80',
        APPLICATION_ROOT='/',
    )
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
  • have you had a look at the server logs? there should be some hint about what's going wrong there. – mata Jun 26 '14 at 18:06
  • I have. Unfortunately, they show nothing out of the ordinary (except that the status code of every access is 404). – Jasmijn Jun 26 '14 at 18:09
  • that's probably the access log, you need to look at the error log. – mata Jun 26 '14 at 18:17
  • Yes, that was the access log. However, there weren't any errors at all in the error logs (none relevant, that is: some notices about Apache restarting and the like, but nothing that happened during recent requests) – Jasmijn Jun 26 '14 at 18:30
  • How are you deploying your flask application in the VPS? Are you using uwsgi or gunicorn? Look in your wsgi server log, not the apache logs. Although if you are deploying with mod_wsgi in Apache, the errors should be in the apache error log. Are there separate error logs for each virtual host in Apache? If so, verify you're looking at the correct error log for your virtual host. Also, has the apache/wsgi configuration changed recently? – dcr Jun 27 '14 at 00:04
  • mod_wsgi. And I am, because the error log does show errors in the past (as well as errors I made when trying to fix things), and I haven't changed any configuration at all. And the problem doesn't seem to be the Flask installation either, as I've upgraded Flask and the problem remained. – Jasmijn Jun 27 '14 at 07:52
  • I have the same problem, I asked in an [issue](https://github.com/mitsuhiko/flask/issues/1554) – Alexis Benoist Aug 11 '15 at 12:18

8 Answers8

61

I had the same issue. I had it because I changed the parameters SERVER_NAMEof the config to a name which is not the name of the server.

You can solve this bug by removing SERVER_NAME from the config if you have it.

Alexis Benoist
  • 2,400
  • 2
  • 17
  • 23
  • 7
    SAVED ME! I had added it after I had a real URL, it was not working, even though it did match the URL. Removed it, and it started working. Thank you. – nycynik Mar 21 '16 at 02:44
  • 1
    I had the same behavior, 404s everywhere until I removed `SERVER_NAME` from the config. However, I don't understand why that actually creates this situation. Is there any explanation or documentation of this behavior? – npdoty Nov 30 '17 at 21:53
  • i have a brand new install of flask on python 3.6 pip3 18.1. i have not touched anything other than create a new app.py with 3 line of code creating a new app. it starts fine but get the 404 when i use the url from console – Sonic Soul Jan 12 '19 at 15:59
  • 1
    when i print it, it returns "None" print('server name ', app.config["SERVER_NAME"]) – Sonic Soul Jan 12 '19 at 16:31
  • Lol, I spent wayyyyyy too long trying to figure this out and a single '#' fixed it. Thanks – Ambiwlans Jun 09 '19 at 17:09
  • 1
    This also fixed my problem. Had started requiring use of (a correctly specified) "SERVER_NAME" param within my app in development, but hadn't specified it on the Staging instance. – Mr Chris Jul 23 '19 at 07:37
  • It is possible to test this with `pytest`? – Martin Thoma Jul 13 '20 at 14:09
  • For anyone who is still having trouble, if you have hosted on localhost (e.g. `http://127.0.0.1:5000/`), make sure your URL points to the correct `@app.route() name/function`. If you have a function `def index` and route `@app.route("/index")`, make sure you visit `http://127.0.0.1:5000/index` – Freddy Mcloughlan Feb 21 '22 at 07:54
19

Had the same issue because the following lines

if __name__ == '__main__':
    app.run(host='127.0.0.1', threaded=True)

were declared before the function and decorator.

Moving these lines in the very bottom of file helped me.

Alveona
  • 850
  • 8
  • 17
15

I know this is old, but I just ran into this same problem. Yours could be any number of issues but mine was that I had commented out the from app import views line in my __init__.py file. It gave me the same symptom: every endpoint that required @app.route and a view was responding with 404's. Static routes (.js and .css) were fine (that was my clue).

Scott
  • 3,204
  • 3
  • 31
  • 41
2

Extremely old by this point, but mine was related to bug in importing. I'm using blueprints and I had:

from app.auth import bp

instead of

from app.main import bp

I was importing the wrong bp/ routes

user7804097
  • 308
  • 1
  • 3
  • 17
2

I received this error from defining my flask_restful route inside the create_app method. I still don't quite understand why it didn't work but once I changed the scope / moved it outside as shown below it worked.

from flask import Flask
from flask_restful import Resource
from extensions import db, api
from users.resources import User

def create_app():
    app = Flask(__name__)
    app.config.from_object('settings')
    db.init_app(app)
    api.init_app(app)

    return app

api.add_resource(User, '/users') 
Braden Holt
  • 1,544
  • 1
  • 18
  • 32
1

Also check carefully the routes. If some does not end with a slash but you are calling it with a trailing slash, flask will return a 404.

I had this error following a url from an email client that (I don't know why) append a trailing slash to the urls.

See documentation.

pvilas
  • 812
  • 6
  • 7
0

I had this problem and in my case it was about my templates. I had an index page like this:

<div>
    <a href="index.html">Home</a> |
    <a href="login.html">Login</a> |
    <a href="register.html">Register</a>
    <hr>
</div>

I should have used url_for('index') instead of index.html.

<div>
    <a href="{{ url_for('index') }}">Home</a> |
    <a href="{{ url_for('login') }}">Login</a> |
    <a href="{{ url_for('register') }}">Register</a>
    <hr>
</div>
Daniel
  • 61
  • 10
0

For me, I was getting 404s on every request due to missing favicon.ico and manifest.json. The solution was to provide the missing favicon.ico, and remove the link rel refence to manifest.json.

I found this out by printing the path that was causing problems on every request in my @app.errorhandler(Exception):

from flask import request

@app.errorhandler(Exception)
def handle_exception(err):
  path = request.path # this var was shown to be 'favicon.ico' or 'manifest.json'
gene b.
  • 10,512
  • 21
  • 115
  • 227