【Django】同时部署Daphne,uWSGI并由Nginx服务器反向代理转发

本文介绍如何使用Nginx、uWSGI和Daphne部署Django应用,包括配置Nginx转发HTTP和WebSocket请求,以及设置Django的ASGI配置。

进入/etc/nginx/conf.d/*.conf
nginx的*.conf配置如下:

upstream daphne{
   # nginx通过socket在环回接口地址的9000端口与本地的daphne进程通信
   # 支持ip:port模式以及socket file模式
   server 127.0.0.1:9000;
}
upstream uwsgi{
   # nginx通过socket在环回接口地址的9001端口与本地的uWSGI进程通信
   # 支持ip:port模式以及socket file模式
   server 127.0.0.1:9001;
}
server {

    listen 9090;   #nginx服务器的监听端口
    server_name sever_on_cloud ; #定义自己的网站域名(可以自己取)
    
    access_log /var/log/nginx/access.log;
    charset utf-8;
  
    gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    
    # nginx转发动态请求到uWSGI
    location / {
        include uwsgi_params;
        uwsgi_connect_timeout 60;
        uwsgi_pass uwsgi;
    }

    #nginx转发动态请求到Daphne
    location /ws{
        proxy_pass http://daphne;
        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;
        # proxy_read_timeout 60s;#默认为60s
        # proxy_send_timeout 60s;#默认为60s
    }
    
    # 如果写成/static/,nginx无法找到项目静态文件路径
    location /static {
        alias /opt/app/static;
    }
    
    # 如果写成/media/,nginx无法找到项目媒体文件路径
    location /media {
        alias /opt/app/media;
    }
}

此外还要配置Django中的asgi.py:

"""
ASGI config for app_backen project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'appserver.settings')
django.setup()

import (yourapp)
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from (yourapp) import routing

#django_application = get_asgi_application()
application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            (yourapp).routing.websocket_urlpatterns
        )
    ),
})

也有些文章是按照下面这样配置的,两种配置方式都可以,不过按照上面配置的话,可以在不部署nginx的情况下,只靠daphne完成两种类型的连接的处理。当然在部署nginx的情况下上面的配置也可以正常运行,不需要更改。建议按照上面的asgi来配置。

"""
ASGI config for app_backen project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'appserver.settings')
django.setup()

from django.core.asgi import get_asgi_application

#django_application = get_asgi_application()
application = get_default_application()

注意两个点:一个是django的import和setup()要放前面;另一个是yourapp换成自己的app名字。

另外还需要写一个routing.py,routing.py可以参考下面:

from django.urls import re_path

from . import consumer  #需要自己写consumer.py

websocket_urlpatterns = [
    re_path(r'ws/(yourapp)/(连接名)/', consumer.Consumer.as_asgi()),  
]

consumer就不贴了。

配置完成,分别开启nginx,uwsgi和daphne之后,前端将请求发送到9090端口,nginx会自动将ws开头的请求转发到9000端口给daphne服务器去处理,普通的http的请求转发到9001端口给uwsgi服务器去处理。
部署完成。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值