django生产环境部署(四):asgi服务器daphne处理websocket请求

貌似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,就可以了

参考:
https://www.cnblogs.com/wdliu/p/10032180.html

你可能感兴趣的:(生产环境)