Skip to content

Instantly share code, notes, and snippets.

@vehrka
Created December 15, 2015 09:48
Show Gist options
  • Save vehrka/7799b263a32124e7b10e to your computer and use it in GitHub Desktop.
Save vehrka/7799b263a32124e7b10e to your computer and use it in GitHub Desktop.
Flask on Gunicorn behind Apache Proxy

Apache conf

<Proxy *>
  Order deny,allow
  Allow from all
</Proxy>
ProxyPreserveHost On
<Location "/myapp">
  ProxyPass "http://127.0.0.1:4000/myapp/"
  ProxyPassReverse "http://127.0.0.1:4000/myapp/"
</Location>

Flask

Reference http://flask.pocoo.org/snippets/84/

class CherrokeeFix(object):

    def __init__(self, app, script_name):
        self.app = app
        self.script_name = script_name

    def __call__(self, environ, start_response):
        path = environ.get('SCRIPT_NAME', '') + environ.get('PATH_INFO', '')
        environ['SCRIPT_NAME'] = self.script_name
        environ['PATH_INFO'] = path[len(self.script_name):]
        # assert path[:len(self.script_name)] == self.script_name
        return self.app(environ, start_response)

And then

app = Flask(__name__)
app.wsgi_app = CherrokeeFix(app.wsgi_app, '/myapp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment