How to pass custom parameters to PHP from Nginx?
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
Related videos on Youtube
Max
Analytics consultant available for hire. More info: https://maxcorbeau.com
Updated on September 18, 2022Comments
-
Max over 1 year
I'm using
Nginx 1.2.4
in combination withPHP-FPM 5.4.8
with afastcgi
pass and trying to pass custom parameters toPHP
. Here are the options I have found so far:using the
env
directive to set an environment variable innginx
and fetch it withgetenv()
or$_ENV
fromPHP
. The problem is thatenv
only operates inmain
context whereas I need to set the parameter inserver
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 thefastcgi_param
directive to take effect?Q2: Is the
fastcgi_param
directive limited to a predefined list of parameters (e.g. I can setPATH_INFO
but not custom variables of my own such asFOO
)?Q3: If yes to Q2: is there a way to pass custom parameters to PHP from Nginx?
-
Max over 11 yearsI have
fastcgi.conf
andfastcgi_params
but notfastcgi-php.conf
: where did you get it from? Do you mind sharing it via pastebin? -
Krzysztof Księżyk over 11 yearsIt'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 over 11 yearsThe 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 compilingnginx
in the past, butnginx
would refuse to start saying it didn't know about some directives I was using: here nothing, no complaints, but yet thatfastcgi_param
isn't working :( I'll give a try to the originalubuntu nginx
package and see how it goes. -
Max over 11 yearsYes, it's working with the default
ubuntu nginx
package... -
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 over 11 yearsI 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... :(