Django 2.2的Gunicorn / Nginx配置提供了502错误的网关

时间:2020-07-19 19:38:36

标签: django nginx gunicorn

当我访问该站点时,我收到了错误的网关502错误。这就是我的配置

/etc/nginx/sites-enabled/django

upstream app_server {
    server unix:/home/django/gunicorn.socket fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/my_project/current/my_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/my_project/current/my_project/static;
    }

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/local/lib/python3.6/dist-packages/django/contrib/admin/static/admin/;
    }

    location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_buffering off;

            proxy_pass http://app_server;
    }

}

/etc/systemd/system/gunicorn.service

[Unit]
Description=Gunicorn daemon for Django
Before=nginx.service
After=network.target

[Service]
WorkingDirectory=/home/django/my_project/current
ExecStart=/usr/bin/gunicorn3 --name=my_project --pythonpath=/home/django/my_project/current --bind unix:/home/my_project/gunicorn.socket --config /etc/gunicorn.d/gunicorn.py my_project.wsgi:application --log-file /var/log/gunicorn.log --log-level debug --workers=2 
Restart=always
SyslogIdentifier=gunicorn
User=django
Group=django


[Install]
WantedBy=multi-user.target

运行service gunicorn status时,我可以看到此信息:

● gunicorn.service - Gunicorn daemon for Django
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2020-07-19 19:28:37 UTC; 7min ago
  Process: 9373 ExecStart=/usr/bin/gunicorn3 --name=my_project --pythonpath=/home/django/my_project/current --bind unix:/home/django/gunicorn.socket --config /etc/gunicorn.d/gunicorn.py my_project.wsgi:application --log-file /var/log/gunicorn.l
 Main PID: 9373 (code=exited, status=1/FAILURE)

Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: gunicorn.service: Service hold-off time over, scheduling restart.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: gunicorn.service: Scheduled restart job, restart counter is at 5.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: Stopped Gunicorn daemon for Django.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: gunicorn.service: Start request repeated too quickly.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: Failed to start Gunicorn daemon for Django.

我可以在nginx错误日志中看到:

2020/07/19 19:26:00 [crit] 9039#9039: *15 connect() to unix:/home/django/gunicorn.socket failed (2: No such file or directory) while connecting to upstream, client: 84.48.191.11, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "161.35.197.193"

有什么建议吗?我调查了类似的问题,但无法解决我的问题。

1 个答案:

答案 0 :(得分:0)

似乎我有一个错误502,因为它无法打开数据库。我已经在Gunicorn日志中找到了有关它的更多信息。