Last active
August 28, 2021 21:20
-
-
Save jrialland/bf017843bbead538c20c382a19d880fe to your computer and use it in GitHub Desktop.
Flask 1.1.x reverse proxy fix path prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
location /flask { | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header X-Forwarded-Proto $scheme; | |
proxy_set_header X-Forwarded-Host $host; | |
proxy_set_header X-Forwarded-Port $server_port; | |
proxy_set_header X-Forwarded-Prefix /flask; | |
proxy_pass http://localhost:4000; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from werkzeug.middleware.proxy_fix import ProxyFix | |
class WsgiFlaskPrefixFix(ProxyFix): | |
def __init__(self, flaskapp): | |
super().__init__(flaskapp.wsgi_app, x_host=1, x_port=1, x_prefix=1) | |
self.flaskapp = flaskapp | |
def __call__(self, environ, start_response): | |
prefix = environ.get('HTTP_X_SCRIPT_NAME') or environ.get('HTTP_X_FORWARDED_PREFIX') | |
if prefix: | |
self.flaskapp.config['APPLICATION_ROOT'] = environ['SCRIPT_NAME'] = prefix | |
environ['PATH_INFO'] = environ['PATH_INFO'][len(prefix):] | |
return super().__call__(environ, start_response) | |
application = WsgiFlaskPrefixFix(app) #declared wsgi callable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment