Search code examples
url-rewritingfastcgilighttpd

lighttpd fastcgi: how to restore stripped URL path elements?


I'm implementing a (rather complex) RESTful API.

I use Lighttpd as web server with two FastCGI backends:

  • a generic PHP handler for the bulk of requests
  • a specialized C++ handler for several other requests

my current fastcgi.conf looks like (simplified):

server.modules += ( "mod_fastcgi" )

fastcgi.server = ( ".php" => ( "PHP" => (
                 "bin-path" => "/usr/bin/php-cgi",
                 "socket" => "/tmp/php.socket",
                 "check-local" => "disable",
                 "bin-environment" => ( "PHP_FCGI_CHILDREN" => "0", "PHP_FCGI_MAX_REQUESTS" => "500" ),
                 "bin-copy-environment" => ( "PATH", "SHELL", "USER" ),
                 "max-procs" => 1,
                 "broken-scriptfilename" => "enable"
             )),
               "/api/groupA" => ( "Server" => (
                 "socket" => "/tmp/fcgi-C.socket",
                 "check-local" => "disable",
             )),
               "/api/groupB" => ( "Server" => (
                 "socket" => "/tmp/fcgi-C.socket",
                 "check-local" => "disable",
             )),
               "/api/groupC" => ( "Server" => (
                 "socket" => "/tmp/fcgi-C.socket",
                 "check-local" => "disable",
             )),
               "/api/" => ( "PHP" => (
                 "bin-path" => "/usr/bin/php-cgi",
                 "socket" => "/tmp/php-P.socket",
                 "check-local" => "disable",
                 "bin-environment" => ( "PHP_FCGI_CHILDREN" => "0", "PHP_FCGI_MAX_REQUESTS" => "500" ),
                 "bin-copy-environment" => ( "PATH", "SHELL", "USER" ),
                 "max-procs" => 1,
                 "broken-scriptfilename" => "enable"
             ))
             )

This means I filter prefixes intended for C++ handling and default the rest to PHP.

PHP part works as expected.

Problem here is C++ handler receives a truncated request path with "/group(A|B|C)" stripped away (everybody gets the path without the leading "/api", but that's OK).

How can I restore the full path?

Alternatively: Is there (or can I produce) some variable/header/whatever I can test in C++ to get the "missing part"?

I am fully prepared to cope with redundant information (e.g.: path restored to the full "/api/..."), but I need the information that has been dropped.

Only thing that comes to mind would be to differentiate the sockets and to have a filter restoring the information based on actual incoming socket but that seems overcomplex and very brittle; I'm hoping for a simpler solution, possibly involving URL-rewrite (I'm not familiar with Lighttpd configuration intricacies, I'm more of a C++/PHP/java coder, so I hope I overlooked something obvious ;) )


Solution

  • The original request from the client can be found in REQUEST_URI.

    mod_fastcgi also has config options to allow a limited set of URL mangling for broken PHP. https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModFastCGI