配置文件/etc/nginx/sites-enabled/myapp:

配置文件/etc/nginx/sites-enabled/myapp:

我是服务器端进程的初学者,有关该主题的其他问题运行的堆栈与我不同,因此很难找到可用的答案。

我很可能在服务器块中的 IP 地址上犯了一个愚蠢的错误,但由于我是一名初学者,所以这并不明显。

我在 Digital Ocean 上运行 Django 2.1 应用程序,使用Nginxsupervisor。我的应用程序使用Django channels因此daphne我使用supervisor(asgi而不是uwsgi)。

我还在 Let's Encrypt 注册以获取 SSL 证书。

Supervisor 和 Nginx 正在运行,我看到

502 Bad Gateway当我输入我的域名时。

nginx 日志显示

2019/02/26 15:51:40 [error] 20187#20187: *8 connect() failed (111: Connection refused) while connecting to upstream, client: 86.xxx.xxx.14, server: myapp.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "www.myapp.com"

sudo /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf 节目

2019-02-26 15:52:39,429 CRIT Supervisor running as root (no user in config file)
2019-02-26 15:52:39,430 WARN Included extra file "/etc/supervisor/conf.d/appname_asgi.conf" during parsing
2019-02-26 15:52:39,431 INFO Creating socket tcp://127.0.0.1:8000
2019-02-26 15:52:39,431 INFO Closing socket tcp://127.0.0.1:8000
Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.

这显然表明另一个程序正在使用port 8000

然而sudo netstat -nlp | grep 8000什么也没有返回。

配置文件/etc/nginx/sites-enabled/myapp:

upstream myapp {
    server 127.0.0.1:8000;
}

server {
    server_name myapp.com www.myapp.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        try_files $uri @proxy_to_app;
    }

    location /static/ {
        root /home/me/myapp/src/myapp;
    }

    location /media/  {
        root /home/me/myapp/src/myapp;
        include /etc/nginx/mime.types;
    }

    location @proxy_to_app {
        proxy_pass http://myapp;

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

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}


server {
    if ($host = www.myapp.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = myapp.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name myapp.com www.myapp.com;
    return 404; # managed by Certbot

/etc/supervisor/conf.d/myapp_asgi.conf

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

# Directory where your site's project files are located
directory=/home/me/myapp/src/myapp

# 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=daphne -u /home/me/daphne/run/daphne%(process_num)d.sock --f$

# 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=/home/me/daphne/logs/asgi.log
redirect_stderr=true

sudo netstat -花生

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode       PID/Program name
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      112        2954261     11650/redis-server 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          3495386     19401/nginx -g daem
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          15750       1564/sshd       
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      0          3499269     19401/nginx -g daem
tcp        0    356 xxx.xx.xxx.76:22        xx.xx.xxx.14:52521     ESTABLISHED 0          3417368     13924/1         
tcp        0      0 xxx.xx.xxx.76:22        xx.xx.xxx.14:52528     ESTABLISHED 0          3418136     14131/2         
tcp6       0      0 :::6379                 :::*                    LISTEN      1000       2954691     11712/redis-server 
tcp6       0      0 :::80                   :::*                    LISTEN      0          3495387     19401/nginx -g daem
tcp6       0      0 :::22                   :::*                    LISTEN      0          15761       1564/sshd     

如果有人能看一下这个问题我将非常感激,就像我说的,我相信对于这个领域具有更高技能水平的人来说这个问题是显而易见的。

非常感谢。

答案1

通过更改为解决了这个问题

command=/home/Env/myapp/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers mysite.asgi:application

所以它能够执行daphne命令

相关内容