当前位置: 首页 > 工具软件 > uvicorn > 使用案例 >

DjangoREST使用Uvicorn部署笔记

姜锋
2023-12-01

前言

  • 我为什么选择使用uvicorn

Uvicorn是使用uvloop和httptools的闪电般快速的ASGI服务器实现。
https://www.uvicorn.org/

血与泪水的结晶,折腾到凌晨2点钟都没弄好一度怀疑自己的智商。

基础环境的安装

#最好先更新一下pip至最新版本
python -m pip install --upgrade pip
# 安装uvicorn和gunicorn
python -m pip install uvicorn gunicorn
# 如果安装缓慢或者超时,可以使用清华镜像加速
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple uvicorn gunicorn

Uvicorn

  • 使用Gunicorn启动服务是一个成熟的,功能齐全的服务器和流程管理器。
# 使用gunicorn启动uvicorn的服务
# 这将开始监听一个进程127.0.0.1:8000。它要求您的项目位于Python路径上;确保从与manage.py文件相同的目录中运行此命令。
# 使用gunicorn启动uvicorn的服务
 gunicorn DjangoREST.asgi:application -k uvicorn.workers.UvicornWorker

  • 注意:

在启动服务的时候,可能会报错,注意看错误信息,一般都是提示缺少什么模块啥的。这个时候不要慌张,无脑使用pip install安装缺少的模块就好了。

nginx

  • 配置文件参考
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
# user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    # include /etc/nginx/conf.d/*.conf;
    

    upstream DjangoREST {
        # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
        server 127.0.0.1:8000; # for a web port socket (we'll use this first)
    }

    server {
        listen       80 default_server;
        # listen       [::]:80 default_server;
        server_name  localhost;


        location /static {
            alias   /www/DjangoREST/static; # your Django project's static files - amend as required
        }
        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_redirect off;
            proxy_buffering off;
            proxy_pass http://DjangoREST;
        }

        


        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

}


  • 在配置nginx时,遇到问题时也不要慌张,第一时间去查看nginx的错误日志error.log。根据错误日志里的报错关键信息,进行搜索能够更加有效的解决问题。
#查找自己的nginx安装位置
find / -name nginx

参考

B站使用Nginx+uWSGI部署Django教学视频
Django uvicorn官方参考
Django with uWSGI demo tutorial

 类似资料: