nginx部署Channels实现wss协议请求

1 篇文章 0 订阅
1 篇文章 0 订阅

在已经用nginx部署完了uwsgi的https请求的前提下

我们将https的请求发送给uwsgi进行处理,将websocket请求发送给daphne进行处理,daphne和uwsgi可以使用supervisord进行控制,aupool项目中只用supervisord来控制daphne,uwsgi服务需要单独起。

1.安装supervisor:

$ sudo apt install supervisor

2.安装daphne,在安装channels的时候,会自动安装daphne,所以不需要再次安装:

3.配置supervisor,找到/etc/supervisor/supervisor.conf文件:

[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
# 与nginx端口保持一致
socket=tcp://localhost:9009

# Directory where your site's project files are located
# 项目路径
directory=/var/www/test/

# 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
# 启动daphne的命令
command=/root/anaconda3/envs/APVENV/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers aupool.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=/var/www/asgi.log
redirect_stderr=true

directory为项目所在地址

command为daphne启动命令

如果没有/run/daphne文件需要手动创建

修改完后使用 supervisorctl reread 和 supervisorctl update让新配置生效,如果执行不了先执行supervisord -c /etc/supervisor/supervisor.conf命令

4.配置nginx:

upstream channels-backend {
    server localhost:9009;
    }

server {
        listen       9000 ssl;
        server_name  域名;
        ssl_certificate      test.cn.pem;
        ssl_certificate_key 2_test.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location /#/web/ {
            root   /var/www/html;
            index  index.html index.htm;
        }
        location /ws/ {
            proxy_pass http://channels-backend;

            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 / {
                include /usr/local/nginx/conf/uwsgi_params;
                uwsgi_pass 127.0.0.1:9001;
        }
    }

channel-backend端口必须要和supervisor.conf中socket端口一致

location /ws/ 是websocket添加的部分

5.redis都要指定配置文件(/etc/redis/redis.conf),不然配置不生效,会提示没有密码:

6.调用daphne提示Apps aren't loaded yet错误:

解决办法:

在asgi.py中添加os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'aupool.settings') django.setup(),如果还不行就调一下代码的位置

7.启动celery:

$ celery -A aupool worker -l info

8.启动supervisord:

supervisord -c /etc/supervisor/supervisor.conf 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只要思想不滑坡办法总比困难多--小鱼干

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值