Make Flask's url_for use the 'https' scheme in an AWS load balancer without messing with SSLify

后端 未结 2 982
梦谈多话
梦谈多话 2020-12-25 12:43

I\'ve recently added a SSL certificate to my webapp. It\'s deployed on Amazon Web Services uses load balancers. The load balancers work as reverse proxies, handling external

2条回答
  •  忘掉有多难
    2020-12-25 13:01

    I was having these same issues with `redirect(url_for('URL'))' behind an AWS Elastic Load Balancer recently & I solved it this using the werkzeug.contrib.fixers.ProxyFix call in my code. example:

    from werkzeug.contrib.fixers import ProxyFix
    app = Flask(__name__)
    
    app.wsgi_app = ProxyFix(app.wsgi_app)
    

    The ProxyFix(app.wsgi_app) adds HTTP proxy support to an application that was not designed with HTTP proxies in mind. It sets REMOTE_ADDR, HTTP_HOST from X-Forwarded headers.

    Example:

    from werkzeug.middleware.proxy_fix import ProxyFix
    # App is behind one proxy that sets the -For and -Host headers.
    app = ProxyFix(app, x_for=1, x_host=1)
    

提交回复
热议问题