Regular app redirect to HTTPS?

I’m not familiar with Python / Flask, but I believe the following is happening:

  • You have Flask setup to handle HTTP requests
  • Ngrok is terminating your HTTPS requests and forwarding them on as HTTP requests to Flask
  • Flask thinks the user is using HTTP and is therefore generating HTTP redirect URLs

I think you need to use the X-Forwarded-Proto header which I believe Ngrok now supports. Essentially this header allows the proxy (Ngrok in this case) to tell the back-end server which scheme was used for the request and therefore what scheme to use for any generated URLs.

It appears that in Flask you just need to use the ProxyFix middleware component in order to handle this header:

Failing this, you could try setting the PREFERRED_URL_SCHEME.

Hope this helps.

2 Likes