当我们使用channel来进行websocket时在本地环境runserver启动后一切正常,
但是在nginx中使用uwsgi部署使用后无法访问报错404找不到,原因是channel使用得是asgi服务,当使用uwsgi时配置找不到等原因反正无法访问,网上得解决办法什么改nginx配置啥的基本无效。最直接得解决办法就是使用daphne来启动django服务,使用daphne启动必须要修改asgi.py这个文件不然还是不能访问的。下来进行整天修改步骤
1.修改项目主目录下面得asgi.py文件中得配置,将之前配置得进行修改。
# 修改前的配置
"""
ASGI config for scrapy_admin 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
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'scrapy_admin.settings')
application = get_asgi_application()
# 修改后的配置
"""
ASGI config for scrapy_admin 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
from channels.routing import get_default_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'scrapy_admin.settings')
django.setup()
application = get_default_application()
2.我们是使用docket部署的服务,所以将之前的容器中要安装daphne库,然后再更改docket-compose.yml中的启动命令,将之前的uwsgi -i uwsgi.ini 改为daphne -b 0.0.0.0 -p 8000 scrapy_admin.asgi:application
3.修改nginx中的配置,由于uwsgi不能使用,所以之前的老配置进行修改
# 旧配置
server {
listen 9090;
server_name localhost;
location / {
uwsgi_pass django;
include uwsgi_params;
}
}
# 修改后配置
server {
listen 9090;
server_name localhost;
location / {
# uwsgi_pass django;
# include uwsgi_params;
proxy_pass http://django;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
评论抢沙发