.Net Core 2.2 App Returning 404 for every call on Linux AWS Instance

Viewed 365

I setup a .Net Core application on an AWS EC2 instance and can't seem to get my calls to succeed. Specifically, I find that routes that work just fine on my localhost when I run locally do not work on this Linux box. I followed some instructions from Microsoft to setup Kestrel with an nginx reverse proxy.

Relevent details:

I have a working route that functions on my localhost:

[HttpGet("test")]
[AllowAnonymous]
public ActionResult Test()
{
    return Ok("WORKED");
}

On my Linux box, I start dotnet (and have verified nginx is running). I do dotnet MyApp.dll and it says it's listening.

I now make a Postman call to http://api.mydomainhere.com

and in my command/terminal where I have dotnet running, I see the request come in:

[ec2-user@xxxxxx MyApp]$ dotnet MyApp.dll
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using '/home/ec2-user/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Production
Content root path: /home/ec2-user/MyApp
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://api.mydomainhere.com/api/users/Test application/json 119
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 67.8504ms 404 
info: Microsoft.AspNetCore.Server.Kestrel[32]
      Connection id "0HM0NUJ8Q68OU", Request id "0HM0NUJ8Q68OU:00000001": the application completed without reading the entire request body.

My nginx setup is:

server {
    listen        80;
    server_name   _;
    location / {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

Despite this, when I call /api/users/test, it 404's me, and if I run the same test locally, it works. Is there a misconfiguration in my app settings in .Net or anything like that?

If I try a curl command from the Linux instance I also get a 404, which means I can't even hit the .Net app right?

ec2-user@xxxx ~]$ curl -v -H "Content-Type: application/json" http://localhost:5000/api/users/test

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET /api/users/test HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Type: application/json
> 
< HTTP/1.1 404 Not Found
< Date: Wed, 24 Jun 2020 04:22:41 GMT
< Server: Kestrel
< Content-Length: 0
< 
* Connection #0 to host localhost left intact

Any suggestions or areas to start looking at?

Thank you!

1 Answers

For those who as despaired as was me, I composed small checklist, which worked for me (from this question):

  1. Comment try_files $uri $uri/ =404; line in your nginx host configuraion in

sudo nano /etc/nginx/sites-available/<your-site>

or/and

sudo nano /etc/nginx/sites-available/default

as it's testing if a certain url exists on the file system and if not return 404.

  1. Also I commented all content in sudo nano /etc/nginx/sites-available/default file to be sure that nothing excess prevents the api from working (ONLY if your api configuration is in separate file like /etc/nginx/sites-available/<your-site>)

Then restart nginx: sudo systemctl restart nginx && systemctl status nginx

  1. In your dotnet project, check that redirect middleware is only used in production environment or try to comment for all environments to detect if this is the problem (worked for my case)
if (!app.Environment.IsDevelopment())
{
    app.UseHttpsRedirection(); //leave like this or try to comment it just for checking
}
  1. Also in your dotnet project, check if you have your api path setting. Something like:
app.UsePathBase(apiBasePath);

Make sure, that this path is the same as location setting in files from step one. Of course, don't forget to upload your project on server if changes happened and restart your api service:

sudo systemctl restart your-kestrel-api.service

Related