Mono + SCGI

Over a year ago I started a SCGI daemon for Mono (scgi-mono-server). For those who don’t know, the “SCGI protocol is a replacement for the Common Gateway Interface (CGI) protocol. It is a standard for applications to interface with HTTP servers. It is similar to FastCGI but is designed to be easier to implement”.I stopped all development shortly after I started because lighttpd (the web server I cared about) required a very small patch to be useful. Having supplied the patch I waited around for it to be rolled into a release (1.4.20) and then waited for it to be picked up by Ubuntu, but it still hasn’t. So what brought it back to life? A silly bug: https://bugs.launchpad.net/ubuntu/+source/mod-mono/+bug/227781.

Some of our servers need both PHP5 & Mono to run side-by-side so one of our guys listed the options:

  • Pull mono & mod-mono 1.9.1 back-ports from a PPA (http://ppa.launchpad.net/directhex/ppa/ubuntu)
  • Patch and rebuild mod-mono 1.2.5 ourselves
  • Switch to FastCGI for PHP5, so we can use apache2-mpm-worker for mod-mono-server
  • Switch from Apache to lighttpd (and rebuild with our patch)
  • Switch from Apache + mod-mono to Apache + FastCGI or SCGI (requires patching lighttpd)
  • Switch from Apache + mod-mono to Apache + mod_proxy for XSP

Given our growing dislike for Apache & appreciation for lighttpd we decided on a phased approach:

  • Switch PHP5 to Apache + FastCGI
  • Switch Mono to Apache + SCGI
  • Run for a while (make sure everything works well)
  • Switch from Apache to lighttpd

We choose SCGI for Mono over FastCGI because:

  • FastCGI requires a lot of chatter between the client & server
  • Great incentive to finally give the server to Mono

With our approach in mind I started working on scgi-mono-server again, but with a new target – Apache. This shouldn’t have been very hard given that mod_scgi is provided by Quixote, but there were a few stumbling blocks. Apache, for whatever reason, has decided to break the SCGI specification (in my eyes). As stated in the Protocol: “[t]he format of the response is not specified” – meaning whatever the SCGI server sends back to the client (Apache, lighttpd, etc) should go back to the originator (web browser). Apache however, does not honor this.

The first line of any HTTP response (AFAIK) should be it’s Status Line:

HTTP/1.1 200 OK.

Apache’s mod_scgi requires that all lines until the body have a colon so that is can parse the header and update it’s internal data model. Therefore, our first line now has to be replaced by:

Status: 200 OK

Thankfully the Example section of the protocol showed me how to fix the issue. I’m tempted to patch Apache/mod_scgi but I’m a bit worried about how many SCGI servers require this broken code. So for now I’ve added a configuration option that can be put inside ASP.NET’s web.config to control the hack:

<appSettings>
	<add key="MonoServerApacheStatus" value="true" />
</appSettings>

I’ll be submitting the server back to Mono with some documentation after it goes through some solid developer testing on our side – so I hope someone enjoys!