我正在尝试使用Mono / Nginx / Fastcgi将C#asp.net项目移植到Ubuntu,并且在一些API请求中,我看到Nginx在我期待200时返回404 http响应 . 该应用程序正常工作Windows服务器上的IIS,所以我真的很感激一些帮助调试这种不一致的行为 .
我正在使用:每晚单声道4.5,nginx 1.9,fastcgi-mono-server4和Net4.5项目 .
我的代码使用 xbuild 'mysolution'.sln
编译得很好,然后运行
fastcgi-mono-server4 /applications=/:/var/www/html/{folder containing my /bin} /socket=tcp:127.0.0.1:9000 /printlog=True /verbose=True &
打开我的mono fastcgi服务器,然后打开我的nginx服务器 service nginx restart
但在运行时,前端请求命中我的任何API,包括以下API
[HttpGet]
[Authorize]
[Route("UserRoles")]
public IHttpActionResult GetUserRoles(string userId)
{
var userRoles = _repo.GetUserRoles(userId);
return Ok(userRoles);
}
它有时会返回404错误,而不是200次成功 .
通过Charles跟踪我的http请求/响应显示nginx正在发送404未找到的响应,所以我认为问题出在API上 .
但是,添加带有添加的日志记录的自定义MessageHandler会显示API正在接收请求,处理它,然后返回200个成功的响应 . 看起来它位于MessageHandler和nginx服务器之间,响应消息从200找不到404 .
由于在Charles中重放相同的API请求会返回200响应,因此在多个请求期间Mono中的某处似乎存在一些资源约束问题 .
我尝试在nginx和fastcgi中打开调试以获得更有用的日志记录:在nginx.conf中使用 error_log /var/log/nginx/error.log debug;
并在运行Fastcgi时使用 loglevels=All
但没有看到任何有用的东西 .
nginx configs / etc / nginx / sites-enabled / default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/RM.WebAPI;
error_log /var/log/nginx/error.log debug;
rewrite_log on;
location / {
try_files $uri $uri/ @proxy;
}
location @proxy {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $http_origin always;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
return 204;
}
include /etc/nginx/fastcgi_params;
fastcgi_next_upstream http_404; #invalid_header http_500;
fastcgi_keep_conn off;
autoindex on;
fastcgi_pass 127.0.0.1:9000;
}
}
我的自定义MessageHandler MonoMessageHandler.cs
public class MonoMessageHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = base.SendAsync(request, cancellationToken);
Console.WriteLine("Returning response {0}:{3} for request {1}\n Content:{2}", response.Result.ToString(), request.RequestUri.ToString(), response.Result.Content.ToString(), response.Result.IsSuccessStatusCode);
return response;
}
}
更新:我添加自定义异常处理程序后,我开始在MessageHandler中收到以下错误
System.Runtime.ExceptionServices.ExceptionDispatchInfo
Response:System.Web.Http.Results.ExceptionResult
Exception:System.NullReferenceException: Object reference not set to an instance of an object
at RM.WebAPI.MonoPatchingDelegatingHandler.SendAsync (System.Net.Http.HttpRequestMessage request, CancellationToken cancellationToken) <0x40f18640 + 0x0019d> in <filename unknown>:0
at System.Net.Http.DelegatingHandler.SendAsync (System.Net.Http.HttpRequestMessage request, CancellationToken cancellationToken) <0x40f18600 + 0x0002b> in <filename unknown>:0
at System.Web.Http.HttpServer.<>n__FabricatedMethod9 (System.Net.Http.HttpRequestMessage , CancellationToken ) <0x40f185d0 + 0x0001b> in <filename unknown>:0
at System.Web.Http.HttpServer+<SendAsync>d__0.MoveNext () <0x40efbbb0 + 0x0042f> in <filename unknown>:0