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套接字文件.)

上一篇:Django grappelli似乎无法看到它的所有媒体文件


下一篇:django – 如果你只发送JSON对象,那么适当的NGINX配置是什么?