Configure SSL for Python Flask
Flask SSL in production means TLS in front of the app, not inside it. Flask's built-in server is for development, and ProxyFix plus the cookie flags are what make HTTPS work behind a proxy.
Development: a local HTTPS server
For testing things that need a secure context (service workers, secure cookies, OAuth callbacks), you can pass a certificate and key to the dev server:
flask run --cert=cert.pem --key=key.pem
# or generate a throwaway certificate for local use
flask run --cert=adhocThe adhoc option needs the cryptography package installed and produces a certificate your browser will not trust. For a certificate your browser does trust locally, a tool such as mkcert is easier. Never use these files on a public server. Flask's own documentation warns that the dev server is not suitable for production.
Flask SSL: the production layout
A common setup is: browser to Nginx on 443, Nginx to Gunicorn on a Unix socket or localhost port. Nginx holds the certificate. If you need a certificate, Certbot can issue a free one. The Nginx part looks like this:
server {
listen 443 ssl;
server_name app.example.com;
ssl_certificate /etc/ssl/app.example.com/fullchain.pem;
ssl_certificate_key /etc/ssl/app.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Start the app with gunicorn -b 127.0.0.1:8000 app:app. Gunicorn can also terminate TLS itself with --certfile and --keyfile, which is acceptable for small internal tools, but a proxy gives you better renewal handling and static file serving.
Flask SSL: make Flask aware of the proxy
Behind a proxy, Flask sees plain HTTP from 127.0.0.1. Without help, url_for(..., _external=True) builds http:// links and request.is_secure is false. Werkzeug ships a fix:
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1
)Set the counts to the number of proxies you actually have. Trusting forwarded headers when none of your proxies set them lets clients lie about their address and scheme.
Secure cookie and session settings
| Setting | Value | Why |
|---|---|---|
| SESSION_COOKIE_SECURE | True | Cookie is sent only over HTTPS |
| SESSION_COOKIE_HTTPONLY | True | Hidden from JavaScript |
| SESSION_COOKIE_SAMESITE | "Lax" | Limits cross-site sending |
| PREFERRED_URL_SCHEME | "https" | Links built outside a request use HTTPS |
In code: app.config.update(SESSION_COOKIE_SECURE=True, SESSION_COOKIE_HTTPONLY=True, SESSION_COOKIE_SAMESITE="Lax"). Setting the secure flag on a site that is still reachable over HTTP will make login appear broken over HTTP, which is the point.
Redirect HTTP and add HSTS
Handle the redirect in Nginx with a port 80 server block that returns a 301. For HSTS you can add the header in Nginx, or use the Flask-Talisman extension, which also sets a content security policy. Check how to enable HSTS before using a long max-age, because it is hard to undo.
Calling other HTTPS services from Flask
The requests library verifies certificates by default. If you hit an error with an internal service that uses a private CA, pass verify="/path/to/ca.pem". Do not pass verify=False in code that ships; it disables hostname and chain checks.
Check your work
Run curl -vI https://app.example.com and confirm the protocol and issuer. Then run openssl s_client -connect app.example.com:443 -servername app.example.com to look at the chain; this OpenSSL testing guide explains the output. The free SSL chain checker will flag a missing intermediate certificate, which is the most common mistake after a manual setup.
Finally, make sure renewals reload Nginx. A certificate that renews on disk but is never reloaded still expires in front of your users.
The certificate paths in the examples are shortened for readability. With Certbot the real files sit under /etc/letsencrypt/live/ in a folder named after your domain. Point the config at those files, or at copies placed by a deploy hook.
Common mistakes
- Deploying with
flask runorapp.run(ssl_context=...)on a public server. - Forgetting
ProxyFix, so redirects and external URLs use http://. - Using
adhoccertificates anywhere other than a local machine. - Setting
SESSION_COOKIE_SECUREbefore HTTPS is working for every user.
Related reading: SSL for MySQL, free SSL on nginx and SSL for SMTP.
Frequently Asked Questions
Should Flask serve HTTPS itself?
No in production. The built-in server is for development. Run Gunicorn behind Nginx, or let the WSGI server handle TLS.
Why do my links come out as http?
Flask sees plain HTTP from the proxy. Wrap the app in ProxyFix so it trusts the forwarded protocol header.
How do I keep sessions secure over HTTPS?
Set SESSION_COOKIE_SECURE and SESSION_COOKIE_HTTPONLY, and serve only over HTTPS so the cookie never travels in clear text.
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.