Home Nginx FastCGI FastCGI ProxyingDefinition FastCGI is a way Nginx talks to an app process (often PHP-FPM). Instead of proxy_pass (HTTP), you use fastcgi_pass and pass parameters with fastcgi_param.
A common beginner setup: Nginx serves images itself, and sends .php requests to 127.0.0.1:9000 (or a Unix socket).
Config Start with this shape. Adjust paths for your server.
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} PHP needs SCRIPT_FILENAME. Include fastcgi_params (or fastcgi.conf) from your distro for the other standard params.
Key Directives Name Plain meaning fastcgi_passAddress of the FastCGI server (host:port or unix socket). fastcgi_paramSet a FastCGI parameter passed to the app. include fastcgi_paramsLoad the usual parameter set shipped with Nginx. $document_rootCurrent root path — used to build SCRIPT_FILENAME.
Examples 1. Minimal PHP Site 2. Use a Unix Socket 3. Keep Images on Nginx Example 1 — Minimal PHP Site Static files from root; PHP files go to PHP-FPM on port 9000.
server {
listen 80;
server_name example.org;
root /data/www;
location / {
index index.html index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
} How It Works root sets where .php files live on disk.fastcgi_pass points at PHP-FPM.SCRIPT_FILENAME tells PHP which script to run.Example 2 — Use a Unix Socket Many Ubuntu PHP-FPM packages listen on a socket instead of a TCP port.
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} How It Works Match the socket path to your PHP version.Permissions must allow the Nginx user to connect.Test with a tiny info.php, then remove it.Example 3 — Keep Images on Nginx Serve media from disk; only dynamic scripts hit FastCGI.
location ~* \.(gif|jpg|png)$ {
root /data/images;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} How It Works Static regex avoids PHP for images.Less load on PHP-FPM.Same idea as the official Beginner’s Guide split.Frequently Asked Questions When do I use fastcgi_pass instead of proxy_pass? Use proxy_pass for HTTP backends (Node, Python WSGI via HTTP). Use fastcgi_pass for FastCGI apps like PHP-FPM.
What is SCRIPT_FILENAME? The full path of the PHP script to execute. Usually $document_root$fastcgi_script_name.
Why do I get a blank page or 502? PHP-FPM may be down, the socket path wrong, or SCRIPT_FILENAME incorrect. Check FPM logs and nginx error.log.
Is FastCGI only for PHP? PHP is the common case, but any FastCGI-capable app can sit behind fastcgi_pass.
Do I still need root? Yes for locating scripts on disk. FastCGI runs the script; Nginx still maps the URI to a file path.
Next: Server names Keep going through the beginner Nginx path.
Server names → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion