使用daphne上传django项目的Apache websocket连接

问题描述 投票:0回答:1

asg.py



import os

from django.urls import re_path

from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
import django
from django.urls import path 

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mywebsite.settings')
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

django.setup()

from video.consumers import VideoConsumers
from chat.consumers import Myconsumers


application = get_asgi_application()

application = ProtocolTypeRouter({
    "http":application,
    "websocket": AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
                [
                path("ws/video_call/",VideoConsumers.as_asgi()),
                path('ws/ac/<str:uuid>/',Myconsumers.as_asgi())
                    
                ]
                
            )
        )
    ),

})

daphne.service 文件

  GNU nano 7.2                                                                 daphne.service                                                                           [Unit]
Description=Daphne ASGI Server
After=network.target

[Service]
Type=simple
User=root
Group=www-data
WorkingDirectory=/var/www/mywebsite

ExecStart=/var/www/mywebsite/venv/bin/daphne -b 0.0.0.0 -p 8001 mywebsite.asgi:application

Restart=on-failure


[Install]
WantedBy=multi-user.target

dahpne.service 工作正常

<VirtualHost *:80>
    ServerName www.kartikprajapati.com
    ServerAdmin [email protected]
    #Document Root is not required

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /static /var/www/mywebsite/static
    <Directory /var/www/mywebsite/static>
        Require all granted
    </Directory>

    Alias /media /var/www/mywebsite/media
    <Directory /var/www/mywebsite/media>
        Require all granted
    </Directory>

    <Directory /var/www/mywebsite/mywebsite>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    

    ProxyPass "/ws/" "wss://kartikprajapati.com:8001/"
    ProxyPassReverse "/ws/" "wss://kartikprajapati.com:8001/"
   ProxyPassReverse "/ws/" "wss://kartikprajapati.com:8001/"

    WSGIDaemonProcess mywebsite python-home=/var/www/mywebsite/venv python-path=/var/www/mywebsite
    WSGIProcessGroup mywebsite
    WSGIScriptAlias /  /var/www/mywebsite/mywebsite/wsgi.py
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.kartikprajapati.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

我的 daphne 正在 8001 端口上工作,所有 IP 都为 0.0.0.0 IP

root@srv520922:~# systemctl status daphne
● daphne.service - Daphne ASGI Server
     Loaded: loaded (/etc/systemd/system/daphne.service; enabled; preset: enabled)
     Active: active (running) since Wed 2024-05-08 15:42:10 IST; 40min ago
   Main PID: 30025 (daphne)
      Tasks: 1 (limit: 4658)
     Memory: 49.4M (peak: 49.6M)
        CPU: 1.197s
     CGroup: /system.slice/daphne.service
             └─30025 /var/www/mywebsite/venv/bin/python /var/www/mywebsite/venv/bin/daphne -b 0.0.0.0 -p 8001 mywebsite>

May 08 15:42:10 srv520922 systemd[1]: Started daphne.service - Daphne ASGI Server.
May 08 15:42:11 srv520922 daphne[30025]: 2024-05-08 15:42:11,157 INFO     Starting server at tcp:port=8001:interface=0.>
May 08 15:42:11 srv520922 daphne[30025]: 2024-05-08 15:42:11,158 INFO     HTTP/2 support not enabled (install the http2>
May 08 15:42:11 srv520922 daphne[30025]: 2024-05-08 15:42:11,158 INFO     Configuring endpoint tcp:port=8001:interface=>
May 08 15:42:11 srv520922 daphne[30025]: 2024-05-08 15:42:11,159 INFO     Listening on TCP address 0.0.0.0:8001
lines 1-15/15 (END)

每当我致电 wss://kartikprajapati.com/ws/video_call/ 时,它都会显示错误,请帮忙

我已经做到了

a2enmod proxy rewrite proxy_http proxy_wstunnel

错误:-

[2024 年 5 月 8 日星期三 16:01:15.693322] [wsgi:error] [pid 30048:tid 140661514831552] [远程 49.36.71.188:54708] 未找到:/ws/video_call/ websocket 未连接

如果您想了解有关我的代码和配置的更多信息,请评论

我已经修复了 2 到 3 天,如果您知道任何事情请帮忙

我已经完成了很多配置,但它没有帮助我想知道如何一步步处理它,因为 https 正在工作,但 wss 不是 wokring

deployment hosting vps django-channels daphne
1个回答
0
投票

如果您尝试在本地运行此程序,则需要删除

wss
的代理并使用
ws

为了在互联网上运行应用程序,我们需要将

wss
https
一起使用,而您的应用程序则使用
http
。另外,您似乎正在使用
asgi.py
和 asgi 应用程序,但您的 apache 代码定义了 wsgi 应用程序,这是不必要的。将 Daphne 与 Django Channels 结合使用,我们使用的是异步版本 asgi。

这是一个在 Ubuntu Linux 服务器上与 Daphne 和 Django Channels 一起使用用于 Websockets 的 Apache 服务器配置。

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://127.0.0.1:8001%{REQUEST_URI} [P,QSA,L]
ProxyPass /wss/ wss://127.0.0.1:8001/
ProxyPassReverse /wss/ wss://127.0.0.1:8001/
...
SSLEngine on
SSLCertificateFile /etc/ssl/certificate.crt
SSLCertificateKeyFile /etc/ssl/private/private.key
SSLCertificateChainFile /etc/ssl/ca_bundle.crt

建议运行此命令在生产级服务器上运行 Daphne,我包含了一个在本地服务器上运行 Daphne 的命令。在生产级别,您需要 Kubernetes 或 Docker 容器来保持 Daphne 运行。

daphne -b 0.0.0.0 -p 8001 django_project.asgi:application // Local Development Level
daphne -e ssl:443:privateKey=key.pem:certKey=crt.pem django_project.asgi:application // Production Level
© www.soinside.com 2019 - 2024. All rights reserved.