[英]django-channels nginx settings
我的 django 應用程序使用 django-channels。 我能夠將 django 配置為使用 gunicorn 和 nginx 運行。 如果我使用 python manage.py runserver 和 redis-server 發送通知等,應用程序將運行,但我無法使用 nginx 對其進行配置。
server {
listen 80;
server_name IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/amir/clientcode;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/amir/clientcode/adminpanel.sock;
}
}
但是,當我嘗試為 django-channels 配置它時,它給了我狀態 502
upstream channels-backend {
server localhost:8000;
}
server {
listen 80;
server_name IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/amir/clientcode;
}
location / {
try_files $uri @proxy_to_app;
include proxy_params;
proxy_pass http://unix:/home/amir/clientcode/adminpanel.sock;
}
location @proxy_to_app {
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;
}
}
我的 asgi.py 文件
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "adminpanel.settings")
django.setup()
application = get_default_application()
``
首先,在你的app中安裝Daphne:這里我使用daphne==1.3.0
要啟動 Daphne 服務器,我使用以下命令:
export DJANGO_SETTINGS_MODULE="config.settings"
exec daphne -b 0.0.0.0 --proxy-headers config.asgi:channel_layer
除了Daphne,你還得啟動一個worker:
python manage.py runworker
有了這個,您可以在同一 URL 的項目中使用 sockets。
看看這篇文章: https://medium.com/labcodes/introduction-to-django-channels-d1047e56f218
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.