You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地通过HTTPS运行Django应用遇阻,RunServerPlus加载站点失败求助

解决Django RunServerPlus HTTPS无法加载站点问题

问题背景

已安装Django Extensions、Werkzeug和pyOpenSSL,配置如下:

INSTALLED_APPS = [
# ...
 'django_extensions',
]
ALLOWED_HOSTS = ['mysite.com', 'localhost', '127.0.0.1']

执行 runserver_plus --cert-file cert.crt 后,本地的mysite.com站点完全无法加载。

排查与解决步骤

1. 确认证书文件配置

  • 自签名证书需要同时指定证书和私钥文件,仅提供cert.crt会导致服务启动异常,执行命令需补充私钥:
    runserver_plus --cert-file cert.crt --key-file key.key
    
  • 若没有现成证书,可直接让RunServerPlus自动生成自签名证书,执行:
    runserver_plus --ssl
    
    生成的localhost.crt和localhost.key会保存在项目根目录,后续可复用。

2. 本地DNS映射配置

  • 确保本地hosts文件已添加mysite.com到127.0.0.1的映射:
    127.0.0.1 mysite.com
    
    不同系统的hosts路径:
    • Windows:C:\Windows\System32\drivers\etc\hosts
    • macOS/Linux:/etc/hosts

3. 访问地址与端口检查

  • RunServerPlus默认使用8000端口,访问时需明确使用HTTPS协议和对应端口:
    https://mysite.com:8000
    
  • 若自定义了端口,比如9000,启动命令和访问地址需对应:
    runserver_plus 0.0.0.0:9000 --cert-file cert.crt --key-file key.key
    
    访问地址为 https://mysite.com:9000

4. 浏览器证书信任处理

  • 自签名证书会被浏览器判定为不安全,需手动确认访问:
    • 打开地址后,在浏览器的风险提示页面选择「高级」-「继续访问」(不同浏览器选项表述略有不同)
    • 也可将cert.crt导入浏览器的根证书信任列表,彻底解决提示问题

5. 依赖与配置兼容性检查

  • 更新相关依赖到稳定版,避免版本兼容问题:
    pip install --upgrade django-extensions werkzeug pyopenssl
    
  • 若使用0.0.0.0启动服务,需将其添加到ALLOWED_HOSTS:
    ALLOWED_HOSTS = ['mysite.com', 'localhost', '127.0.0.1', '0.0.0.0']
    

内容的提问来源于stack exchange,提问作者Geoffrey Odiwuor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 09:01:04