Skip to main content
 首页 » 编程设计

python之Django 所有 URL SSL

2024年10月01日3452php

我已经在 digitalocean 上设置了一个 django 应用程序,使用最少的 nginx 和 gunicorn 设置。我正在使用 cloudflare 管理我的 dns,还使用 ​​cloudflare 的灵活 ssl。现在的问题是我所有的 django 生成的 url 都有 http url(http://example.com/favicon.ico),我怎样才能让所有的 url 都变成 https?

Nginx session :

server { 
listen       80; 
client_max_body_size 4G; 
server_name  ***; 
 
location / { 
    include proxy_params; 
    proxy_pass http://unix:**.sock; 
} 
location /static { 
    autoindex off; 
    alias **/static_files; 
} 
 
location /media { 
    autoindex off; 
    alias **/media; 
} 
 
error_page   500 502 503 504  /50x.html; 
location = /50x.html { 
    root   html; 
} 
} 

请您参考如下方法:

Django 确实提供了 SECURE_SSL_REDIRECT将所有 url 重定向到 https 的设置,

将此添加到您的 settings.py 文件中

if not DEBUG: 
    SECURE_SSL_REDIRECT = True 

此外,请查看 SSL 实现指南 here .