15

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?

2 Answers 2

17

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
6
  • 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?
    – Max
    Nov 15, 2012 at 13:18
  • 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 Nov 15, 2012 at 14:06
  • 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
    Nov 15, 2012 at 14:29
  • Yes, it's working with the default ubuntu nginx package...
    – Max
    Nov 15, 2012 at 14:36
  • @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.
    – VBart
    Nov 15, 2012 at 20:21
1

You can modify the request by passing your custom variables in the request header, example:

proxy_set_header Variable-name-here Value-here;

And then you can read those values from the request header in your PHP script.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .