[Django]-Gunicorn with unix socket not working gives 502 bad gateway

4๐Ÿ‘

Put your socket file in /var/run instead of /tmp

And you are welcome.

This answer cost me two hour, fmlโ€ฆ

I find it in https://serverfault.com/questions/463993/nginx-unix-domain-socket-error/464025#464025

2๐Ÿ‘

I got the same problem, and i was doing the same tutorial too, so here is my solution following this:
http://docs.gunicorn.org/en/stable/deploy.html

Note: i am not using Upstart instead, i am using SystemD service

1) Make a service in /etc/systemd/system/nginx.service

[Unit]
Description=Gunicorn server for {SITENAME}
After=network.target

[Service]
User={user}
Group=www-data
WorkingDirectory=/home/{user}/sites/{SITENAME}/source
ExecStart=/home/{user}/sites/{SITENAME}/virtualenv/bin/gunicorn --workers 3 --bind unix:/tmp/{SITENAME}.socket superlists.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

2) i followed the next steps from http://docs.gunicorn.org/en/stable/deploy.html making a gunicorn.socket and a gunicorn.conf, but i just notice that my socket status is inactive, so i think its not necessary.

the gunicorn.conf file in /usr/lib/tmpfiles.d/gunicorn.conf

d /run/gunicorn 0755 someuser someuser -

Next enable the services so they autostart at boot:

$ systemctl enable nginx.service
$ systemctl enable gunicorn.socket

Either reboot, or start the services manually:

$ systemctl start nginx.service
$ systemctl start gunicorn.socket

Some hints that help:

  • make sure that your service is Up and Running

    $ systemctl status gunicorn.service

  • Check if Nginx configuration is ok

    $ sudo nginx -t

  • Check for a letter misplaced
  • Make sure to put your Domain in your ALLOWED_HOSTS as String, it took like 1 hour to realize that i miss โ€˜ โ€˜

ALLOWED_HOSTS = [**'**{SITENAME}**'**]

it took me like 6 hours to get it running but for my first time doing it and zero knowledge in Unix i think its OK.

Hope it helps, keep trying until it works !!

๐Ÿ‘คTiago Almeida

1๐Ÿ‘

Try to add your user to nginx group like:

sudo usermod -a -G user nginx
๐Ÿ‘คZeddin Arief

Leave a comment