daphne部署异步django

调试开关与静态文件

https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail

  • 调试模式下(DEBUG=True),默认提供静态文件服务

  • 生产模式下(DEBUG=False),默认不提供静态文件服务

    • 由nginx,apache提供文件服务

    • 改变运行方式

      python manage.py runserver --insecure
      
    • 添加静态文件路由

      from django.views import static
      from django.conf import settings
      from django.conf.urls import url
      
      url(r'^static/(?P<path>.*)$',
              static.serve, {'document_root': settings.STATIC_ROOT},
              name='static'),
      

nginx+daphne部署异步django项目

https://www.upidev.com/tutoriels/set-up-a-django-async-project-with-daphne-nginx-on-ubuntu-18-04/

django_with_daphne_nginx

请求响应链路图

  1. 启动django

    daphne --proxy-headers mydjango.asgi:application
    
  2. 域名服务

    # C:\Windows\System32\drivers\etc\hosts文件加一行
    127.0.0.1       myspider.com
    
  3. 配置nginx

    # nginx.conf
    
    server {
           listen 80;
           server_name myspider.com;
           
           charset     utf-8;
           
           client_max_body_size 75M;   # adjust to taste
    
           # Django media
           location /static/ {
               alias D:\myspider\mydjango\static\\; 
           }
    
           location / {
              try_files $uri @proxy_to_app;
           }
    
           location @proxy_to_app {
               proxy_pass http://127.0.0.1:8000;
    
               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "upgrade";
    
               proxy_redirect off;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header X-Forwarded-Host $server_name;
           }
        }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Daphne 是一个由 Django Channels 开发的基于 ASGI(异步服务器网关接口)协议的Web服务器,可以用于部署 Django异步应用程序。 以下是在 Windows 上使用 Daphne 部署 Django 项目的步骤: 1. 安装 Daphne 可以通过 pip 命令安装 Daphne: ``` pip install daphne ``` 2. 在 Django 项目的根目录下创建一个名为 `run_daphne.bat` 的批处理文件,并将以下内容复制到该文件中: ``` @echo off set DJANGO_SETTINGS_MODULE=<your_project_name>.settings daphne -b 0.0.0.0 -p 8000 <your_project_name>.asgi:application ``` 其中 `<your_project_name>` 是你的 Django 项目的名称。 3. 运行 `run_daphne.bat` 文件 双击 `run_daphne.bat` 文件或在命令行中输入以下命令来启动 Daphne 服务器: ``` run_daphne.bat ``` 如果一切正常,你将看到类似于以下内容的输出: ``` 2021-06-23 17:39:20,959 INFO Starting server at tcp:port=8000:interface=0.0.0.0 2021-06-23 17:39:20,960 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras) 2021-06-23 17:39:20,960 INFO Using busy-loop synchronous mode on channel 1 2021-06-23 17:39:20,960 INFO Using busy-loop synchronous mode on channel 2 2021-06-23 17:39:20,960 INFO Listening on endpoint tcp:port=8000:interface=0.0.0.0 ``` 4. 测试是否能够访问 Django 项目 在浏览器中输入 `http://localhost:8000`,如果一切正常,你应该能够看到 Django 项目的首页。 注意:如果你使用的是 Windows 防火墙,请确保已允许 Daphne 服务器通过端口 8000 进行通信。 以上是在 Windows 上使用 Daphne 部署 Django 项目的简单步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值