ASP.NET Ajax Error 405 on Nginx


Yesterday I had a problem setting up an ASP.NET application running on a Nginx webserver using the fastcgi-mono-server. Everything worked fine following the Nginx guide in the Mono wiki until I noticed that something was wrong with the page methods. The mono-server just threw an Error 405 back to me instead of the requested data. After reading a tip in Florent’s Blog it was clear that the request from the Nginx server sent to the mono-server did not contain the correct path information.

To investigate that further I compared the actual HttpRequest object created by ASP.NET by adding the following code into the Application_BeginRequest eventhandler.

        protected virtual void Application_BeginRequest(Object sender, EventArgs e)
        {
            if (Request.FilePath.Contains("WebService"))
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine("Request.filepath: " + Request.FilePath);
                builder.AppendLine("Request.Path: " + Request.Path);
                builder.AppendLine("Request.Rawurl: " + Request.RawUrl);
                builder.AppendLine("Request.type: " + Request.RequestType);
                builder.AppendLine("Reqest.url: " + Request.Url);
                builder.AppendLine("PATH_INFO: " + Request.Params["PATH_INFO"]);

                Response.Write(builder.ToString());
                Response.End();
            }
        }

Request using Apache + mod_mono

image

Request using Nginx + fastcgi

image

The problem is now Nginx thinks without any further configuration, that the “FileName.aspx/MethodName” is an existing resource in the file system. The solution is already well documented in the Nginx Wiki for php. The requested url needs to be divided into the actual script path and its parameters. Just integrate the following lines into your virtual host definition.

location ~\.aspx(.*) {
                 fastcgi_split_path_info ^(.+\.aspx)(.*)$;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 fastcgi_param PATH_INFO $fastcgi_path_info;
                 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                 include /etc/nginx/fastcgi_params;
                 fastcgi_index Default.aspx;
                 fastcgi_pass 127.0.0.1:9000;
         }

After this fix the page methods are working fine with Nginx. This solution should also work for normal web services (.asmx instead of .aspx).
Fixed (Request from Nginx + fastcgi)

image

, ,

  1. Bisher keine Kommentare.
(wird nicht veröffentlicht)