Nginx – 502 Bad Gateway nginx/1.14.0 (Ubuntu)

djangogunicornnginxpythonubuntu-18.04

Im trying to deploy django app using nginx & gunicorn but its showing error

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful




sudo systemctl status gunicorn.service

gunicorn.service - gunicorn service

   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset:

   Active: active (running) since Sat 2020-04-11 21:19:19 IST; 11s ago

 Main PID: 16326 (gunicorn)


    Tasks: 4 (limit: 1689)

sudo systemctl status nginx

● nginx.service - A high performance web server and a reverse proxy server

   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)

   Active: active (running) since Sat 2020-04-11 04:54:18 IST; 18h ago

     Docs: man:nginx(8)

  Process: 1650 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfi

  Process: 1652 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exite

  Process: 1651 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (c

 Main PID: 1653 (nginx)

    Tasks: 9 (limit: 1689)

   CGroup: /system.slice/nginx.service

           ├─1653 nginx: master process /usr/sbin/nginx -g daemon on; master_process o

           ├─1654 nginx: worker process

           ├─1655 nginx: worker process

           ├─1656 nginx: worker process

           ├─1657 nginx: worker process

           ├─1658 nginx: worker process

           ├─1659 nginx: worker process

           ├─1660 nginx: worker process

           └─1661 nginx: worker process



Apr 11 04:54:18 wms systemd[1]: Starting A high performance web server and a reverse p

Apr 11 04:54:18 wms systemd[1]: Started A high performance web server and a reverse pr

Already tried

sudo systemctl stop nginx && sudo systemctl disable nginx && sudo systemctl enable nginx && sudo systemctl start nginx

/etc/nginx/sites-available/iitians

server {
       listen 80;
       server_name 192.95.37.123;
       location = /images/favicon.ico {access_log off;log_not_found off;}

       location /static/ {
         root /home/wms/iitians;
       }
       location /media/ {
         root /home/wms/iitians;
       }

       location / {
         include proxy_params;
         proxy_pass http://unix:/home/wms/iitians/iitians.sock;
       }
     }

/etc/systemd/system/gunicorn.service

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

[Service]
User=wms
Group=www-data
WorkingDirectory=/home/wms/iitians/
ExecStart=/home/wms/iitiansenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/wms/iitians/iitans.sock iitians.wsgi:application

[Install]
WantedBy=multi-user.target

Best Answer

After start gunicorn, I suggest that you should first test the gateway with the command:

sudo -u <nignx-user> curl --unix-socket /home/wms/iitians/iitians.sock http

This

proxy_pass http://unix:/home/wms/iitians/iitians.sock;

should be

proxy_pass unix:/home/wms/iitians/iitians.sock;

Deploying Gunicorn