Bob
Bob

Reputation: 8714

Gunicorn/Nginx configuration for Django 2.2 gives 502 Bad gateway

I am getting bad gateway 502 error when I access the site. This is how my configuration looks like

/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

When I run service gunicorn status, I can see this:

● 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.

And I can see in the nginx error log:

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"

Any advice? I looked into similar questions, but I was not able to resolve my problem.

Upvotes: 0

Views: 166

Answers (1)

Bob
Bob

Reputation: 8714

It seems like I had an error 502 because it couldn't open database. I have found more information about it in Gunicorn log.

Upvotes: 0

Related Questions