nginx cannot connect to the Gunicorn socket: Permission denied
(13: Permission denied) while connecting to upstreamShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
ls -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
Gunicorn started without systemd socket activation, so no SocketMode appliesGunicorn creates the socket itself and its file mode is not what you thinkFix
- 1
What does the error look like?
(13: Permission denied) while connecting to upstream502 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.socket2Fix
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.target3Verify
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
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:8000Confirmed by gunicorn.org
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
Servers
nginx shows 502 Bad Gateway in front of a Django app on Gunicorn
(13: Permission denied) while connecting to upstream
A 502 means nginx could not get an answer from Gunicorn.
4 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Your website on a Google Compute Engine VM times out on port 80
The site works with curl on the VM but not from outside
A timeout, as opposed to a refusal, usually means a firewall dropped the packets.
3 causes, each with a fixServers
Gunicorn rejects a request with "Error parsing headers: limit request headers fields size"
Error parsing headers: 'limit request headers fields'
Gunicorn caps the size of one header field at 8190 bytes and the number of headers at 100 by default.
2 causes, each with a fix