Problems with fastcgi php migration

Carlos carloscm at
Sun Mar 9 12:16:14 MSK 2008

I'm using CodeIgniter (a PHP framework) with "pretty URLs" which comes
close to what you want to achieve. This is what my server section
looks like:

server {
                include conf/php-fcgi.conf;

                location / {
                        index           index.php index.html;
                        if (-f $request_filename) {
                        if (!-f $request_filename) {
                                rewrite ^/(.*)$ /index.php/$1 last;

and php-fcgi.conf:

        location ~ .*\.php.*$ {
                fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
                fastcgi_param  SERVER_SOFTWARE    nginx;
                fastcgi_param  QUERY_STRING       $query_string;
                fastcgi_param  REQUEST_METHOD     $request_method;
                fastcgi_param  CONTENT_TYPE       $content_type;
                fastcgi_param  CONTENT_LENGTH     $content_length;
                fastcgi_param  SCRIPT_FILENAME
                fastcgi_param  PATH_TRANSLATED
                fastcgi_param  REQUEST_URI        $request_uri;
                fastcgi_param  DOCUMENT_URI       $document_uri;
                fastcgi_param  DOCUMENT_ROOT      $document_root;
                fastcgi_param  SERVER_PROTOCOL    $server_protocol;
                fastcgi_param  REMOTE_ADDR        $remote_addr;
                fastcgi_param  REMOTE_PORT        $remote_port;
                fastcgi_param  SERVER_ADDR        $server_addr;
                fastcgi_param  SERVER_PORT        $server_port;
                fastcgi_param  SERVER_NAME        $server_name;
                fastcgi_index  index.php;

This allows me to directly refer a php file in any location and get it
executed by fastcgi, and at the same time any non-existing file (like
/galleries/1/2/3 in your example) is matched by "if (!-f
$request_filename)" and gets passed to fastcgi as
"/index.php/galleries/1/2/3". Static content is handled directly by
nginx. I think you could use a similar setup with several location
entries that reference a different index.(shtml|php) in the second

On Sun, Mar 9, 2008 at 8:58 AM, Ian M. Evans <ianevans at> wrote:
> Philip Ratzsch wrote:
>  > [Caveat - in my limited experience, the rewrite engine in Nginx uses a very similar syntax to Apache's mod_rewrite module, so I'm hoping this is close enough that someone with more knowledge than I can correct any mistakes.  This does not take proxying into account, and simply serves as a logic example.
>   > These are not the droids you're looking for.]
>  Killed myself on the droids bit...also daylight saving kicked in and 2AM
>  just became 3AM, so more coffee.
>  If I get what you're saying, you're suggesting creating a new .php file
>  that, rather than looking for a PATH_INFO that may or may not be mangled
>  through the FCGI process instead gets translated into ?variable=whatever
>  in a rewrite from the old extensionless URI. Visitor still sees
>  /galleries/123 but nginx translates it into galleries.php?event=123
>  I wouldn't need numeric length specifics as they can be any lenght and
>  can be there or not.
>  e.g. /galleries gives the user a main menu
>  /galleries/[num] gives them an event (like a film festival)
>  /galleries/[num]/[num] gives them a subevent listing (like a premiere
>  photo listing at a film festival)
>  and /galleries/[num]/[num]/[num] gives them a specific photo.
>  Fun and games. As I said people, regex is below a weakness for me so if
>  there are any suggestions, I'm open.
>  At least the site is, and has been, fully functional in the Apache
>  backend format...would love to go fully nginx soon though.

More information about the nginx mailing list