'Gunicorn with unix socket not working gives 502 bad gateway
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 ?
Solution 1:[1]
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
Solution 2:[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 !!
Solution 3:[3]
Try to add your user to nginx group like:
sudo usermod -a -G user nginx
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ??? |
| Solution 2 | Tiago Almeida |
| Solution 3 | Zeddin Arief |
