How PHP‑FPM Communicates with Nginx: FastCGI Explained with Real Config Examples

This article explains the role of PHP‑FPM as a FastCGI process manager, describes how Nginx acts as a reverse proxy, details the communication steps between them, and provides practical configuration snippets for both TCP and Unix domain sockets.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How PHP‑FPM Communicates with Nginx: FastCGI Explained with Real Config Examples

PHP‑FPM

PHP‑FPM (PHP FastCGI Process Manager) implements the FastCGI protocol and adds process‑management capabilities. It runs a single master process that listens on a port and spawns configurable worker processes, each embedding a PHP interpreter to execute PHP code.

Nginx

Nginx (pronounced “engine x”) is a high‑performance HTTP server, reverse proxy, and also supports IMAP/POP3/SMTP. The article briefly distinguishes forward proxy and reverse proxy concepts.

Forward Proxy

Reverse Proxy

PHP‑FPM + Nginx Communication

FastCGI reduces the overhead of web‑server‑to‑CGI interaction by reusing persistent processes instead of spawning a new process per request. The FastCGI manager (PHP‑FPM) handles these processes, while Nginx forwards dynamic requests to it.

Communication Diagram

(1) Nginx receives an HTTP request and initializes the FastCGI environment.

(2) A typical Nginx configuration line for PHP requests is: fastcgi_pass 127.0.0.1:9000; or

fastcgi_pass  unix:/tmp/php-cgi.sock;
This line tells Nginx where to send dynamic requests for PHP processing.

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

(4) The master selects an idle worker, passes CGI environment variables and STDIN to it.

(5) The worker processes the script and returns STDOUT and any error output through the same socket.

(6) The worker closes the connection and waits for the next request.

Different Configuration Scenarios

Nginx also has master and worker processes; workers handle each network request.

In the Nginx+PHP architecture, PHP acts as a CGI program, so PHP‑FPM (a FastCGI manager) listens on a port (default 9000) and has its own master/worker model.

FastCGI is a protocol; many implementations exist, PHP‑FPM being one of them.

FastCGI configuration files are usually placed alongside nginx.conf and come in two forms:

/usr/local/openresty/nginx/conf/fastcgi.conf
fastcgi_params

(the PHP‑FPM configuration entry point)

When handling a PHP request, Nginx’s worker hands it to a PHP‑FPM worker, effectively an indirect call to PHP.

location ~ \.php$ {
    try_files $fastcgi_script_name =404;
    include fastcgi_params;
    # fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Key directives: include fastcgi_params; – loads the FastCGI parameter file. fastcgi_pass 127.0.0.1:9000; – sends the request to the FastCGI server listening on that address.

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

– tells PHP‑FPM the script’s full path.

fastcgi_pass

Nginx can communicate with PHP‑FPM via two methods: TCP socket (IP + port) or Unix domain socket (file).

TCP sockets work across servers; Unix sockets avoid the network stack and require both processes on the same machine.

TCP Socket Method

In nginx.conf: fastcgi_pass 127.0.0.1:9000; In php-fpm.conf: listen=127.0.0.1:9000; When both are on the same host, the data flow is: Nginx <=> socket <=> TCP/IP <=> socket <=> PHP‑FPM When on different hosts, the flow includes the physical network layer and routers.

Nginx <=> socket <=> TCP/IP <=> physical layer <=> router <=> physical layer <=> TCP/IP <=> socket <=> PHP‑FPM

Unix Domain Socket Method

In nginx.conf: fastcgi_pass unix:/tmp/php-fpm.sock; In PHP‑FPM configuration: listen = /tmp/php-fpm.sock; (the socket file is created by PHP‑FPM)

Communication flow:

Nginx <=> socket <=> PHP‑FPM

include fastcgi_params

The fastcgi_params (and fastcgi.conf) files contain many fastcgi_param directives that are passed to PHP‑FPM. These parameters populate the $_SERVER superglobal in PHP, providing request metadata such as QUERY_STRING, REQUEST_METHOD, SCRIPT_NAME, and client address.

$ cat fastcgi_params

fastcgi_param  QUERY_STRING        $query_string;
fastcgi_param  REQUEST_METHOD      $request_method;
fastcgi_param  CONTENT_TYPE        $content_type;
fastcgi_param  CONTENT_LENGTH      $content_length;

fastcgi_param  SCRIPT_NAME         $fastcgi_script_name;
fastcgi_param  REQUEST_URI         $request_uri;
fastcgi_param  DOCUMENT_URI        $document_uri;
fastcgi_param  DOCUMENT_ROOT       $document_root;
fastcgi_param  SERVER_PROTOCOL     $server_protocol;
fastcgi_param  REQUEST_SCHEME      $scheme;
fastcgi_param  HTTPS               $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE   CGI/1.1;
fastcgi_param  SERVER_SOFTWARE     nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR         $remote_addr;
fastcgi_param  REMOTE_PORT         $remote_port;
fastcgi_param  SERVER_ADDR         $server_addr;
fastcgi_param  SERVER_PORT         $server_port;
fastcgi_param  SERVER_NAME         $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS     200;
All these values are transmitted to the PHP‑FPM managed FastCGI process, enabling PHP scripts to access request details via the $_SERVER array.
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.

Server Configurationfastcgiphp-fpm
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.