FastCGI Proxying

Beginner
6 min read
Updated: Nov 2026
3 examples
PHP-FPM

Definition

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.

Nginx
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

NamePlain 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

Example 1 — Minimal PHP Site

Static files from root; PHP files go to PHP-FPM on port 9000.

Nginx
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

  1. root sets where .php files live on disk.
  2. fastcgi_pass points at PHP-FPM.
  3. 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.

Nginx
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

  1. Match the socket path to your PHP version.
  2. Permissions must allow the Nginx user to connect.
  3. Test with a tiny info.php, then remove it.

Example 3 — Keep Images on Nginx

Serve media from disk; only dynamic scripts hit FastCGI.

Nginx
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

  1. Static regex avoids PHP for images.
  2. Less load on PHP-FPM.
  3. Same idea as the official Beginner’s Guide split.

Frequently Asked Questions

Use proxy_pass for HTTP backends (Node, Python WSGI via HTTP). Use fastcgi_pass for FastCGI apps like PHP-FPM.
The full path of the PHP script to execute. Usually $document_root$fastcgi_script_name.
PHP-FPM may be down, the socket path wrong, or SCRIPT_FILENAME incorrect. Check FPM logs and nginx error.log.
PHP is the common case, but any FastCGI-capable app can sit behind fastcgi_pass.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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