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

news/2024/7/10 1:05:51 标签: nginx, 负载均衡, linux, python, 腾讯云

进入/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:

python">"""
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来配置。

python">"""
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可以参考下面:

python">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服务器去处理。
部署完成。


http://www.niftyadmin.cn/n/1026759.html

相关文章

基于Apache在本地配置多个虚拟主机站点

简单的说,打开httpd.conf 在最后加入如下内容: <VirtualHost 127.0.0.2:80> DocumentRoot d:/AppServ/www2 ServerName 127.0.0.2:80< irtualHost> <Directory "d:/AppServ/www2"> Options Indexes FollowSymLinks Multiviews AllowOv…

【Windows核心编程】一个使用内存映射文件进行进程间通信的例子

进程间通信的方式有很多种&#xff0c;其底层原理使用的都是内存映射文件。 本文实现了Windows核心编程第五版475页上的demo&#xff0c;即使用内存映射文件来在进程间通信。 进程1 按钮【Create mapping of Data】用来创建命名内存映射文件&#xff0c;后备存储器为页交换文件…

【深度学习模型】下载部署efficientnet.h5

问题 Downloading data from https://storage.googleapis.com/keras-applications/efficientnet.h5 Exception: URL fetch failure on https://storage.googleapis.com/keras-applications/efficientnet.h5: None -- unknown url type: https unable to load app 0 (mountpoin…

【Linux】硬链接和符号链接(软连接)都可以修改原文件吗?--相同点与不同点

答案是都可以修改 测试 测试命令如下 $ cd /tmp # 所有人都有rwx权限 $ touch test.txt $ vim test.txt #写入&#xff1a; this is the test file $ ln test.txt hardlink.txt #硬链接 $ ln -s test.txt symboliclink.txt #符号链接 $ vim symboliclink.txt # 换行写入s…

hdu 1513 1159 poj Palindrome (dp, 滚动数组, LCS)

题目 以前做过的一道题&#xff0c; 今天又加了一种方法 整理了一下。。。。。 题意&#xff1a;给出一个字符串&#xff0c;问要将这个字符串变成回文串要添加最少几个字符。 方法一&#xff1a; 将该字符串与其反转求一次LCS&#xff0c;然后所求就是n减去 最长公共子串的长度…

【ULINK2仿真器stm32编程下载器】---- keil无法识别问题

之前由于JLINK仿真器有点问题&#xff0c;换了一个ULINK2仿真器&#xff1a;图片来源http://www.elecfans.com/d/1094778.html 使用的JTAG连接器是调试ARM 的标准&#xff08;2.54mm/0.1"&#xff09;20-针连接器&#xff0c;由于自己的板子接了一个外设&#xff0c;板子…

SQL Server 2008中的hierarchyid

这也是SQL Server 2008的一个重要新增特性。主要解决的问题是拥有层次关系的表格。例如我们日常生活中用到最多的组织结构图。我们一般会用一个Employees表保存员工数据&#xff0c;而每个员工则又可能会有相应的上级。以前要得到某个员工的所有上级&#xff0c;或者所有下级&a…

【Linux(bash)】中容易被忽略但又超好用的命令【持续更新】

Linux(bash)中容易被忽略但又超好用的命令【持续更新】 目录Linux(bash)中容易被忽略但又超好用的命令【持续更新】1 常用2 文件更新自动显示3 纯文本文件的多窗口功能4 按文件大小排序列出5 打包时排除掉某些文件6 借助alias,bash变量完成快捷操作6.1 使用alias设置命令别名6.…