Sub topic 5
Unix sockets or TCP ports between web server and app
Nginx and your application server run as separate processes, so they need a channel between them. On one machine you have two practical choices: a Unix domain socket (a special file) or a TCP port on the loopback address. This chapter explains how each works, how to configure both for PHP-FPM and Gunicorn, and which errors each one produces.
It assumes you can edit Nginx configuration, read file modes such as 0660, and run commands with sudo on a Debian/Ubuntu-style server. Later chapters use these settings when connecting Nginx to PHP-FPM and Gunicorn.
The two kinds of address
A TCP address is an IP address plus a port, such as 127.0.0.1:8000. The kernel sends the data through its network stack, even though the traffic never leaves the machine. Any local process that can reach the address can connect to it.
A Unix domain socket is an endpoint named by a path in the filesystem, such as /run/gunicorn/app.sock. Both processes must run on the same host. The data goes straight through the kernel from one process to the other, with no IP or port involved. The socket file has an owner, a group, and a mode like any other file, so ordinary file permissions decide who may connect.
Comparing the two
| Aspect | Unix socket | TCP port |
|---|---|---|
| Access control | File owner, group, and mode | Firewall rules, or binding to loopback only |
| Hosts | Same machine only (or a shared volume) | Same machine or across the network |
| Overhead | Slightly lower, no network stack | Slightly higher |
| Visible to `ss -ltn` | No, use `ss -xl` | Yes |
| Typical failure | Permission denied on the file | Port already in use, or exposed to the network |
| Containers | Needs a shared volume | Works across containers and hosts |
The speed difference is real but small. For most applications the time spent in PHP or Python is far larger than the connection cost. Choose on access control and deployment shape first.
A common rule of thumb:
- Use a Unix socket when Nginx and the app server share one host and you want file-based permissions.
- Use TCP when they run in different containers or on different hosts, or when a tool needs a port number, such as a health checker or a load balancer.
Configuring PHP-FPM
PHP-FPM chooses its address with the listen directive in a pool file. On Debian/Ubuntu with PHP 8.3 the default pool is /etc/php/8.3/fpm/pool.d/www.conf. The socket path and PHP version depend on your distribution and installed package.
[www]
user = www-data
group = www-data
listen = /run/php/php8.3-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660The user and group lines set who the PHP workers run as. The listen.* lines set who may connect to the socket. These are separate settings. If you leave out listen.owner, listen.group, and listen.mode, the socket belongs to the user that started the master process, which is usually root. Nginx workers can then fail to connect.
The TCP version changes one line:
listen = 127.0.0.1:9000In Nginx, fastcgi_pass must match whichever you chose:
# Unix socket
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
# TCP
fastcgi_pass 127.0.0.1:9000;Configuring Gunicorn
Gunicorn uses --bind. The unix: prefix selects a socket file. Use --umask to control the file mode.
gunicorn --workers 3 --umask 007 --bind unix:/run/gunicorn/app.sock app:applicationA umask of 007 removes all permissions for "others", so the socket is accessible only to its owner and group. Set the umask explicitly, because relying on the default can leave the socket more open than you intend.
For TCP, bind to the loopback address:
gunicorn --workers 3 --bind 127.0.0.1:8000 app:applicationThe matching Nginx directives are:
# Unix socket
proxy_pass http://unix:/run/gunicorn/app.sock;
# TCP
proxy_pass http://127.0.0.1:8000;Under systemd, create the socket directory with RuntimeDirectory and set the group so Nginx can reach the socket. This example runs the app as its own user and shares the group with Nginx:
[Unit]
Description=Gunicorn for myapp
After=network.target
[Service]
User=myapp
Group=www-data
WorkingDirectory=/srv/myapp
RuntimeDirectory=gunicorn
RuntimeDirectoryMode=0750
ExecStart=/srv/myapp/venv/bin/gunicorn --workers 3 --umask 007 --bind unix:/run/gunicorn/app.sock app:application
Restart=on-failure
[Install]
WantedBy=multi-user.targetWith this unit, the directory is 0750 owned by myapp:www-data, and the socket is 0770 with the same owner and group. Nginx workers run as www-data on Debian/Ubuntu, so they can enter the directory and write to the socket. Other users cannot.
Inspecting what is listening
Check the real state before you read configuration files.
# Unix sockets in the listening state, with the owning process
sudo ss -xlp | grep -E 'gunicorn|php'
# TCP listeners
sudo ss -ltnp | grep -E 'gunicorn|php'
# File mode, owner, and group of the socket
ls -l /run/gunicorn/app.sock
# Talk to Gunicorn directly through the socket, bypassing Nginx
curl --unix-socket /run/gunicorn/app.sock http://localhost/The last command is the quickest way to tell whether the problem is in the app or in Nginx. If curl works through the socket but the site fails, look at the Nginx side.
Reading the errors
Nginx reports the system error number when a connection to the upstream fails. Look in /var/log/nginx/error.log for lines like connect() to unix:/run/gunicorn/app.sock failed (13: Permission denied) while connecting to upstream. The browser sees a 502 Bad Gateway in each case.
| Error in the log | Meaning | Usual fix |
|---|---|---|
| `(2: No such file or directory)` | The socket file does not exist | The app server is not running, or the path differs between the two configurations |
| `(13: Permission denied)` | The file exists but Nginx's user cannot use it | Fix the socket's group and mode, or a directory on the path |
| `(111: Connection refused)` | Nothing is listening: a stale socket file, or a TCP port with no process | Start or restart the app server |
A leftover socket file from a crashed process gives error 111 rather than error 2, because the path exists but nobody is listening on it.
Gotchas
- Bind TCP to loopback.
listen = 0.0.0.0:9000for PHP-FPM exposes the FastCGI protocol to the network. Anyone who can reach that port can ask PHP to run files on your server. Use127.0.0.1unless a firewall restricts access and you truly need remote access. - Do not put sockets in
/tmp. If Nginx or the app runs under systemd withPrivateTmp=true, each service sees its own private/tmp, so the socket is invisible to the other. Use/run. - Keep the paths identical. After a PHP upgrade the default socket name changes, for example from
php8.2-fpm.socktophp8.3-fpm.sock. Nginx keeps pointing at the old name until you edit it. - Mind the backlog. Both mechanisms queue connections when workers are busy. The queue length comes from the
listen.backlogsetting for PHP-FPM and from--backlogfor Gunicorn, and the kernel caps it atnet.core.somaxconn. A small queue under load produces refused connections even though the service is up. - Test before reloading. After changing either side, run
sudo nginx -tand thensudo systemctl reload nginx.
Exercise
Nginx's error log shows connect() to unix:/run/gunicorn/app.sock failed (13: Permission denied). The socket file exists and Gunicorn is running. Name two things you would check and the command for each.
Show answer
First, check the socket's owner, group, and mode with ls -l /run/gunicorn/app.sock. The Nginx user (www-data on Debian/Ubuntu) must be the owner or in the group, and that class must have write permission. Second, check every directory on the path with namei -l /run/gunicorn/app.sock. Each one needs execute permission for the Nginx user. You can also set --umask 007 and a shared group in the Gunicorn service.
Exercise
You move Gunicorn into a container while Nginx stays on the host. Why does a Unix socket become harder to use, and what would you change in the --bind value and the Nginx proxy_pass line?
Show answer
A Unix socket exists only inside the filesystem where it was created, so the host cannot see it unless you share the directory with a volume mount. The simpler choice is TCP. Publish a port from the container, bind Gunicorn to an address inside the container such as 0.0.0.0:8000, and set proxy_pass http://127.0.0.1:8000; on the host. Publish the port only on the host's loopback address so it is not exposed to the network.