nscgi: ns_request_cgi and REQUEST_URI change

In the “scratch your own itch” department, I decided to get WordPress working under AOLserver. WordPress is written in PHP, and while AOLserver can load PHP as a module, it doesn’t play nice when you configure AOLserver with multiple virtual servers. The other option, running PHP as CGI using nscgi and php-cgi, does work for the most part. However, there were a few issues I ran into that required changes to nscgi, which I’ll outline below.

Adding REQUEST_URI to CGI env. variables

PHP expects an additional environment variable, REQUEST_URI, to be included on CGI requests. Of course, this isn’t part of the CGI/1.1 specification. Basically, PHP expects REQUEST_URI to contain the Request-URI (from the HTTP/1.1 specification, see section 5.1.2). So, I added this in.

Mapping URLs to CGI with ns_register_cgi

In order to map an URL to be handled by nscgi, you previously had to make the change in your config .tcl and restart AOLserver. One of the great things about AOLserver is the fact that you can modify some settings at runtime, without a restart. So, I looked at whether it would be possible to add additional mappings at runtime, and it appears that under the hood, nscgi also uses the Ns_RegisterRequest C API which is supposed to be safe to execute at runtime. So, I added a new Tcl command, ns_register_cgi, which is now part of nscgi. It is just a wrapper around CgiRegister, and thus takes the same argument.

For example, if you wanted to map all requests to “/foo” (including “/foo/bar“) to a PHP script “/var/www/myfoo.php“, you would use something like this:

foreach method {GET POST HEAD} {
    ns_register_cgi "$method /foo /var/www/myfoo.php"

This maps GET, POST and HEAD requests to URLs starting with “/foo” to the script specified.

Caveat: It is currently possible to load the nscgi module multiple times in the same AOLserver process, for each virtual server. It is also possible to load it multiple times for the same virtual server. I’m not sure why anyone would do that, but it’s possible. In that circumstance, the behavior of ns_register_cgi is undefined–as in, I haven’t tested it. It may or may not work correctly. If you run such a configuration, where nscgi is loaded multiple times in the same virtual server, please test this carefully and share your findings. Thanks.

Where’s the code?

I’ve commited the changes to SourceForge CVS, both in HEAD (rev 1.33, diff) and the aolserver_v40_bp branch (rev, diff).

Tags: , ,

Speak Your Mind