Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 {

    }
like image 527
Erdinç Özdemir Avatar asked May 04 '17 11:05

Erdinç Özdemir


People also ask

Does ASP.NET Core work on Linux?

With ASP.NET Core, you can build and run . NET applications not only on Windows but also macOS and Linux.

How do I enable routing in .NET Core?

Inside the ConfigureRoute method, you can configure your routes; you can see that this method has to take a parameter of type IRouteBuilder. The goal of routing is to describe the rules that ASP.NET Core MVC will use to process an HTTP request and find a controller that can respond to that request.

Can Kestrel run on Linux?

Net applications run on Kestrel servers and we run Apache or Nginx server in Linux environments, which acts as a proxy server and handles the traffic from outside the machine and redirects it to the Kestrel server so we will have Apache or Nginx server as the middle layer.


2 Answers

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.

like image 181
Tseng Avatar answered Sep 29 '22 14:09

Tseng


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;
    }
}
like image 22
WiseTap Avatar answered Sep 29 '22 15:09

WiseTap