gunicorn with gaiohttp worker always returning 404 with flask app gunicorn with gaiohttp worker always returning 404 with flask app flask flask

gunicorn with gaiohttp worker always returning 404 with flask app


I managed to work this out.

The line here was causing the issue in the nginx config:

rewrite /app(.*) /$1 break;

And I needed a proxy middleware in my Flask app to handle the reverse proxy correctly.

class ReverseProxied(object):'''Wrap the application in this middleware and configure thefront-end server to add these headers, to let you quietly bindthis to a URL other than / and to an HTTP scheme that isdifferent than what is used locally.In nginx:location /myprefix {    proxy_pass http://192.168.0.1:5001;    proxy_set_header Host $host;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    proxy_set_header X-Scheme $scheme;    proxy_set_header X-Script-Name /myprefix;    }:param app: the WSGI application'''def __init__(self, app):    self.app = appdef __call__(self, environ, start_response):    script_name = environ.get('HTTP_X_SCRIPT_NAME', '')    if script_name:        server = environ.get('HTTP_X_FORWARDED_SERVER', '')        if server:            environ['HTTP_HOST'] = server        environ['SCRIPT_NAME'] = script_name        path_info = environ['PATH_INFO']        if path_info.startswith(script_name):            environ['PATH_INFO'] = path_info[len(script_name):]    scheme = environ.get('HTTP_X_SCHEME', '')    if scheme:        environ['wsgi.url_scheme'] = scheme    return self.app(environ, start_response)

In the __init__.py of the app: app.wsgi_app = ReverseProxied(app.wsgi_app)


Another solution is to Add a prefix to all Flask routes.

Sub-mounted inside of another WSGI container

Assuming that you are going to run this application inside of a WSGI container (mod_wsgi, uwsgi, gunicorn, etc); you need to actually mount, at that prefix the application as a sub-part of that WSGI container (anything that speaks WSGI will do) and to set your APPLICATION_ROOT config value to your prefix:

app.config["APPLICATION_ROOT"] = "/app"@app.route("/")def index():    return "The URL for this page is {}".format(url_for("index"))# Will return "The URL for this page is /app"

Setting the APPLICATION_ROOT config value simply limit Flask's session cookie to that URL prefix. Everything else will be automatically handled for you by Flask and Werkzeug's excellent WSGI handling capabilities.

An example of properly sub-mounting your app

If you are not sure what the first paragraph means, take a look at this example application with Flask mounted inside of it:

from flask import Flask, url_forfrom werkzeug.serving import run_simplefrom werkzeug.wsgi import DispatcherMiddlewareapp = Flask(__name__)app.config['APPLICATION_ROOT'] = '/app'@app.route('/')def index():    return 'The URL for this page is {}'.format(url_for('index'))def simple(env, resp):    resp(b'200 OK', [(b'Content-Type', b'text/plain')])    return [b'Hello WSGI World']app.wsgi_app = DispatcherMiddleware(simple, {'/app': app.wsgi_app})if __name__ == '__main__':    app.run('localhost', 5000)

Proxying requests to the app

If, on the other hand, you will be running your Flask application at the root of its WSGI container and proxying requests to it (for example, if it's being FastCGI'd to, or if nginx is proxy_pass-ing requests for a sub-endpoint to your stand-alone uwsgi / gevent server then you can either:

  • Use a Blueprint, as Miguel points out in his answer.
  • or use the DispatcherMiddleware from werkzeug (or the PrefixMiddleware from su27's answer) to sub-mount your application in the stand-alone WSGI server you're using. (See An example of properly sub-mounting your app above for the code to use).