502 Bad Gateway problems

Hello Everybody

Hello Everyone,

I am experiencing a 502 Gateway Error with my Django and Nginx setup. I have searched all over the internet and tried every possible solution, but I still cannot resolve the issue.

Gunicorn is definitely running.
The Gunicorn.socket is active.
The Gunicorn.service is active with no issues.

Below are all the details I’m sharing regarding the problem.

NGINX STATUS
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Thu 2024-10-03 19:57:28 UTC; 19min ago
Docs: man:nginx(8)
Main PID: 691468 (nginx)
Tasks: 2 (limit: 2141)
Memory: 1.7M (peak: 2.0M)
CPU: 14ms
CGroup: /system.slice/nginx.service

gUNICORN sTATUS

Oct 03 17:10:58 ubuntu-1cpu-2gb-us-chi1 systemd[1]: Started gunicorn.service - gunicorn daemon.
Oct 03 17:10:58 ubuntu-1cpu-2gb-us-chi1 gunicorn[630778]: [2024-10-03 17:10:58 +0000] [630778] [INFO] Starting gunicorn 23.0.0
Oct 03 17:10:58 ubuntu-1cpu-2gb-us-chi1 gunicorn[630778]: [2024-10-03 17:10:58 +0000] [630778] [INFO] Listening at: unix:/run/gunicorn.sock (630778)
Oct 03 17:10:58 ubuntu-1cpu-2gb-us-chi1 gunicorn[630778]: [2024-10-03 17:10:58 +0000] [630778] [INFO] Using worker: sync
Oct 03 17:10:58 ubuntu-1cpu-2gb-us-chi1 gunicorn[630779]: [2024-10-03 17:10:58 +0000] [630779] [INFO] Booting worker with pid: 630779
Oct 03 17:10:58 ubuntu-1cpu-2gb-us-chi1 gunicorn[630780]: [2024-10-03 17:10:58 +0000] [630780] [INFO] Booting worker with pid: 630780
Oct 03 17:10:58 ubuntu-1cpu-2gb-us-chi1 gunicorn[630781]: [2024-10-03 17:10:58 +0000] [630781] [INFO] Booting worker with pid: 630781

and the mistake eror log

2024/10/03 19:43:59 [crit] 605006#605006: *144 connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream, client: xx.50.xx.48, server: shiptroy.com.tr, request: “GET / HTTP/1.1”, upstream: “http://unix:/run/gunicorn.sock:/”, host: “xx.50.xx.54:80”
2024/10/03 19:59:29 [crit] 691469#691469: *1 connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream, client: xx.xx.40.46, server: shiptroy.com.tr, request: “GET / HTTP/1.1”, upstream: “http://unix:/run/gunicorn.sock:/”, host: “shiptroy.com.tr”

how is the fixed

Welcome @yunusdogan !

Please show the full commands being used to start gunicorn.

Show your nginx service file.

Show the permissions currently existing on the gunicorn.sock file.

hello @KenWhitesell thank you

Gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

Gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/report_web
ExecStart=/var/www/report_web/env/bin/gunicorn --workers 3 --bind unix:/run/gunicorn.sock report_web.wsgi:application

[Install]
WantedBy=multi-user.target

wsgi
server {
listen 80;
server_name shiptroy.com.tr www.shiptroy.com.tr;

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

location /static/ {
    alias /var/www/report_web/static/;  # Düzgün bir alias ile güncellenmiştir
}

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

}

if i started serv ( python manage.py runserver 0.0.0.0:8000 )

the site is working

What are the current permissions on /run/gunicorn.sock?

Also, you really don’t want your Django project to reside within your nginx-accessible directory structure. Your code should reside somewhere else.

How can I see current permissions?

This is just a test, not the main project. I couldn’t run the server. The main project will be uploaded at a different time. If I manage to get this to work.

ls -l /run/gunicorn.sock

ls -l /run/gunicorn.sock
ls: cannot access ‘/run/gunicorn.sock’: No such file or directory
(env) root@xxxxxx:/var/www/report_web#

Then your gunicorn.socket service isn’t running, or has thrown an error.

● gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; preset: enabled)
Active: active (running) since Thu 2024-10-03 17:10:58 UTC; 3h 40min ago
TriggeredBy: ● gunicorn.socket

All of them working i dont understand :frowning:

it seems your gunicorn socket path is not /run/gunicorn.sock

I’m not asking about your gunicorn.service, I’m asking about your gunicorn.socket.

EVERYBODY THANKSSSSS

IT IS working

HI younusdogan - I am also facing same issue. Could you please let me know how did it get working

Welcome @hubofalok !

The Bad Gateway error has many possible causes. If you are getting this error and would like assistance, please open a new topic with the details of your deployment.

hello if you share code i can help you @KenWhitesell thank you wonderful man