What's fastcgi_param scope

Maxim Dounin mdounin at mdounin.ru
Thu Mar 24 12:24:16 MSK 2011


On Wed, Mar 23, 2011 at 04:34:38PM -0700, Jakub Zalas wrote:

> Hi,
> I'm experiencing odd behavior of fastcgi_param.
> Based on the URL I'd like to pass additional parameters to PHP.
> For example:
> location ~ "^/foo/bar$" {

Just a side note: use "location = /foo/bar" instead.

>   # this is not passed to fastcgi and not acesible via $_SERVER
>   fastcgi_param _ROUTING__route foo;
>   fastcgi_index app.php;

Just a side note: fastcgi_index doesn't make sense here at all.

>   rewrite ^(.*) /app.php last;
> }

As soon as you did internal redirect (with rewrite) - 
configuration in this location no longer applies.  Instead 
configuration in destination location will be used.

> # standard PHP stuff
> location ~ \.php {
>     set  $script     $uri;
>     set  $path_info  "";
>     if ($uri ~ "^(.+\.php)(/.*)") {
>       set  $script     $1;
>       set  $path_info  $2;
>     }
>     fastcgi_pass;
>     include /etc/nginx/fastcgi_params;
>     fastcgi_param  SCRIPT_FILENAME  /var/www/$host/web$script;
>     fastcgi_param  PATH_INFO        $path_info;
>     fastcgi_param  SCRIPT_NAME $script;
> }

... and this one has no "fastcgi_param _ROUTING__route" set.

> I also tried nesting location but the result is the same.
> fastcgi_param defined in nested location block is not passed to PHP.
> Could anyone explain me why it works this way?

See above.  Solution is to write explicitly what you want to 
happen instead of using internal redirects, i.e. something like 

    location = /foo/bar {
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/$host/web/app.php;
        fastcgi_param _ROUTING__route foo;

Maxim Dounin

p.s. The only thing which survives internal redirect is variables.  
Theoretically you may rewrite your config to set some variables 
before rewrite and then use appropriate fastcgi_param in .php 
location.  But it's not going to scale well and not recommended.

More information about the nginx mailing list