本文介绍了通过Apache服务器的Django频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django项目,该项目在基于Apache的VPS上运行良好.在将Django通道添加到我的项目后,它可以在localhost上正常运行,但是在我的vps中,我的浏览器记录了一个错误"WebSocket连接到'...'失败:WebSocket握手期间出错:意外的响应代码:404",该项目失败了./p>

根据我的谷歌搜索,我认为Apache无法支持网络套接字.但找不到明确的答案,也无法在Apache上运行频道

这是我在000-default.conf中的apache配置:

 < VirtualHost *:80>别名/static/opt/kalameh/static<目录/opt/kalameh/static>要求所有授予</目录><目录/opt/kalameh/server><文件wsgi.py>要求所有授予</文件></目录>WSGIDaemonProcess kalameh python-path = opt/kalameh python-home = opt/kalameh/kalamehenvWSGIProcessGroup卡拉麦WSGIScriptAlias/opt/kalameh/server/wsgi.pyServerAdmin网站管理员@localhostDocumentRoot/var/www/html错误日志$ {APACHE_LOG_DIR}/error.logCustomLog $ {APACHE_LOG_DIR}/access.log合并</VirtualHost> 

这是我的wsgi.py

  import os导入系统sys.path.append('/opt/kalameh/')从django.core.wsgi导入get_wsgi_applicationos.environ.setdefault("DJANGO_SETTINGS_MODULE",服务器设置")应用程序= get_wsgi_application() 
解决方案

您需要检查 channels'部署部分.请注意,频道 v1.x 和 v.2x 文档.拿起合适的一个.

您注意到,Apache不支持websocket.因此,您将需要运行公开websocket的服务器-本机ASGI接口服务器(渠道文档使用的术语).您可以使用 Django的达芙妮.达芙妮还支持HTTP接口.

因此,您有两种解决方案:

  1. 让Apache传递Django的HTTP请求+达芙妮传递Websocket(如果您使用此解决方案,则用于查找更多文档的关键字是 reverse proxy ProxyPass )
  2. 使用Daphne交付Websocket和HTTP(无需Apache).

I have a Django project that's working well on my VPS over apache.after adding Django channels to my project it's working on localhost perfectly but in my vps, my browser logged an error "WebSocket connection to '...' failed: Error during WebSocket handshake: Unexpected response code: 404" and the project failed.

according to my googling, I think apache can't support web-socket. but can't find a clear answer and trip for running channels on apache

this is my apache config in 000-default.conf:

<VirtualHost *:80>

Alias /static /opt/kalameh/static
<Directory /opt/kalameh/static>
    Require all granted
</Directory>

<Directory /opt/kalameh/server>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>


WSGIDaemonProcess kalameh python-path=opt/kalameh python-home=opt/kalameh/kalamehenv
WSGIProcessGroup kalameh
WSGIScriptAlias / opt/kalameh/server/wsgi.py

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

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

and this is my wsgi.py

import os
import sys

sys.path.append('/opt/kalameh/')

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")


application = get_wsgi_application()
解决方案

You need to check channels' Deploying section. Be aware the slight differences between channels v1.x and v.2x documentation. Pick up the appropriate one.

As you notice, Apache does not support websocket. So you would need to run a server that exposes websocket - a native ASGI interface server (terminology used by channel documentation). You can use Django's Daphne. Daphne also supports HTTP interface.

So you have two solutions:

  1. Keep Apache delivering your Django's HTTP request + Daphne delivering your websocket (if you go for this solution, the keywords to use to find more documentation are reverse proxy and ProxyPass)
  2. Using Daphne for delivering your Websocket and HTTP (no need of Apache).

这篇关于通过Apache服务器的Django频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:51
查看更多