Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx cannot connect to the Gunicorn socket: Permission denied

Error(13: Permission denied) while connecting to upstream

Short answer

Error 13 means the operating system refused nginx permission to open the socket file. The nginx worker user must be the socket owner or in its group, and the mode must allow it. Set SocketUser, SocketGroup and SocketMode in a systemd socket unit, test as the nginx user, or switch to a loopback TCP port.

  • Applies to nginx 1.26, Gunicorn (current docs), Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at who owns the socket and which user nginx workers run as. Then try to connect as that user: if curl fails with a permission error, the problem is the socket, not nginx config.

    ls -l /run/gunicorn.sock
    grep -E "^\s*user" /etc/nginx/nginx.conf
    sudo -u www-data curl --unix-socket /run/gunicorn.sock http://localhost/
  2. 2Match what you see, then jump to its fix

    1. 1ls -l shows a socket owned by another user, with no write access for the group nginx runs inThe socket owner, group or mode excludes the nginx userFix
    2. 2Gunicorn started without systemd socket activation, so no SocketMode appliesGunicorn creates the socket itself and its file mode is not what you thinkFix

What does the error look like?

  • (13: Permission denied) while connecting to upstream
  • 502 Bad Gateway

Cause 1 of 2

The socket owner, group or mode excludes the nginx user

1Check

Compare the socket's owner, group and mode with the user line in nginx.conf. nginx must be the owner, or a member of the group, and that group needs write access to the socket.

ls -l /run/gunicorn.sock
systemctl cat gunicorn.socket

2Fix

Let systemd create the socket and set its owner, group and mode in the socket unit, with a group nginx belongs to.

# /etc/systemd/system/gunicorn.socket
[Socket]
ListenStream=/run/gunicorn.sock
SocketUser=www-data
SocketGroup=www-data
SocketMode=0660

[Install]
WantedBy=sockets.target

3Verify

Reload systemd, restart the socket, and repeat the test as the nginx user.

sudo systemctl daemon-reload
sudo systemctl restart gunicorn.socket
sudo -u www-data curl --unix-socket /run/gunicorn.sock http://localhost/

Confirmed by gunicorn.org

Did this fix it?

Cause 2 of 2

Gunicorn creates the socket itself and its file mode is not what you think

1Check

When Gunicorn binds the socket itself, its umask setting controls the file mode of the socket. See which bind and umask the service uses.

systemctl cat gunicorn | grep -i -E 'bind|umask|ExecStart'

2Fix

The simpler fix is to stop using a socket file: bind a loopback port and point nginx at it. A TCP port has no file mode to get wrong.

# Gunicorn
gunicorn --bind 127.0.0.1:8000 myproject.wsgi:application

# nginx
location / {
    proxy_pass http://127.0.0.1:8000;
}

3Verify

Reload nginx and request the site.

sudo nginx -t && sudo systemctl reload nginx
curl -I http://127.0.0.1:8000

Confirmed by gunicorn.org

Did this fix it?

How do I stop it coming back?

  • Prefer systemd socket activation, so owner, group and mode are written down in one unit.
  • After any change of the nginx user or the socket path, repeat the sudo -u curl test.
  • Keep sockets under /run, which is meant for runtime files, not under a home directory.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.