小程序django后台开发https支持记录

后台我采用的是django
django 本身针对小程序,不需要特殊的配置。
但是nginx需要对https进行支持。
首先需要ssl证书,阿里云提供免费的ssl,可以使用。

image.png

点击下载,选择nginx的方式,会下载两个文件,一个.key 文件一个 .pem
将这两个文件上传到你的阿里云。
然后配置nginx
之前只配置了http所以是这样的

server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name x.xxx.55.85 www.xxxxx.com;
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /root/load567/media;  # 指向django的media目录
    }

    location /static {
        alias /root/load567/static; # 指向django的static目录
        #alias /root/m2m_svr/xadmin/static; # 指向xadmin的static目录
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     uwsgi_params; # the uwsgi_params file you installed
    }
}

为了支持https 我们只需添加一个server 并新增几行配置指定刚刚上传的.key和.pem

server {
    listen 443 ssl; # 监听443端口
    server_name xxx.xxxx.xx.xx www.xxxxx.com;
    ssl_certificate /root/load567/conf/nginx/cert/xxxxx.com.pem; # 证书地址
    ssl_certificate_key /root/load567/conf/nginx/cert/xxxxx.com.key; # 私有密钥文件地址

    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /root/load567/media;  # 指向django的media目录
    }

    location /static {
        alias /root/load567/static; # 指向django的static目录
        #alias /root/m2m_svr/xadmin/static; # 指向xadmin的static目录
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     uwsgi_params; # the uwsgi_params file you installed
    }
}

重启nginx
nginx -s reload :修改配置后重新加载生效

这样修改之后将地址由http改为https,用postman调用发现已经成功了。
但是微信小程序调用是依然报错。
原因是小程序需要你指明你调用的域名是安全的,所以要网页登录小程序
「小程序后台-开发-开发设置-服务器域名」

image.png
image.png
image.png

配置好了之后,我们到开发工具查看:

image.png

发现这里已经更新了
这时小程序也不报错了~~~

posted @   宋桓公  阅读(48)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示