首页 > 解决方案 > 在 ubuntu 服务器上部署 Django ASGI 应用程序时遇到问题。NGINX 已被用作网络服务器

问题描述

在部署我的 Django ASGI 应用程序时,我遇到了很多麻烦。我已经尝试了所有的解决方案。

在我的 ngix/error.log 文件中,错误是:

2020/11/05 21:37:48 [error] 6716#6716: *31 connect() failed (111: Connection refused) while connecting to upstream, client: 103.144.89.98, server: 156.67.219.10, request: "GET /ws/chat/bbd7182cd0ee95488f1a1e6f3fe0d8f94ed0d14e4db1dce713fe82a3231c523d/ HTTP/1.1", upstream: "http://[::1]:9001/ws/chat/bbd7182cd0ee95488f1a1e6f3fe0d8f94ed0d14e4db1dce713fe82a3231c523d/", host: "156.67.219.10"

在网络浏览器控制台中,我收到以下错误:

WebSocket connection to 'ws://156.67.219.10/ws/chat/bbd7182cd0ee95488f1a1e6f3fe0d8f94ed0d14e4db1dce713fe82a3231c523d/' failed: Error during WebSocket handshake: Unexpected response code: 400

这是我的 settings.py 文件:

WSGI_APPLICATION = 'thinkgroupy.wsgi.application'
ASGI_APPLICATION = "thinkgroupy.routing.application"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("0.0.0.0", 6379)],
        },
    },
}

这是nginx配置文件/etc/nginx/sites-available/thinkgroupy

#added this block
upstream channels-backend {
 server localhost:9001;
}

server {
    listen 80;
    server_name 156.67.219.10;
    client_max_body_size 4G;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        alias /home/admin/thinkgroupy/staticfiles/;
    }
    location /media/ {
        root /home/admin/thinkgoupy;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
    #path to proxy my WebSocket requests
    location /ws/ {

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

这是主管文件/etc/supervisor/conf.d/thinkgroupy.conf

[program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:9001

# Directory where your site's project files are located
directory=/home/admin/thinkgroupy

# 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/admin/thinkgroupy/venv/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers thinkgropy.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

我遵循了部署 ASGI 应用程序的 django-channels 官方文档。我仍然遇到同样的问题。

希望任何专家能帮助我找出解决方案。

标签: djangonginxwebsocketdjango-channelsdjango-supervisor

解决方案


推荐阅读