Skip to content

Q&A · Answered

How to add HTTPS to Django with Nginx

asked 9 months, 1 week ago 2605 views 1 answer
45

I need to enable HTTPS for my Django site. How do I configure SSL?

1 Answer

One accepted
19

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago
# Get SSL certificate
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
# nginx configuration
server {
    listen 443 ssl;
    server_name yourdomain.com;
    
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    
    location / {
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
# settings.py
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.