Flask-Python redirection to https The connection was reset Flask-Python redirection to https The connection was reset flask flask

Flask-Python redirection to https The connection was reset


You cannot do this using ssl_context and Werkzung (default server of Flask) now. A functionality to allow this was proposed and rejected in 2014: auto http to https redirect; citing:

That requires running another HTTP server. Werkzeug is not capable of that and IMO it's out of scope. run_simple should only be used for development anyway.

So what's going on is your Flask application calls run_simple underneath, passing ssl_context and some other variables. SSLify has no impact on your routing as long as you use ssl_context, because sole presence of this variable makes Werkzung host only using https schema. To get redirection from http to https, you need either to set up another server, listening at http and redirecting to https or migrate to other, more advanced server which allows redirection easily.

I recommend to migrate to Apache or gunicorn. Flask provides comprehensive instructions on deployment: Deployment Options. Keep in mind that built-in server of Flask (Werkzung) is not suitable for production, as authors of Flask write:

While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time.

Using Apache you could redirect all http requests using VirtualHost rule, listening at 80:

<VirtualHost *:80>   ServerName mysite.example.com   DocumentRoot /usr/local/apache2/htdocs   Redirect /secure https://mysite.example.com/secure</VirtualHost>

See more on this on Redirect Request to SSL Apache wiki.