Understanding PHP‑FPM and Nginx Communication: FastCGI Process Flow

This article explains how PHP‑FPM and Nginx interact via FastCGI, detailing the roles of master and worker processes, configuration directives, and the differences between TCP and Unix socket communication methods.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Understanding PHP‑FPM and Nginx Communication: FastCGI Process Flow

PHP‑FPM (PHP FastCGI Process Manager) implements FastCGI and provides process management. A FastCGI system consists of a single master process that listens on a port and multiple worker processes, each embedding a PHP interpreter to execute scripts.

Nginx is a high‑performance HTTP and reverse‑proxy server that also supports IMAP/POP3/SMTP. Understanding forward and reverse proxy concepts helps grasp how Nginx forwards dynamic requests to PHP‑FPM.

FastCGI reduces the overhead of spawning a new CGI process per request by keeping persistent worker processes. The communication steps are:

Nginx receives an HTTP request for a PHP script and initializes the FastCGI environment.

In the Nginx configuration, a fastcgi_pass directive tells Nginx where to send the request (e.g., 127.0.0.1:9000 or unix:/tmp/php-cgi.sock).

Nginx forwards the request via a socket to the FastCGI master process.

The master selects an idle worker, sends CGI environment variables and the request body to that worker.

The worker processes the PHP script and returns the output and any error messages through the same socket.

The worker closes the connection and waits for the next request.

Both Nginx and PHP‑FPM have master/worker architectures. PHP‑FPM typically listens on port 9000 (or a Unix socket) and handles PHP requests similarly to how Nginx handles network requests.

FastCGI configuration files are usually fastcgi.conf and fastcgi_params. A crucial parameter often missing in fastcgi_params is:

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

Adding this line ensures PHP‑FPM knows the script filename.

Typical Nginx location block for PHP:

location ~ \.php$ {
    try_files $fastcgi_script_name =404;
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

The fastcgi_pass directive can use two communication methods:

TCP Socket : fastcgi_pass 127.0.0.1:9000; – works across machines.

Unix Domain Socket : fastcgi_pass unix:/tmp/php-fpm.sock; – faster but only works when Nginx and PHP‑FPM reside on the same host.

Configuration examples:

TCP Socket

In nginx.conf: fastcgi_pass 127.0.0.1:9000; In php-fpm.conf:

listen=127.0.0.1:9000;

Unix Domain Socket

In nginx.conf: fastcgi_pass unix:/tmp/php-fpm.sock; In PHP‑FPM config: listen = /tmp/php-fpm.sock Choosing between TCP and Unix sockets depends on deployment topology: use TCP for distributed setups, Unix sockets for same‑host deployments to avoid network overhead.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend Architecturefastcgiphp-fpmUnix socketTCP socket
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.