502 Bad Gateway - django + nginx + gunicorn - sock failed (13: Permission denied) 502 Bad Gateway - django + nginx + gunicorn - sock failed (13: Permission denied) nginx nginx

502 Bad Gateway - django + nginx + gunicorn - sock failed (13: Permission denied)


Seems there are permission issues start your nginx service as root because root has permission to access your my project.sock

sudo service nginx stop

Then

 sudo service nginx start

But it is not a good idea to start nginx as root. You may try changing permission of your current user

I can suggest you an alternative which works fine. Let a shell script handle all this. Create a shell script like this (* indicates required in comments)

#!/bin/bashNAME=""                              #Name of the application (*)DJANGODIR=/path/to/django/project            # Django project directory (*)SOCKFILE=/path/to/socket/file/myproject.sock        # we will communicate using this unix socket (*)USER=                                      # the user to run as (*)GROUP=                                     # the group to run as (*)NUM_WORKERS=1                                     # how many worker   processes should Gunicorn spawn (*)DJANGO_SETTINGS_MODULE=yourproject.settings             # which settings file should Django use (*)DJANGO_WSGI_MODULE=yourproject.wsgi                     # WSGI module name (*)echo "Starting $NAME as `whoami`"# Activate the virtual environmentcd $DJANGODIRsource /path/to/virtualenv/bin/activateexport DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULEexport PYTHONPATH=$DJANGODIR:$PYTHONPATH# Create the run directory if it doesn't existRUNDIR=$(dirname $SOCKFILE)test -d $RUNDIR || mkdir -p $RUNDIR# Start your Django Unicorn# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)exec /path/to/virtualenv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \  --name $NAME \  --workers $NUM_WORKERS \  --user $USER \  --bind=unix:$SOCKFILE


link to similar question

I just ran into this problem. I was able to create the gunicorn socket file, but nginx complained about permission denied. The issue was that my socket file was in a sub-folder and the root folder did not have read or execute permissions. So even though the sub-folder had the correct permissions, the root folder prevented nginx from entering the sub-folder.

The solution was to add read and execute permissions to the root folder:

chmod o+rx /example_root_folder


I had a very similar problem like you before (also proceeded according to the mentioned tutorial on the digitalocean.com), by this I mean the socket was not working and also I was getting the 502 error.

The solution for me was to move from socket binding to binding a normal IP address.

Inside of a virtualenv (next to "manage.py") I have a gunicorn.py file: (NOTE: I use 3.4 version)

#!/usr/bin/python3.4"""Run Gunicorn (Django) on a specific IP addr"""import os# Change directory to the virtualenv folderos.chdir("/home/your_path/name_of_env/")# Run Gunicornos.system("bin/gunicorn -w 3 -b 127.0.0.1:8000 your_project.wsgi:application &")

The script runs Gunicorn without activating the virtualenv (command "bin/gunicorn") and in background ("&").

NOTE: If you don't use virtualenv than remove "bin/" from the script above.

For calling that script after booting PC and login, add this line to your ".profile" file at the end. (It's inside your home directory)

python3.4 path/to/the/file/gunicorn.py

In a nginx file you should have this part of code:

location / {    proxy_pass http://127.0.0.1:8000;    proxy_set_header Host $host;    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;}

Remember, the IP addr 127.0.0.1:8000 is reserved. So if you want to run Django developing server, you have 2 options:

  1. Turn off Gunicorn by commands "ps aux | grep gunicorn" and "kill -9 [PID]" and then normally run a command "python3.4 manage.py runserver"
  2. Or run Django developing server on a different IP and port, for example "python3.4 manage.py runserver 0.0.0.0:8001"

This solution works for me.