Running webapp2 app in a multiple WSGI apps set up with Werkzeug
Question: I am trying to run a django app and a webapp2 app together in one python
interpreter. I'm using werkzeug for that as described
[here](http://flask.pocoo.org/docs/patterns/appdispatch/).

Here's my sample code.

    
    
    from werkzeug.wsgi import DispatcherMiddleware
    from django_app import application as djangoapp
    from webapp2_app import application as webapp2app
    
    application = DispatcherMiddleware(djangoapp, {
        '/backend':     webapp2app
    })
    

After doing this, I would expect all requests to /backend should be treated by
the webapp2 app as /. But it treats the requests as /backend. This work fines
with other WSGI apps using django or flask. The problem only appears with
webapp2 apps. Does anyone have any suggestions how to overcome this? Is there
any other way I can achieve my purpose without using werkzeug for serving
multiple WSGI apps under one domain?


Answer: `DispatcherMiddleware` fabricates environments for your apps and especially
`SCRIPT_NAME`. Django can deal with it with configuration varibale
`FORCE_SCRIPT_NAME = ''`
([docs](https://docs.djangoproject.com/en/1.5/ref/settings/#force-script-
name)).

With Webapp2 it's slightly more complicated. You can create subclass of
`webapp2.WSGIApplication` and override `__call__()` method and force
`SCRIPT_NAME` to desired value. So in your `webapp2_app.py` it could be like
this

    
    
    import webapp2
    
    class WSGIApp(webapp2.WSGIApplication):
    
        def __call__(self, environ, start_response):
            environ['SCRIPT_NAME'] = ''
            return super(WSGIApp, self).__call__(environ, start_response)
    
    # app = WSGIApp(...)
    

