title: wsgi nginx error: permission denied while connecting to upstream
body: <p>There seem to be many questions on StackOverflow about this but unfortunately nothing has worked for me.</p>
  
  <p>I'm getting a 502 bad gateway on nginx, and the following on the logs: <code>connect() to ...myproject.sock failed (13: Permission denied) while connecting to upstream</code></p>
  
  <p>I'm running <code>wsgi</code> and <code>nginx</code> on <code>ubuntu</code>, and I've been following <a href="https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04" rel="noreferrer">this guide from Digital Ocean</a>. I apparently configured <code>wsgi</code> correctly since <code>uwsgi -s myproject.sock --http 0.0.0.0:8000 --module app --callable app</code> worked, but I keep getting the <code>nginx</code> permission denied error and I have no idea why:</p>
  
  <p>After coming across <a href="https://stackoverflow.com/questions/21820444/nginx-error-13-permission-denied-while-connecting-to-upstream">this question</a> and <a href="https://stackoverflow.com/questions/22071681/permission-denied-nginx-and-uwsgi-socket">this other one</a>, I changed the <code>.ini</code> file and added the <code>chown-socket</code>, <code>chmod-socket</code>, <code>uid</code> and <code>gid</code> parameters (also tried just setting the first two, either or, and a couple of different permission settings --and even the most permissive didn't work).</p>
  
  <p><a href="https://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx">This one seemed promising</a>, but I don't believe <code>selinux</code> is installed on my Ubuntu (running <code>sudo apt-get remove selinux</code> gives "Package 'selinux' is not installed, so not removed" and <code>find / -name "selinux"</code> doesn't show anything). Just in case, though, I tried what <a href="http://www.thegeekstuff.com/2009/06/how-to-disable-selinux-redhat-fedora-debian-unix/" rel="noreferrer">this post</a> recommended as well. Uninstalling <code>apparmor</code> (<code>sudo apt-get install apparmor</code>) didn't work either.</p>
  
  <p>Every time I make a change, I run <code>sudo service nginx restart</code>, but I only see the 502 Gateway Error (and the permission denied error when I read the logs).</p>
  
  <p>This is is my <code>nginx</code> configuration file:</p>
  
  <pre><code>server {
      listen 80;
      server_name 104.131.110.156;
  
      location / {
          include uwsgi_params;
          uwsgi_pass unix:/home/user/myproject/web_server/myproject.sock;
      }
  }
  </code></pre>
  
  <p><code>.conf</code> file:</p>
  
  <pre><code>description "uWSGI server instance configured to serve myproject"
  
  start on runlevel [2345]
  stop on runlevel [!2345]
  
  setuid user
  setgid www-data
  
  env PATH=/root/.virtualenvs/my-env/bin
  chdir /home/user/myproject/web_server
  exec uwsgi --ini /home/user/myproject/web_server/myproject.ini
  </code></pre>
  
  <p><code>.ini</code> file:</p>
  
  <pre><code>[uwsgi]
  module = wsgi
  
  master = true
  processes = 5
  
  socket = /home/user/myproject/web_server/myproject.sock
  chown-socket=www-data:www-data
  chmod-socket = 664
  uid = www-data
  gid = www-data
  
  vacuum = true
  die-on-term = true
  </code></pre>
  
  <p>(If it helps, these are the specs of my Digital Ocean machine: <code>Linux 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux</code>)</p>
  
  <p>Please let me know if there's anything I can do, and thank you very much.</p>