How to run streamlit in https mode?

Thanks all! I was running on AWS, but my domain was NOT in AWS (squarespace), so doing the load balancer route gets complicated - you have to do a Network Load Balancer in front of, and I couldn’t figure how to get all the balancers and targets to seeming work together. So I heavily borrowed from @Dhruv4 's great post - here was my entire thing, beginning to end, on a clean machine

  • Start a small instance on AWS using a stock ubuntu AMI
    • Set up a security group that allows HTTP, HTTPS, and SSH traffic
  • Register an Elastic IP address, and assign it to the instance
  • Go to domain registrar, and assign @ and www to the IP address
  • Log into the machine, and then
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install certbot python3-certbot-nginx python3-pip -y
sudo certbot --nginx -d yoursite.extension -d www.yoursite.extension
  • Add this block to the http section of /etc/nginx/nginx.conf:
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
    server {
        listen 443 ssl;
        server_name yoursite.extension;

        ssl_certificate /etc/letsencrypt/live/yoursite.extension/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/yoursite.extension/privkey.pem;

        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers HIGH:!aNULL:!MD5;

        location / {
            proxy_pass http://localhost:8501;
            proxy_http_version 1.1;

            # WebSocket support
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

            # Standard proxy headers
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }

    server {
        listen 80;
        server_name yoursite.extension;
        return 301 https://$host$request_uri;  # Redirect all HTTP traffic to HTTPS
    }
  • Finally:
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t && sudo nginx -s reload
# make sure it's not returning an error here, of course! 

tmux new -s yourapp
python3 -m pip install streamlit
git clone https://github.com/yourorg/yourrepo.git
cd yourrepo
python3 -m pip install -r requirements.txt 
streamlit run some_appfile.py
2 Likes