Running a Flask app over HTTPS seems like it should be a simple exercise, however there are few recent guides covering this topic. Here are the steps I took to run a Flask app over HTTPS.

We create an instance of a Flask App. The argument passed into the Flask instantiator (name) evaluates to a string that 'names' the Flask App. When run from the command line, name 'main'. You can set the first argument to whatever you want. Kubectl to deploy and manage apps on Kubernetes; If you’re on a Mac. This spec will create a new Deployment for a Flask app with a single replica (Pod). Take note of the containers section. Here, we specified the Docker image along with the container port the application will run on. (compare and contrast running Flask on Docker Swarm.

Here are the Basic steps.

  1. Set up a Flask App.
  2. Run it with Waitress.
  3. Use a reverse proxy with NGINX.
  4. Set up an SSL.
  5. Configure the NGINX server rules.

This guide assumes you already have a Flask app up and running. If you don't here is a simple Flask App.

Running this should give you the following.

Okay we have our Flask app running, why not just use this server? Per Flask documentation:

'While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well. Some of the options available for properly running Flask in production are documented here.'

One of the most simple solutions to this is to use Waitress Feel free to read the official guide on your own. But for this example we only need to make some minor changes. First install Waitress.

Next we need to make some changes to our Flask app to let it know we are using Waitress.

Run Flask App Command Line

Now we have our Flask app running with a production ready server. But we still need our SSL. If you don't have one already, you can use Certbot or use a self-signed Cert.

We will make one final change to our Flask app. We need to let waitress know we will be using https. Documentation. We add url_scheme='https' to our app.

Now we have our SSL and are ready to use NGINX. If you don't already have it installed here is how to set it up.

Our Web Server rules will be at

CD there and delete the example.

These will be our rules. Note

example.com & your.ip

Run flask application in production

Is just an example use your own domain/server address

Note on this line.

Run Flask Application In Production

proxy_pass http://your.ip.adress:5000;

If you are running your app on a different port you need to make sure these match. For example if you are using port 3000 it would be

proxy_pass http://your.ip.adress:3000;

Flask App Api

Okay all of the configurations are done. But we need to let NGINX know, so we restart our server like so.

Run Flask App Mac Os

App

sudo systemctl status nginx

Run Flask App Mac Free

Refresh and you should see the new server running with the SSL.