Mono 2.11 on nginx using fastcgi-mono-server4 will not work

My sample MVC3 application is working fine on Mono 2.10.8.1 from badgerports.org (I use ubuntu).

After installing Mono 2.11.2 from source the same application would always return a 502 Bad Gateway just like in your case.

My nginx logs looked like this:

[error] 3384#0: *101 upstream sent unexpected FastCGI record: 3 while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8000"

After invoking fastcgi-mono-server4 with the printlog option set to true

sudo /opt/mono-2.11.2/bin/fastcgi-mono-server4 /printlog=True /applications=/:/var/www/mono/Mvc3Template /socket=tcp:127.0.0.1:9000

It would generate this stacktrace after visiting localhost:8000/

[2012-07-27 14:55:57Z] Notice  Beginning to receive records on connection.
[2012-07-27 14:55:58Z] Error   ERROR PROCESSING REQUEST: System.TypeInitializationException: An exception was thrown by the type initializer for Mono.WebServer.FastCgi.WorkerRequest

Server stack trace: 
  at Mono.WebServer.FastCgi.ApplicationHost.ProcessRequest (Mono.WebServer.FastCgi.Responder responder) [0x00001] in /home/r522/Desktop/mono-2.11.2/build/xsp-2.10.2/src/Mono.WebServer.FastCgi/ApplicationHost.cs:47 
  at (wrapper remoting-invoke-with-check) Mono.WebServer.FastCgi.ApplicationHost:ProcessRequest (Mono.WebServer.FastCgi.Responder)
  at (wrapper xdomain-dispatch) Mono.WebServer.FastCgi.ApplicationHost:ProcessRequest (object,byte[]&,byte[]&)

Exception rethrown at [0]: 
 ---> System.InvalidCastException: Cannot cast from source type to destination type.
  at System.Configuration.ConfigurationManager.get_AppSettings () [0x00000] in <filename unknown>:0 
  at Mono.WebServer.FastCgi.WorkerRequest..cctor () [0x0002c] in /home/r522/Desktop/mono-2.11.2/build/xsp-2.10.2/src/Mono.WebServer.FastCgi/WorkerRequest.cs:50 
  --- End of inner exception stack trace ---
  at (wrapper xdomain-invoke) Mono.WebServer.FastCgi.ApplicationHost:ProcessRequest (Mono.WebServer.FastCgi.Responder)
  at (wrapper remoting-invoke-with-check) Mono.WebServer.FastCgi.ApplicationHost:ProcessRequest (Mono.WebServer.FastCgi.Responder)
  at Mono.WebServer.FastCgi.Responder.Process () [0x00060] in /home/r522/Desktop/mono-2.11.2/build/xsp-2.10.2/src/Mono.WebServer.FastCgi/Responder.cs:90 
[2012-07-27 14:55:58Z] Notice  Finished receiving records on connection

Here is some of my configuration:

#/etc/nginx/sites-enabled/mono

 server {
         listen   8000;
         server_name  localhost;
         access_log   /var/log/nginx/mono.access.log;
         error_log    /var/log/nginx/mono.error.log;
         location / {
                 root /var/www/mono/Mvc3Template;
                 index index.html index.htm default.aspx Default.aspx;
                 fastcgi_index /Home/Index; # Points to HomeController Index Action
                 fastcgi_pass 127.0.0.1:9000;
                 include /etc/nginx/fastcgi_params;
         }
 }

-

# Directory structure
/var/www/mono
├── Mvc3Template
│   ├── bin
│   ├── Content
│   ├── Controllers
|   ... omitted rest
├── Mvc3Template.sln
├── Mvc3Template.suo
├── Mvc3Template.userprefs
├── packages

-

I found a solution to my problem here: bugzilla.xamarin.com/show_bug.cgi?id=2876 (What bugs me is that it's supposively been fixed on February 2012 - the mono I compiled was released in June afaik yet it didn't work out of the box )

I made a script based on the suggestions found there that fixes the mono installation, just point the PREFIX variable to your mono directory and run it:

#!/bin/bash

# Your mono directory
PREFIX=/opt/mono-2.11.2


FILES=('mod-mono-server4'
       'fastcgi-mono-server4'
       'xsp4')

cd $PREFIX/lib/mono/4.0

for file in "${FILES[@]}"
do
   cp "$file.exe" ../4.5
done


cd $PREFIX/bin

for file in "${FILES[@]}"
do
  sed -ie 's|mono/4.0|mono/4.5|g' $file
done

After this the application "works", but it's not quite there yet. It doesn't 502 any longer, and I get a nice stacktrace...

System.Web.Compilation.CompilationException
: at IKVM.Reflection.Reader.ModuleReader.ResolveType (Int32 metadataToken, IGenericContext context) [0x00000] in <filename unknown>:0

Description: Error compiling a resource required to service this request. Review your source file and modify it to fix this error.

Details: : at IKVM.Reflection.Reader.ModuleReader.ResolveType (Int32 metadataToken, IGenericContext context) [0x00000] in <filename unknown>:0

Error origin: Compiler

Error source file: ~/Global.asax
Exception stack trace:

So I still have things to figure out myself, but I hope something from this will be helpful to you.