How to pass custom parameters to PHP from Nginx?

26,836

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
Share:
26,836

Related videos on Youtube

Max
Author by

Max

Analytics consultant available for hire. More info: https://maxcorbeau.com

Updated on September 18, 2022

Comments

  • Max
    Max over 1 year

    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?

  • Max
    Max over 11 years
    I have fastcgi.conf and fastcgi_params but not fastcgi-php.conf: where did you get it from? Do you mind sharing it via pastebin?
  • Krzysztof Księżyk
    Krzysztof Księżyk over 11 years
    It's my specific config but there's no magic: fastcgi_pass 127.0.0.1:1028; fastcgi_index index.php; include conf/fastcgi.conf; and conf/fastcgi.conf contains rest of fastcgi required params and settings
  • Max
    Max over 11 years
    The only thing I could think of is that I forgot to include something in the conf when compiling nginx. On the other hand, I've forgotten to include things when compiling nginx in the past, but nginx would refuse to start saying it didn't know about some directives I was using: here nothing, no complaints, but yet that fastcgi_param isn't working :( I'll give a try to the original ubuntu nginx package and see how it goes.
  • Max
    Max over 11 years
    Yes, it's working with the default ubuntu nginx package...
  • VBart
    VBart over 11 years
    @user64204 this definitely was a problem with your configuration, and ubuntu package just overwritten some of your config files. But since you did not show to us your full configuration, there's no way to determine what the problem actually was.
  • Max
    Max over 11 years
    I fixed it. Nothing wrong with my compiled Nginx, just another stupid mistake (there was another fastcgi location which I was convinced was not being applied but actually was, and that one was not using fastcgi_param, hence parameters not being set... :(