聊天部署(jango3.0+django-chanenls3.0.4+supervisord+daphne+nginx+uwsgi)

uwsgi配置

[uwsgi]
# 使用nginx连接时使用,Django程序所在服务器地址
# 选择内网IP和端口
socket=172.20.233.65:8053

# 项目根目录
chdir=/home/xiaoma/project/wudi/chat_wx


#项目中wsgi.py文件的相对目录
wsgi-file=chat_wx/wsgi.py

# 进程数
processes=4

# 线程数
threads=4

# uwsgi服务器的角色
master=True

# 存放进程编号的文件
pidfile=uwsgi.pid

# 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。以前的runserver是依赖终端的
daemonize=logs/uwsgi.log

# 指定虚拟环境所在目录,不能填相对目录
;virtualenv=/root/.virtualenvs/py3env

super

[program:daphne]
socket=tcp://172.20.233.65:8053

# Directory where your site's project files are located
directory=/home/xiaoma/project/wudi/chat_wx

# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=daphne -b 0.0.0.0 -p 8052 --proxy-headers chat_wx.asgi:application

# Number of processes to startup, roughly the number of CPUs you have
numprocs=4

# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d

# Automatically start and recover processes
autostart=true
autorestart=true

# Choose where you want your log to go
stdout_logfile=/home/xiaoma/project/wudi/chat_wx/logs/asgi.log
redirect_stderr=true

[supervisord]
nodaemon=true
[supervisorct]

asgi.py

import os
import django
from channels.auth import AuthMiddlewareStack
from channels.http import AsgiHandler
from channels.routing import ProtocolTypeRouter, URLRouter
# django.setup(False)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chat_wx.settings')
from django.core.asgi import get_asgi_application
django_asgi_app = get_asgi_application()
from apps.chat_index import routing
application = ProtocolTypeRouter({
    "http": AsgiHandler(),
    # "http": django_asgi_app,
    # Just HTTP for now. (We can add other protocols later.)
    "websocket": AuthMiddlewareStack(
        URLRouter(
            routing.websocket_urlpatterns
        )
    ),
})

nginx

upstream chat_wx {
    # supervior 配置中的daphne 启动的IP和端口
    server 172.20.233.65:8052;
}

upstream chats {
    # uwsgi运行的IP地址和端口号
    server 172.20.233.65:8053;
}
server {
    # 监听端口
    listen      80;

    # 服务器域名或者ip地址
    server_name wudiapi.yxzyjs.com;
    # 编码
    charset     utf-8;

    # 文件最大上传大小
    client_max_body_size 75M;

    # 主目录
    location /live {
        uwsgi_pass  live;
        include    /etc/nginx/uwsgi_params;
    }
      # 商学院
    location /school {
        uwsgi_pass  school;
        include    /etc/nginx/uwsgi_params;
    }
    # 聊天
    location /chat {
        uwsgi_pass  chats;
        include    /etc/nginx/uwsgi_params;
}
    location /ws {
        proxy_pass http://chat_wx;
 
        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;
   }
     location / {
        uwsgi_pass  super;
        include    /etc/nginx/uwsgi_params;
    }
}

通过supervisor 管理daphne的进程,通过nginx>supervisor>daphne>uwsgi,http请求/chat路径通过nginx转发到8053,请求路径为/ws的通过nginx转发到8052

posted @ 2021-08-23 14:01  河图s  阅读(128)  评论(0)    收藏  举报