微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django频道制作Nginx

我有一个django项目,最近添加了使用websockets的渠道.这似乎一切正常,但我遇到的问题是准备好生产.

我的设置如下:

Nginx web server
Gunicorn for django
SSL enabled

因为我已经为混音添加了频道.我花了最后一天试图让它发挥作用.

在所有turtotials他们说你在某个端口上运行daphne然后显示如何为此设置Nginx.

但是如果有枪支服务django呢?

所以现在我有guncorn在8001上运行这个django应用程序

如果我在另一个端口上运行daphne,那么就说8002 – 它应该如何知道这个django项目的标准?跑步工人怎么样?

Gunicorn,daphne和跑步者都应该一起跑吗?

解决方法:

这个问题实际上是在最新的Django Channels docs解决的:

It is good practice to use a common path prefix like /ws/ to
distinguish WebSocket connections from ordinary HTTP connections
because it will make deploying Channels to a production environment in
certain configurations easier.

In particular for large sites it will be possible to configure a
production-grade HTTP server like Nginx to route requests based on
path to either (1) a production-grade Wsgi server like Gunicorn+Django
for ordinary HTTP requests or (2) a production-grade Asgi server like
daphne+Channels for WebSocket requests.

Note that for smaller sites you can use a simpler deployment strategy
where daphne serves all requests – HTTP and WebSocket – rather than
having a separate Wsgi server. In this deployment configuration no
common path prefix like is /ws/ is necessary.

在实践中,您的Nginx配置看起来像(缩写为仅包含相关位):

upstream daphne_server {
  server unix:/var/www/html/env/run/daphne.sock fail_timeout=0;
}

upstream gunicorn_server {
  server unix:/var/www/html/env/run/gunicorn.sock fail_timeout=0;
}

server { 
  listen   80; 
  server_name _;

  location /ws/ {
    proxy_pass http://daphne_server;
  }

  location / {
    proxy_pass http://gunicorn_server;
  }
}

(上面假设您将Gunicorn和daphne服务器绑定到Unix套接文件.)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐