带有nginx的Django频道没有客户端收到的消息

前端之家收集整理的这篇文章主要介绍了带有nginx的Django频道没有客户端收到的消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我们有一个使用渠道包的应用程序,在localhost上工作得很好.一旦我们点击staging并在Django(使用SSL)前放置一个Nginx框,我们就可以连接到套接字,但客户端不会收到任何消息.

Nginx conf:

  1. worker_processes auto;
  2. error_log /dev/stdout info;
  3. user nobody nogroup;
  4. pid /tmp/Nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. accept_mutex off;
  8. }
  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;
  12. access_log /dev/stdout;
  13. sendfile on;
  14. keepalive_timeout 65;
  15. gzip on;
  16. gzip_disable "MSIE [1-6].(?!.*SV1)";
  17. gzip_vary on;
  18. upstream ws_server {
  19. server unix:/tmp/daphne.sock fail_timeout=0;
  20. }
  21. server {
  22. # redirect all http requests to https
  23. listen 80;
  24. listen [::]:80 ipv6only=on;
  25. return 301 https://$host$request_uri;
  26. }
  27. server {
  28. listen 443 ssl;
  29. client_max_body_size 4G;
  30. server_name changemyip.com;
  31. keepalive_timeout 5;
  32. add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
  33. ssl_session_timeout 1d;
  34. ssl_session_cache shared:SSL:50m;
  35. ssl_session_tickets on;
  36. ssl_dhparam /etc/Nginx/ssl/dhparam.pem;
  37. location /ws/ {
  38. try_files $uri @proxy_to_ws;
  39. }
  40. location @proxy_to_ws {
  41. proxy_pass http://ws_server;
  42. proxy_redirect off;
  43. proxy_set_header Host $host;
  44. proxy_set_header X-Real-IP $remote_addr;
  45. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  46. proxy_set_header X-Forwarded-Proto $scheme;
  47. # Websocket specific
  48. proxy_http_version 1.1;
  49. proxy_set_header Upgrade $http_upgrade;
  50. proxy_set_header Connection "upgrade";
  51. proxy_set_header Host $http_host;
  52. proxy_connect_timeout 86400;
  53. proxy_read_timeout 86400;
  54. proxy_send_timeout 86400;
  55. }
  56. ...
  57. ssl_protocols TLSv1.1 TLSv1.2;
  58. ...
  59. ssl_prefer_server_ciphers on;
  60. ssl_stapling on;
  61. ssl_stapling_verify on;
  62. }
  63. }

Django用gunicorn和websockets运行我加入了一个daphne服务器.我可以在daphne日志中看到我的客户端正在连接,但仍然没有收到从daphne到客户端的消息.

Daphne正在创建一个unix套接字,Nginx接受通信:
daphne main.asgi:channel_layer -u /tmp/daphne.sock

最佳答案
我有同样的问题.我无法通过unix-socket连接,但我找到了一种使用系统端口实现请求管理的简单方法.我使用了以下教程,(并使用了我对Gunicorn的经验),并设法修改了一下他们的Nginx配置文件,我建议你查看教程:

> Django Channels Group Pt1
> Django Channels Group Pt2

我的Nginx文件

  1. # Enable upgrading of connection (and websocket proxying) depending on the
  2. # presence of the upgrade field in the client request header
  3. map $http_upgrade $connection_upgrade {
  4. default upgrade;
  5. '' close;
  6. }
  7. # Create an upstream alias to where we've set daphne to bind to
  8. upstream django_app_server {
  9. server 127.0.0.1:8000;
  10. }
  11. server {
  12. listen 80;
  13. server_name YOURDOMAIN.COM;
  14. client_max_body_size 4G;
  15. access_log /webapps/General/logs/Nginx-access.log;
  16. error_log /webapps/General/logs/Nginx-error.log;
  17. location /static/ {
  18. alias /webapps/General/DjangoProject/static/;
  19. }
  20. location /media/ {
  21. alias /webapps/General/DjangoProject/media/;
  22. }
  23. location / {
  24. if (!-f $request_filename) {
  25. proxy_pass http://django_app_server;
  26. break;
  27. }
  28. # Require http version 1.1 to allow for upgrade requests
  29. proxy_http_version 1.1;
  30. # We want proxy_buffering off for proxying to websockets.
  31. proxy_buffering off;
  32. # http://en.wikipedia.org/wiki/X-Forwarded-For
  33. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  34. # enable this if you use HTTPS:
  35. # proxy_set_header X-Forwarded-Proto https;
  36. # pass the Host: header from the client for the sake of redirects
  37. proxy_set_header Host $http_host;
  38. # We've set the Host header,so we don't need Nginx to muddle
  39. # about with redirects
  40. proxy_redirect off;
  41. # Depending on the request value,set the Upgrade and
  42. # connection headers
  43. proxy_set_header Upgrade $http_upgrade;
  44. proxy_set_header Connection $connection_upgrade;
  45. }
  46. # Error pages
  47. error_page 500 502 503 504 /500.html;
  48. location = /500.html {
  49. root /webapps/General/DjangoProject/templates/;
  50. }
  51. }

我的项目中的websockets运行得非常好(组和通道),所有请求都由Daphne提供,但如果您真的需要使用套接字,这种配置可能实际上对您有所帮助.

要考虑的要点

>请记住,这个Nginx文件允许Daphne通常连接,但在生产服务器中,您需要分别运行“Daphne Instance Server”和“Daphne Workers”,以便能够通过您的频道传输消息.
>检查在为频道和组提供服务时是否使用Redis-Server或其他队列管理器.我这样说是因为我注意到在使用“InMemory”配置时,丢失了多条消息.
>还要检查您的生产环境是否将Redis-Server作为守护程序运行.我注意到在几个系统中,Redis-Server甚至没有工作,但是当连接被拒绝时,Django应用程序没有引发异常.
>你需要一些东西来保持达芙妮和它的工人,因为即使他们循环,他们也不是“例外抗性”所以当异常被提出时他们会死.显然,我建议使用Supervisor或使用Linux系统进行服务.
>我不知道当DEBUG == False时,daphne的工作人员是否可以提供静态和媒体文件,但显然使用Nginx配置单独提供它们会更好.
>与使用套接字相比,我仍然不知道使用端口的安全/性能影响,所以这是值得检查的事情(如下所示,我发现Daphne或我的配置可能存在错误).

我知道现在这对你来说可能无关紧要了(我的意思是差不多1个月),但也许有人会对这个答案有所帮助.

未知和非常奇怪的安全问题

TL; DR:不要使用此配置在同一服务器中描绘两个Django-Daphne应用程序,否则您将遇到不好的时间.

通过使用这种配置,我可以在没有任何问题的情况下将Phoenix应用程序与Django应用程序一起部署,但是在使用这种类型的配置部署2个或更多Django应用程序时遇到了问题.出于某种原因,达芙妮知道它必须不断阅读哪些端口来接收请求,但它只是读取所有这些端口并将它们提供给任何它喜欢的人.例如,如果我在同一台服务器上运行DJANGO_APP_1和DJANGO_APP_2(具有不同的Nginx配置和明显不同的系统端口),有时DJANGO_APP_2的Daphne Workers将窃取针对DJANGO_APP_1的请求,反之亦然.我无法确定问题的根源,但我认为这与达芙妮的工作人员在一定程度上与他们所涉及的项目无关. (只是一个理论,我没有时间检查他们的代码).

猜你在找的Nginx相关文章