Serving your ASP.NET MVC site on Nginx / fastcgi-mono-server4

In my previous post I showed you how to compile and install mono and get your MVC site up running using the development web server xsp4. The next step is to serve your site using a real web server, my choice is nginx.

First, we need a configuration file for the nginx site. If you haven’t already got nginx installed, install it (sudo apt-get install nginx).

/etc/nginx/sites-enabled/mvc:

server {
    listen   80;
    server_name mvctest.sourcecodebean.com;
    root  /home/peter/MonoMvcDeploy/;

    location / {
      root /home/peter/MonoMvcDeploy/;
      index index.html index.htm default.aspx Default.aspx;
      fastcgi_index /Home;
      fastcgi_pass 127.0.0.1:8000;
      include /etc/nginx/fastcgi_params;
    }
}

Type “sudo service nginx reload” to reload the configuration. Browsing to the site should give you a fastcgi error since there is no fastcgi server listening on 127.0.0.1:8000 yet. So far this is a good thing and shows that your configuration is correct.

Next we create a start script for our fastcgi server.

/home/peter/startmvc:

#!/bin/bash
source ./monoenv.sh
fastcgi-mono-server4 /applications=/:/home/peter/MonoMvcDeploy/ /socket=tcp:127.0.0.1:8000

This is pretty straigt forward, the only thing that might be a bit confusing is this part “/:/home/peter/MonoMvcDeploy/”. This means that we want to map the URL / to the application in /home/peter/MonoMvcDeploy/. This allows us to run several applications in the same fastcgi server on different URLs.

Now try running the script and browse to your site, our sample MVC site should appear!

The last step is to create a upstart script to have our application start automatically at system boot

/etc/init/mvc.conf:

#!upstart
description "sionmailer web"
author      "peter"

start on startup
stop on shutdown

respawn

script
    echo $$ > /var/run/mvc.pid
    exec sudo -H -u peter /home/peter/startmvc 2>&1
end script

pre-start script
    echo "[`date`] (sys) Starting" >> /var/log/mvc.sys.log
end script

pre-stop script
    rm /var/run/mvc.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/mvc.sys.log
end script

Save the config file and start the site: service mvc start.

All done, your site is being served by nginx/fastcgi-mono-server4!

3 comments on “Serving your ASP.NET MVC site on Nginx / fastcgi-mono-server4

  1. Hi.
    I’m getting Bad Gatewayt error when using fastcgi-mono-server4 and nginx? Site is working fine under xsp4.

  2. Peter Post author

    Sounds like you haven’t setup the proxy correctly, are you using the right ports?

  3. Pingback: ServiceStack API with FastCGI Mono Server and Nginx hosted on DigitalOcean | Joe's Code

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>