使用NGINX和Supervisor设置后如何连接Websocket?

问题描述 投票:0回答:1

我在wsgi应用程序旁边创建了一个Django频道。如果我运行,一切正常:

daphne -b 0.0.0.0 -p 8001 myapp.asgi:application 

我可以连接到websocekt,但是用nginx创建了主管后,我无法连接,并且看不到任何可以解决问题的有用日志。

当我检查主管状态时,没有错误:

asgi:asgi0                       RUNNING   pid 23981, uptime 0:10:22
asgi:asgi1                       RUNNING   pid 23980, uptime 0:10:22
asgi:asgi2                       RUNNING   pid 23983, uptime 0:10:22
asgi:asgi3                       RUNNING   pid 23982, uptime 0:10:22

套接字最初是localhost:8001,我也允许UFW使用该端口,但是它不起作用,因此我更改为服务器IP。

主管

[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://139.162.172.87:8001

# Directory where your site's project files are located
directory=/home/markkiraly/crumb_backend

# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=/home/markkiraly/crumb_backend/venv/bin/daphne --fd 0 --access-log - --proxy-headers crumb_backend.asgi:application
# Number of processes to startup, roughly the number of CPUs you have
numprocs=4

# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d

# Automatically start and recover processes
autostart=true
autorestart=true

# Choose where you want your log to go
stdout_logfile=/var/log/asgi.log
redirect_stderr=true

NGINX CONFIG

upstream channels-backend {
    server 139.162.172.87:8001;
}

server {
    listen 80;
    server_name crumbliterature.com www.crumbliterature.com;
    location /static/ {
        root /home/markkiraly/crumb_backend/;
    }

    location /media/ {
        root /home/markkiraly/crumb_backend/;
    }

     location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

     location /ws/ {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_pass http://channels-backend;

        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;
    }

}

我试图以各种可能的方式进行连接:

ws://139.162.172.87/ws/chat/1/
ws://139.162.172.87:8001/ws/chat/1/
ws://crumbliterature.com/ws/chat/1/
ws://crumbliterature.com:8001/ws/chat/1/
django sockets websocket django-channels channels
1个回答
0
投票

1)

如果NGINX和Django Channel位于一台计算机上,则可以替换

upstream channels-backend {
    server 139.162.172.87:8001;
}

with

upstream channels-backend {
    server localhost:8001;
}

2)

我不知道Django Channels,但是如果默认或选择被另一个应用程序阻止,则某些应用程序可以尝试不同的端口。可能是您的应用程序正常运行,但端口被其他端口阻塞或卡住了,但没有通知您。尝试检查哪个应用程序在8001上侦听(如果有人侦听)

© www.soinside.com 2019 - 2024. All rights reserved.