Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

Serving Flask under a subpath

Your Flask app object implements the __call__ method, which means it can be called like a regular function. When your WSGI container receives a HTTP request it calls your app with the environ dict and the start_response callable. WSGI is specified in PEP 0333. The two relevant environ variables are:

The initial portion of the request URL's "path" that corresponds to the application object, so that the application knows its virtual "location". This may be an empty string, if the application corresponds to the "root" of the server.

The remainder of the request URL's "path", designating the virtual "location" of the request's target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash.

Flask's routing and url_for are provided by Werkzeug. Werkzeug's routing operates on PATH_INFO and its url_for function prepends SCRIPT_NAME.


This means that if your application isn't located at the root of your server but under a path you have to tell your WSGI container the env SCRIPT_NAME. It will then split incoming request paths into SCRIPT_NAME and PATH_INFO.

In production
How you do this depends on your deployment option. Eg. with Gunicorn you can pass environment variables with -e.

gunicorn -e SCRIPT_NAME=/my-app my_app:app

With Flask's dev server
Flask's builtin development server uses werkzeug.serving.run_simple(). In order to use the convenient debugging and reloading features while still serving the site under a SCRIPT_NAME you would have to use a WSGI middleware that sets the SCRIPT_NAME and trims the PATH_INFO.

from werkzeug.serving import run_simple
from my_app import app

class FixScriptName(object):
	def __init__(self, app): = app

	def __call__(self, environ, start_response):
		SCRIPT_NAME = '/my-app'
		if environ['PATH_INFO'].startswith(SCRIPT_NAME):
			environ['PATH_INFO'] = environ['PATH_INFO'][len(SCRIPT_NAME):]
			return, start_response)
			start_response('404', [('Content-Type', 'text/plain')])
			return ["This doesn't get served by your FixScriptName middleware.".encode()]

app = FixScriptName(app)

run_simple('', 5000, app, use_reloader=True)

This comment has been minimized.

Copy link

@roemhildtg roemhildtg commented Nov 22, 2016

Thanks for posting this, this has solved 90% of my issues. In a few cases, I have some code like this though, where the redirect is not working:

from flask import request, redirect, url_for
def route():
    return redirect(url_for('login', next=request.full_path)

It turns out, request.full_path does not have the prefix in the value. It looks like this: /route when it should be /prefix/route.

What's the best way to handle this scenario?


I guess this is solvable by using request.url. Not sure if its the best, but it works :)


This comment has been minimized.

Copy link

@kylerlaird kylerlaird commented Sep 1, 2020

Thank you!!! This was a huge help for me. (I'm trying to get a Flask app running.)


This comment has been minimized.

Copy link

@kevenv kevenv commented Sep 30, 2020

Thanks, it was not obvious to me that you have to run this using python instead of the usual flask run.


This comment has been minimized.

Copy link

@m3m0ry m3m0ry commented Dec 22, 2020

I have a simpler solution. Make your app a blueprint. Use app.register_blueprint(your_blueprint, url_prefix=YOUR_PREFIX)

Or see here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment