fastcgi server variables vs. apache

androo nginx-forum at nginx.us
Tue Apr 7 03:55:15 MSD 2009


First of all, thank you for Nginx. It is a breath of fresh air after Apache. And though it has taken some getting used to, I've discovered that I like the config file syntax better in Nginx.

Second, there are three server variables whose values are not the same under Nginx as under Apache: PATH_INFO, PATH_TRANSLATED, and SCRIPT_NAME. Obviously this makes trouble when trying to use applications designed for Apache. In addition, because the "fastcgi_param" directive cannot be used inside an "if" statement, it is impossible to write a general rule to set their values the same as under Apache. This is an issue when using URIs of the form "/directory/script.php/blah".

PATH_INFO: Under Apache, it doesn't exist unless using URIs of the above form; then it equals "/blah". Under Nginx, it never exists.

PATH_TRANSLATED: Under Apache, it doesn't exist unless using URIs of the same form; then it equals "/document/root/blah" (note: not "/document/root/directory/blah"). Under Nginx, it exists when using such URIs, but it equals "/document/root".

SCRIPT_NAME: Under Apache, when using URIs of the same form, it equals "/directory/script.php". Under Nginx, it equals "/directory/index.php/blah".

So far I haven't been able to find a general rule to fix this. What I would like to do is put something like this at the end of fastcgi_params:

if ($document_uri ~ "^/(.+)\.php/(.*)") {
    fastcgi_param PATH_INFO /$2;
    fastcgi_param PATH_TRANSLATED $document_root/$2;
    fastcgi_param SCRIPT_NAME /$1.php;
}

If someone called a URI that matched the pattern, these values would kick in; otherwise, the defaults would be used. But this can't be done because "fastcgi_param" isn't allowed inside "if". Next I tried the following at the end of fastcgi_params:

if ($document_uri ~ "^/(.+)\.php/(.*)") {
    set $apache_path_info /$2;
    set $apache_path_translated $document_root/$2;
    set $apache_script_name /$1.php;
}
fastcgi_param  PATH_INFO          $apache_path_info;
fastcgi_param  PATH_TRANSLATED    $apache_path_translated;
fastcgi_param  SCRIPT_NAME        $apache_script_name;

But this sets PATH_INFO, PATH_TRANSLATED, and (most problematically) SCRIPT_NAME to empty values even for URIs that don't match the pattern.

Has anyone found a way around this? I.e. a way of writing a general rule (i.e., one that is not hard-coded for specific URIs) that will normalize the three variables to their Apache values?

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,866,866#msg-866






More information about the nginx mailing list