nginx + uWSGI Unix Domain Socket Error in /tmp: Debugging “No such file or directory” Issues


2 views

When configuring nginx with uWSGI using Unix domain sockets on Fedora 17, I encountered a perplexing situation: sockets work perfectly when placed in custom directories with proper permissions, but fail with "No such file or directory" errors when placed in /tmp - despite the socket file clearly existing with correct permissions.

The error message appears in nginx logs:

connect() to unix:/tmp/MySite.sock failed (2: No such file or directory) while connecting to upstream

Key observations:

  • Socket file exists with 777 permissions (temporary test)
  • Both nginx and uWSGI running as same user
  • Works perfectly when socket is in /var/run/mysite
  • Fails consistently in /tmp

After extensive debugging, the root cause emerged: Fedora 17 mounts /tmp with the noexec,nosuid,nodev flags by default, and more critically for our case, some systems use private or tmpfs mount options that create separate namespace views.

This means:

$ mount | grep /tmp
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noexec,relatime,size=102400k)

Option 1: Change Socket Location

Best practice is to use /var/run or a dedicated directory:

# In nginx.conf
location / {
    include uwsgi_params;
    uwsgi_pass unix:/var/run/mysite/mysite.sock;
}

# In uWSGI config
[uwsgi]
socket = /var/run/mysite/mysite.sock
chmod-socket = 660
chown-socket = nginx:nginx

Option 2: Adjust tmpfs Mount (Temporary Fix)

For development environments, you could remount /tmp:

sudo mount -o remount,exec,suid,dev /tmp

But this isn't recommended for production due to security implications.

Here's how to create a proper socket directory:

sudo mkdir -p /var/run/mysite
sudo chown nginx:nginx /var/run/mysite
sudo chmod 755 /var/run/mysite

# Systemd tmpfiles.d setup to persist across reboots
echo "d /var/run/mysite 0755 nginx nginx -" | sudo tee /etc/tmpfiles.d/mysite.conf

To confirm everything works:

  1. Start uWSGI with the new socket path
  2. Check socket file exists: ls -la /var/run/mysite/
  3. Test connection: sudo -u nginx curl --unix-socket /var/run/mysite/mysite.sock http://localhost
  4. Check nginx error logs for any remaining issues

When configuring Nginx with uWSGI using Unix domain sockets on Fedora 17, many developers encounter the perplexing "No such file or directory" error despite the socket file existing with correct permissions. This typically occurs when placing the socket in /tmp directory.

Fedora's /tmp implementation has several special characteristics:


# Check /tmp mount options
$ mount | grep /tmp
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,seclabel)

The key factors affecting socket communication:

  • tmpfs filesystem behavior
  • SELinux context requirements
  • Sticky bit permissions (1777)

First confirm proper permissions exist:


# Check socket permissions
$ ls -laZ /tmp/MySite.sock
srw-rw-rw-. nginx nginx unconfined_u:object_r:user_tmp_t:s0 /tmp/MySite.sock

# Verify process ownership
$ ps aux | grep 'nginx: worker'
nginx    12345  0.0  0.1 123456 7890 ?        S    12:34   0:00 nginx: worker process

Fedora's SELinux often blocks socket communication. Apply these fixes:


# Temporary solution (until next reboot)
$ chcon -t httpd_tmp_t /tmp/MySite.sock

# Permanent solution
$ semanage fcontext -a -t httpd_tmp_t "/tmp/MySite.sock"
$ restorecon -v /tmp/MySite.sock

For production systems, consider these alternatives to /tmp:


# Recommended locations:
/var/run/MySite/
/opt/MySite/run/
/home/MySite/.sockets/

# Example Nginx configuration:
location / {
    include uwsgi_params;
    uwsgi_pass unix:/var/run/MySite/MySite.sock;
}

Proper systemd unit file for uWSGI:


[Unit]
Description=MySite uWSGI service
After=network.target

[Service]
User=nginx
Group=nginx
WorkingDirectory=/var/www/MySite
Environment="PATH=/usr/local/bin:/usr/bin:/bin"
ExecStart=/usr/bin/uwsgi --ini /etc/uwsgi/MySite.ini
RuntimeDirectory=MySite
RuntimeDirectoryMode=0750

[Install]
WantedBy=multi-user.target

Essential diagnostic tools:


# Check SELinux denials
$ ausearch -m avc -ts recent

# Verify socket accessibility
$ sudo -u nginx stat /tmp/MySite.sock

# Test socket communication
$ sudo -u nginx nc -U /tmp/MySite.sock