title: Permission denied (502 Bad Gateway) for NGINX to access uWSGI socket with 'user www-data' but not 'user root' | NGINX uWSGI FLASK Ubuntu 22.04
body: <p>I am setting up a VM with NGINX, uWSGI FLASK, and Ubuntu 22.04.</p>
  <p>I know this question has been asked but I couldn't find a clear answer or a good solution.</p>
  <p>I get a 502 - Bad Gateway error from NGINX when its user in nginx.conf is 'www-data' but not when I set it as root or my sudo user.</p>
  <p>I created a project folder in my sudo-user home directory. Put the virtual environment inside. With the application files, etc. The website works with:</p>
  <pre><code>uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
  </code></pre>
  <p>But NGINX can't access my socket. No matter how I change my socket's permissions or ownership, I get a 502.</p>
  <p>But if the user in nginx.conf is root or my sudo-user name, then it works. I tried changing modes and ownerships but nothing works.</p>
  <p>Is it safe to use my sudo-user in nginx.conf?</p>
  <p>Does anybody have an idea how to solve this and grant access to the socket to www-data user?</p>
  <p>Current permissions for the files:</p>
  <pre><code>-rw-rw-r-- 1 anthony anthony   151 Aug 29 04:16 main.ini
  -rw-rw-r-- 1 anthony anthony   340 Aug 28 13:35 main.py
  srw-rw---- 1 anthony www-data    0 Aug 29 05:05 main.sock
  drwxrwxr-x 4 anthony anthony  4096 Aug 28 06:27 venv
  -rw-rw-r-- 1 anthony anthony    60 Aug 28 08:11 wsgi.py
  </code></pre>
  <p>Nginx sites-available file:</p>
  <pre><code>server {
      listen 80;
      server_name xyz.com www.xyz.com;
  
      location / {
          include uwsgi_params;
          uwsgi_pass unix:/home/anthony/website/main.sock;
      }
  }
  </code></pre>
  <p>Systemd service file:</p>
  <pre><code>[Unit]
  Description=uWSGI instance to serve xyz
  After=network.target
  
  [Service]
  User=anthony
  Group=www-data
  WorkingDirectory=/home/anthony/website
  Environment=&quot;PATH=/home/anthony/website/venv/bin&quot;
  ExecStart=/home/anthony/website/venv/bin/uwsgi --ini main.ini
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  <p>ini file for UWSGI:</p>
  <pre><code>[uwsgi]
  module = wsgi:app
  
  master = true
  processes = 5
  
  socket = /home/anthony/website/main.sock
  chmod-socket = 660
  vacuum = true
  
  die-on-term = true
  </code></pre>
  <p>Beginning of NGINX's conf file (nothing helpful here I think):</p>
  <pre><code>user www-data;
  worker_processes auto;
  pid /run/nginx.pid;
  include /etc/nginx/modules-enabled/*.conf;
  
  events {
          worker_connections 768;
          # multi_accept on;
  }
  </code></pre>
  <p>Thank you for your help.</p>