How to use fastcgi_index?

Igor Sysoev is at rambler-co.ru
Thu Nov 6 13:29:45 MSK 2008


On Thu, Nov 06, 2008 at 09:51:42AM +0100, Michael Schmarck wrote:

> Hi.
> 
> In my nginx.conf (of nginx 0.6.31, part of Sun's CoolStack 1.3.1), I
> have:
> 
>         location ~ \.php$ {
>             root           /data/www;
>             fastcgi_pass   127.0.0.1:65505;
>             fastcgi_index  index.php;
>             fastcgi_param  SCRIPT_FILENAME  /data/www$fastcgi_script_name;
>             include        fastcgi_params;
>         }
> 
> In /data/www, I have a "pi" directory, containing:
> 
> --($ /opt/coolstack/nginx/conf)-- ls -l /data/www/pi
> total 1
> -rw-r--r--    1 webservd webservd       26 Nov  4 13:49 index.php
> --($ /opt/coolstack/nginx/conf)-- cat /data/www/pi/index.php 
> <?php
> phpinfo();
> 
> # EOF #
> 
> When I now call http://$server/pi/, I'm shown the directory
> index of the "pi" directory (because I have "autoindex on;").
> 
> What would be the proper way to have nginx "show" the
> index.php, if there's one and if http://$server/$dir/ is
> invoked? (I don't want to see the source code of index.php,
> I want to have that interpreted by PHP.)
> 
> Do I have to add it to the "index" direcetive, like so?
> 
>         location / {
>             root   /data/www;
>             index  default.htm index.html index.htm index.php;
>         }
> 
> This works, but is that the way it's supposed to be done?
> 
> But also adding "fastcgi_index  index.php;" to the 
> "location / { ??? }" statement doesn't make nginx call
> PHP with the index.php.
> 
> Hm.
> 
> I don't think that I understand what fastcgi_index 
> should be doing.
> 
> I now changed the nginx.conf to contain:
> 
> http {
>   # ???
>   server {
>     # ???
>         location / {
>             root   /data/www;
>             index  default.htm index.html index.htm index.php;
>         }
> 
>         location ~ \.php$ {
>             root           /data/www;
>             fastcgi_pass   127.0.0.1:65505;
>             fastcgi_index  SCHNISMindex.php;
>             fastcgi_param  SCRIPT_FILENAME  /data/www$fastcgi_script_name;
>             include        fastcgi_params;
>         }
>   }
> }
> 
> Effect: When http://$srv/$dir/ is invoked by the browser,
> whatever is in index.php is feed to PHP and shown to
> the browser.
> 
> Nice ??? But why is that so? I would have thought, that because
> of the (in this case purposely) mis-configured fastcgi_index
> parameter, it shouldn't have worked. I would have
> thought, that a "SCHNISMindex.php" would have been
> passed to PHP. Why is that not so?
> 
> What does fastcgi_index do? I have read (and obviously not
> understood) http://wiki.codemongers.com/NginxHttpFcgiModule#fastcgi_index
> of course :)

The index directive looks up local filesystem for index files,
so in following configuration

     location / {
         root   /data/www;
         index  index.php;
     }

     location ~ \.php$ {
         fastcgi ...
     }

the index sees /data/www/index.php and does internal redirect to /index.php,
so the request goes to "location ~ \.php$" and is passed to fastcgi.

However, if fastcgi server is remote one, and nginx has no access to its
filesystem, then index has no idea about remote index files. The fastcgi_index
should help here: it simply adds its value to any /dir/, i.e. it maps
/dir/ to /dir/index.php.

     location ~ (\.php|/)$ {
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  /data/www$fastcgi_script_name;
     }


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list