Search code examples
pythonflaskwsgicaddywaitress

Flask in production with Caddy and Waitress url_for redirect to localhost


I have a Flask server running on a Windows EC2 instance.

I need to have it using https protocol, so my setup is like this :

  • I have a Caddy server
  • I use Waitress to run my flask app

Here is my config for Caddy :

example.com:443{
    proxy / 127.0.0.1:8080
    tls [email protected]
}

Everything is working fine excepted that in my application.py file, when I do :

return redirect(url_for('test', filename=filename))

My web browser redirect me :

Same result with _external=True

But on a template page, for example https://example.com/test2 rendered using render_template( "test2.html") if I have a link <a href="{{ url_for('index') }}" /> the HTML generated is well : https://example.com/

For now I have hard coded my url in application.py but this is not the way that I would like to keep it...


Solution

  • I have updated my Caddyfile to this and now it's working great !

    example.com:443 {
        proxy / 127.0.0.1:8080 { 
            header_upstream Host {host} 
            header_upstream X-Real-IP {remote} 
            header_upstream X-Forwarded-For {remote} 
            websocket 
        }
            tls [email protected]
    }
    

    This answer gave me a hint.

    This answer helped me build the Caddyfile.