Tidbits on software development, technology, and other geeky stuff.

Nginx with ASP.NET Page Methods

I recently spent some time moving an ASP.NET 4.0 website from Apache (mod_mono) over to Nginx with FastCGI to take advantage of Nginx’s killer performance and low memory footprint.

After following the FastCGI Nginx guide on the mono website I was mostly up and running. However, my app has some Page Methods I use for AJAX calls via jQuery and they were not working properly. I was getting 405 or 500 responses from Nginx when jQuery posted to the these methods. I came across this related article: lighttpd + Mono Asp.Net; albeit related to lighttpd it still deals with FastCGI issues. It shed light of the fact that mono’s fastcgi-mono-server is expecting parameters to be passed in a certain format from Nginx to properly handle Page Methods. The default fastcgi config for Nginx does not play nicely with ASP.NET Page Methods.

Then I came across this article: ASP.NET Ajax Error 405 on Nginx, which got me closer to the solution. Although I thought this would be my fix it didn’t work when I implemented the example config and actually caused a different server error. After some trial and error I found that the fastcgi_param PATH_TRANSLATED *config line should *not be included. I’m not sure why including it worked for the author of this article but perhaps he is using a different version of mono. For the record, I am using mono 2.10.8 with fastcgi-mono-server4 (i.e. .NET 4.0). In the end, the following config worked for me:

location ~\.aspx(.*) {​
  include fastcgi_params;​
  fastcgi_split_path_info ^(.+.aspx)(.*)$;​
  fastcgi_param PATH_INFO $fastcgi_path_info;​
  fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;​
  fastcgi_index Default.aspx;​

If you are using ASMX Page Methods you simply need to copy this config and change all the aspx references to asmx and it should work.

Discuss on Twitter