Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask app gives ubiquitous 404 when proxied through nginx

I've got a flask app daemonized via supervisor. I want to proxy_pass a subfolder on the localhost to the flask app. The flask app runs correctly when run directly, however it gives 404 errors when called through the proxy. Here is the config file for nginx:

upstream apiserver {
    server 127.0.0.1:5000;
}

location /api {
            rewrite /api/(.*) /$1 break;
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://apiserver;
            proxy_next_upstream error timeout http_502;
            proxy_buffering off;

}

For instance, when I go to http://127.0.0.1:5000/me, I get a valid response from the app. However when I go to http://127.0.0.1/api/me I get a 404 from the flask app (not nginx). Also, the flask SERVER_NAME variable is set to 127.0.0.1:5000, if that's important.

I'd really appreciate any suggestions; I'm pretty stumped! If there's anything else I need to add, let me know!

like image 552
eatonphil Avatar asked Jan 02 '14 23:01

eatonphil


People also ask

How do I fix Nginx 404?

404 Not Found To solve this issue you have to open the Nginx configuration file of the website and double-check the document root. If the name of the document root is the one you want then, you need to check if the document root directory exists as is defined in the configuration file.

Why is Nginx returning 404?

Essentially, the “404 error” indicates that your or your visitor's web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.

Does Flask work with nginx?

Access Flask Application At this point, your Flask application is installed, configured, and hosted with an Nginx proxy. You can now access it using the URL http://flask.example.com.

How do you handle a 404 error Flask?

To handle 404 Error or invalid route error in Flask is to define a error handler for handling the 404 error. @app. errorhandler(404) def invalid_route(e): return "Invalid route." Now if you save the changes and try to access a non existing route, it will return “Invalid route” message.


2 Answers

Since Flask is handling the request, you could just add a little bit of information to the 404 error to help you understand what's passing through to the application and give you some real feedback about what effect your nginx configuration changes cause.

from flask import request

@app.errorhandler(404)
def page_not_found(error):
    return 'This route does not exist {}'.format(request.url), 404

So when you get a 404 page, it will helpfully tell you exactly what Flask was handling, which should help you to very quickly narrow down your problem.

like image 72
Doobeh Avatar answered Sep 27 '22 16:09

Doobeh


I suggest not setting SERVER_NAME.

If SERVER_NAME is set, it will 404 any requests that don't match the value.

like image 22
Zoidberg Avatar answered Sep 27 '22 17:09

Zoidberg