2
votes

I'm following the http://www.obeythetestinggoat.com/book/chapter_08.html book, and it says to add a unix socket to run nginx server with gunicorn, which i did. This is my nginx file

server {
listen 80;
server_name mydjsuperlist-staging.tk;

location /static {
alias /home/elspeth/sites/mydjsuperlist-staging.tk/static;
}

location / {
proxy_set_header Host $host;
    proxy_pass http://unix:/tmp/mydjsuperlist-staging.tk.socket;
}
} 

Nginx reloads without any failure and checked it with nginx -t

When i run:

gunicorn --bind unix:/tmp/mydjsuperlist-staging.tk.socket superlists.wsgi:application

It succesfully creates mydjsuperlist-staging.tk.socket file in tmp folder and i get this on my terminal

2016-09-01 18:56:01 [15449] [INFO] Starting gunicorn 18.0
2016-09-01 18:56:01 [15449] [INFO] Listening at: unix:/tmp/mydjsuperlist-staging.tk.socket (15449)
2016-09-01 18:56:01 [15449] [INFO] Using worker: sync
2016-09-01 18:56:01 [15452] [INFO] Booting worker with pid: 15452

Everything seems fine, but when i go to my site mydjsuperlist-staging.tk it gives a (502) bad gateway error. When i was using a port my site was running perfectly. What am i doing wrong over here ?

3
nginx returns error 502 or gunicorn?dd42
I think its nginx, because in the browser i get the error message 502 gateway error and beneath it nginx/ubuntu 14.04, it's still confusing, i don't see any kind of connection response on my terminal. How will i know if gunicorn is causing the problemanas munir
What is the message inside the nginx error log?Windsooon
Are you running a Unix system with SELinux enabled? I've had this happen to me recently. If it is, let me know and I'll post a solutionKeenan Lawrence
No, I'm running Ubuntu locally and at the server haven't installed any selinux packageanas munir

3 Answers

3
votes

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
votes

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 !!

0
votes

In my case I rebooted my server and it worked again.

No explanation.