asp.net core on linux with nginx routing doesn't work
Asked Answered
B

2

8

I've created an ASP.NET Core MVC application and deployed it into Linux server. When I go to sitename.com browser shows up the Home/Index page without any problem.

But when I try to go sitename.com/Home/Index or another controller like sitename.com/Admin/Login nginx throws a 404 Not Found error. What should be the problem?

Here is my Startup.cs/Configure method.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseSession();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

Here is my website config from sites-available folder

server {
   listen 80 default_server;
   listen [::]:80 default_server ipv6only=on;

   root /var/www/sitename.com;
   index index.html index.htm;

   server_name sitename.com www.sitename.com;

   location / {
      try_files $uri $uri/ =404;
      proxy_pass http://127.0.0.1:5000;
   }

and nginx.conf

    user www-data;
    worker_processes 4;
    pid /run/nginx.pid;

    events {
        worker_connections 768;
    }

    http {

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }

    mail {

    }
Bespread answered 4/5, 2017 at 11:3 Comment(7)
Do these requests work if you send them to Kestrel directly?Fabiolafabiolas
When I run these lines on bash I'get the correct html responses. curl http://localhost:5000/Home/Index or curl http://localhost:5000/Admin/Login @AlexeyAndrushkevichSsw
Then it worth sharing your nginx.conf file to see if it's configured properly.Fabiolafabiolas
I've added my config files to my question @AlexeyAndrushkevichSsw
Did you try to remove try_files $uri $uri/ =404;? For me this looks like it's testing if a certain url exists and if not return 404. But /Home/Index is a route, which do not map to an existing file but to controller actionJointress
Yes. It worked. Thanks a lot @JointressSsw
Hi, I stuck on the same 404 issue for a week. I removed above statement but the issue still persists. Could anyone please help me to resolve 404.Tamathatamaulipas
J
15

Remove try_files $uri $uri/ =404; as it's testing if a certain url exists on the file system and if not return 404.

But /Home/Index is a route, which do not map to an existing file but to controller action, hence you get the 404 error.

Jointress answered 4/5, 2017 at 15:19 Comment(0)
T
1

To help someone searching on Google

I was getting 404, but I realized that ASP Net only accepts 1 server by name

Example NOT POSSIBLE:

server{
    listen 80;
    listen [::]:80;

    server_name example.com;

    location /asp_app_ONE {
        proxy_pass     http://0.0.0.0:3001;
        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;
    }
    location /asp_app_TWO{
        proxy_pass         http://0.0.0.0:3002;
        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;
    }
}

Example OK:

server{
    listen 80;
    listen [::]:80;

    server_name appONE.example.com;

    location / {
        proxy_pass     http://0.0.0.0:3001;
        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;
    }
}
server{
    listen 80;
    listen [::]:80;

    server_name appTWO.example.com;

    location / {
        proxy_pass     http://0.0.0.0:3002;
        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;
    }
}
Twinscrew answered 30/7, 2018 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.