Sub topic 5
Unix Sockets or TCP Ports Between Web Server and App
Assume you already understand basic Linux permissions, HTTP requests, Nginx configuration, and how to edit and restart services. This chapter focuses on the connection between Nginx and an application process after Nginx has received a request.
Nginx can connect to an application through a Unix domain socket or a TCP port. Both provide a stream of bytes, but they differ in addressing, permissions, portability, and security.
How the connection works
A Unix socket is a special file on the same server. A TCP connection uses an IP address and port, such as 127.0.0.1:8000.
With PHP-FPM, Nginx commonly uses FastCGI:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}The same PHP-FPM pool could listen on TCP instead:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
}The application must be configured to use the matching endpoint. Changing only Nginx creates a connection failure.
Unix sockets
Unix sockets are usually a good default when Nginx and the application run on one host. They avoid TCP port allocation and can enforce access through normal file permissions.
For example, the socket might have an owner and group that allow Nginx to connect:
ls -l /run/php/php8.3-fpm.sockA permission error can produce a 502 Bad Gateway response. The Nginx user needs permission to access the socket and every parent directory in its path.
Unix sockets are local. They cannot directly connect services on another server, and container or chroot boundaries can make the socket path unavailable.
TCP ports
TCP is useful when the application runs in another process environment, container, or host. It is also easier to test with common network tools.
For a local Gunicorn process, Nginx might use:
location / {
proxy_pass http://127.0.0.1:8000;
}Check which process is listening:
sudo ss -ltnp
sudo ss -lxnpThe first command lists TCP listeners. The second lists Unix sockets. 127.0.0.1 limits TCP access to the local machine. Avoid binding an internal application to 0.0.0.0 unless remote access is required and firewall rules protect it.
| Connection | Best fit | Main concern |
|---|---|---|
| Unix socket | Same-host services | File ownership and permissions |
| TCP loopback | Same-host or container services | Correct address and port |
| TCP private network | Services on separate hosts | Firewall and transport security |
Troubleshooting endpoint failures
A 502 usually means Nginx could not communicate with the upstream application. Check these items:
- Is the application process running?
- Does Nginx use the exact socket path or port?
- Does the socket exist?
- Can the Nginx user access the socket?
- Is another process already using the TCP port?
- Is a firewall blocking the connection?
For a TCP application, test the endpoint directly:
curl -i http://127.0.0.1:8000/
sudo tail -n 30 /var/log/nginx/error.logA successful direct request proves that the application is reachable from the shell, but it does not prove that Nginx has the required permissions or configuration.
Exercise
Nginx returns 502 after PHP-FPM was changed from a Unix socket to TCP port 9000. What two configuration areas must you check?
Show answer
Check that PHP-FPM is listening on the intended address and port, and that Nginx uses the same endpoint in its fastcgi_pass directive.