Nginx: 502 Bad Gateway error with (111: Connection refused) to uWSGI socket while running django

djangonginxpermissionspythonuwsgi

I am building an app using an Nginx, uWSGI, Django and Postgresql stack. The app uses a Unix socket between Nginx and uWSGI. The file permissions on the socket is 775. But I still get this permission error:

[error] 6978#0: *6725 connect() to unix:/path/to/socket failed (111: Connection refused) while connecting to upstream, client: 54.250.253.225, server: example.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/path/to/socket:", host: "example.com"

Nginx Configuration

# uWSGI upstream
upstream app{
    server  unix:/path/to/socket;
    }

# redirect www to non-www
server{
    listen  80;
    server_name www.example.com;
    return  301 http://example.com$request_uri;
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for

    # substitute your machine's IP address or FQDN
    server_name example.com
    charset     utf-8;

    # root folder
    root    /path/to/root;

    # max upload size
    client_max_body_size 20M;   # adjust to taste

    access_log  /path/to/access.log;
    error_log   /path/to/error.log;

    # Django media
    location /media  {
    # your Django project's media files - amend as required
    alias /path/to/media;  
    }

    location /static {
    # your Django project's static files - amend as required
    alias /path/to/static; 
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  app;
    # the uwsgi_params file you installed
    include     /path/to/uwsgi_params; 

    uwsgi_param Host $host;
    uwsgi_param X-Real-IP $remote_addr;
    uwsgi_param UWSGI_SCHEME $scheme;
    uwsgi_param SERVER_SOFTWARE nginx/nginx_version;
    }
}

The files in my virtual environment have all been chown to ubuntu:ubuntu. The nginx user is ubuntu.

uWSGI log

*** Starting uWSGI 2.0.12 (64bit) on [Sun Apr 17 17:24:37 2016] ***
compiled with version: 4.8.4 on 17 April 2016 14:40:25
os: Linux-3.13.0-85-generic #129-Ubuntu SMP Thu Mar 17 20:50:15 UTC 2016
nodename: ip-10-167-29-5
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /path/to/app
detected binary path: /path/to/bin/uwsgi
chdir() to /path/to/chdir
your processes number limit is 30035
your memory page size is 4096 bytes
 *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers *** 
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /path/to/socket fd 3
Python version: 3.4.3 (default, Oct 14 2015, 20:31:36)  [GCC 4.8.4]
Set PythonHome to /path/to/home
Python main interpreter initialized at 0xa4df10
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 436608 bytes (426 KB) for 5 cores
*** Operational MODE: preforking ***
/path/to/lib/python3.4/site-packages/cloudinary/models.py:24: RemovedInDjango110Warning: SubfieldBase has been deprecated. Use Field.from_db_value instead.
  return meta(name, bases, d)

WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0xa4df10 pid: 8567 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 8567)
spawned uWSGI worker 1 (pid: 8569, cores: 1)
spawned uWSGI worker 2 (pid: 8570, cores: 1)
spawned uWSGI worker 3 (pid: 8571, cores: 1)
spawned uWSGI worker 4 (pid: 8572, cores: 1)
spawned uWSGI worker 5 (pid: 8573, cores: 1)

Can someone please tell me what is causing this permissions problem?

Best Answer

It looks like uWSGI service is listening on a Unix socket w/different absolute path (due to chdir() call). You can use lsof | grep "/path/to/socket" to find an absolute path of a Unix socket being listened on. I guess it is /path/to/chdir/path/to/socket.