How to pass custom parameters to PHP from Nginx?

I'm using Nginx 1.2.4 in combination with PHP-FPM 5.4.8 with a fastcgi pass and trying to pass custom parameters to PHP. Here are the options I have found so far:

  • using the env directive to set an environment variable in nginx and fetch it with getenv() or $_ENV from PHP. The problem is that env only operates in main context whereas I need to set the parameter in server context.

  • using the fastcgi_param directive as it's designed for that.

I have tried changing the values of some parameters using fastcgi_param but was unsuccessful:

nginx:

fastcgi_param PATH_INFO "/var/tmp";

PHP:

I checked all the predefined arrays I could find:

echo '<pre>';
echo "\n".'$GLOBALS'."\n"; var_dump($GLOBALS);
echo "\n".'$_SERVER'."\n"; var_dump($_SERVER);
echo "\n".'$_GET'."\n"; var_dump($_GET);
echo "\n".'$_POST'."\n"; var_dump($_POST);
echo "\n".'$_FILES'."\n"; var_dump($_FILES);
echo "\n".'$_REQUEST'."\n"; var_dump($_REQUEST);
echo "\n".'$_SESSION'."\n"; var_dump($_SESSION);
echo "\n".'$_ENV'."\n"; var_dump($_ENV);
echo "\n".'$_COOKIE'."\n"; var_dump($_COOKIE);
echo "\n".'$php_errormsg'."\n"; var_dump($php_errormsg);
echo "\n".'$HTTP_RAW_POST_DATA'."\n"; var_dump($HTTP_RAW_POST_DATA);
echo "\n".'$http_response_header'."\n"; var_dump($http_response_header);
echo "\n".'$argc'."\n"; var_dump($argc);
echo "\n".'$argv'."\n"; var_dump($argv);
echo '</pre>';

The only one to show PATH_INFO was $_SERVER:

var_dump($_SERVER); // ["PATH_INFO"]=> string(0) ""

But the value I set in nginx isn't taken into account.

Q1: Do I need to configure anything at nginx/php level for the fastcgi_param directive to take effect?

Q2: Is the fastcgi_param directive limited to a predefined list of parameters (e.g. I can set PATH_INFO but not custom variables of my own such as FOO)?

Q3: If yes to Q2: is there a way to pass custom parameters to PHP from Nginx?


Solution 1:

You can pass additional parameters via fastcgi_param directive. I'm sure because I used this funcionality.

My exemplary nginx config

server {
    server_name localhost;
    include     conf/defaults.conf;
    root        /var/www;

    location    ~* "\.php$" {
        fastcgi_param CRS "crs";
        include conf/fastcgi-php.conf;
    }                                           
}

And part of phpinfo() output:

...
_SERVER["USER"] fcgi
_SERVER["HOME"] /dev/null
_SERVER["FCGI_ROLE"]    RESPONDER
_SERVER["REMOTE_USER"]  no value
_SERVER["CRS"]  crs
_SERVER["QUERY_STRING"] no value