貌似uwsgi2.0之後加入了websocket的支持,但是由於並不成熟,我們選擇成熟的官方推薦的asgi服務器daphne,來處理websocket請求,項目中沒有websocket的在上一篇已經結束了。
部署daphne
# 項目/settings和wsgi.py的同目錄下創建asgi.py
"""
ASGI entrypoint. Configures Django and then runs the application
defined in the ASGI_APPLICATION setting.
"""
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
django.setup()
application = get_default_application()
之後啓動daphne服務測試(在項目根目錄下)
daphne -p 8991 myproject.asgi:application
or
daphne -b 127.0.0.1 -p 8991 myproject.asgi:application
ctrl+C
退出
nginx代理websocket
修改nginx配置文件:*.conf
這裏提供2個參考的配置:
# 這是別人的配置和我的有些不一樣
upstream wsbackend {
server 127.0.0.1:8001;
}
location /ws/deploy {
proxy_pass http://wsbackend;
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 /ws {
proxy_pass http://127.0.0.1:8991;
# proxy_connect_timeout 2s
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_IP;
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;
# proxy_read_timeout 60s;#默認爲60s
# proxy_send_timeout 60s;#默認爲60s
}
誰能告訴我使用upstream wsbackend
的和我直接配置地址端口的有什麼不同嗎?
之後啓動nginx,uwsgi,daphne,就可以了