source code bean

20 Mar, 2012

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

Posted by: Peter In: ASP.NET|Mono|Web

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 Responses to "Serving your ASP.NET MVC site on Nginx / fastcgi-mono-server4"

1 | Szczepan

September 11th, 2012 at 2:59 pm

Avatar

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

2 | Peter

November 21st, 2012 at 5:54 am

Avatar

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

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

June 30th, 2013 at 9:20 pm

Avatar

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

Comment Form

Adwords

About

Welcome to source code bean! On this site I post stuff that I encounter in my job and spare time. The content is mostly related to .NET development, but my interest in techonology is very broad, so often you will find posts on totally different subjects!