How to Pass Custom FastCGI Parameters from Nginx to PHP-FPM: A Complete Guide


3 views

When working with Nginx and PHP-FPM, passing custom parameters requires understanding several configuration layers. The fastcgi_param directive is indeed the proper mechanism, but its behavior depends on both Nginx and PHP-FPM configurations.

To make custom parameters visible in PHP, you need to ensure they're set in the correct context and properly processed by PHP-FPM:

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_param FOO "bar";  # Custom parameter
    fastcgi_param PATH_INFO $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Custom FastCGI parameters become available in PHP's $_SERVER superglobal:

// Accessing our custom parameter
echo $_SERVER['FOO'];  // Outputs "bar"

// Verifying PATH_INFO
var_dump($_SERVER['PATH_INFO']);

Parameter Visibility Issues: If parameters don't appear in PHP:

  1. Ensure your location block includes the fastcgi_params file
  2. Check for parameter overrides in included config files
  3. Verify PHP-FPM isn't clearing environment variables

For complex scenarios, you might need to modify PHP-FPM's pool configuration:

; In php-fpm pool config (www.conf)
clear_env = no  # Preserves environment variables
env[FOO] = $FOO # Explicitly pass through
  • Never pass sensitive data via FastCGI parameters
  • Validate all server-passed parameters in PHP
  • Consider using HTTP headers for some use cases

Each additional parameter adds minimal overhead, but excessive parameters can impact performance in high-traffic scenarios. Benchmark critical paths when implementing custom parameter passing.


When working with Nginx and PHP-FPM, developers often need to pass server-specific configuration or environment variables to their PHP applications. The standard approach using env directive in Nginx is limited because it only works in the main context, while most web applications require server or location-level configuration.

The most effective method is using fastcgi_param directive in Nginx configuration. This allows passing both standard and custom variables to PHP through the FastCGI protocol.

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_param MY_CUSTOM_VAR "development";
    fastcgi_param APPLICATION_ENV "staging";
}

For these parameters to be received by PHP:

  • PHP-FPM must be configured to accept these parameters (default configuration usually allows this)
  • The variables_order in php.ini should include "E" for environment variables
  • No conflicting values should be set in other parts of the configuration

Custom FastCGI parameters appear in PHP's $_SERVER superglobal:

// Accessing custom parameters
$environment = $_SERVER['APPLICATION_ENV'] ?? 'production';
$customVar = $_SERVER['MY_CUSTOM_VAR'] ?? null;

// Debug output
var_dump($_SERVER['APPLICATION_ENV'], $_SERVER['MY_CUSTOM_VAR']);

Case Sensitivity: Nginx variable names are case-sensitive and should match exactly in PHP.

Inheritance: Parameters set in higher-level blocks can be overridden in more specific locations.

Security: Sensitive data should be passed this way only when absolutely necessary.

For complex scenarios, you might need to:

# Set different values based on conditions
map $http_host $app_env {
    default "production";
    "~*dev" "development";
    "~*staging" "staging";
}

server {
    location ~ \.php$ {
        fastcgi_param APPLICATION_ENV $app_env;
        # Other FastCGI parameters...
    }
}

To verify your configuration:

  1. Check Nginx error logs for configuration errors
  2. Create a PHP info page (phpinfo();) and search for your variables
  3. Test with simple echo scripts before implementing complex logic