简体   繁体   English

使用nginx路由的linux上的asp.net核心不起作用

[英]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. 我创建了一个ASP.NET Core MVC应用程序并将其部署到Linux服务器中。 When I go to sitename.com browser shows up the Home/Index page without any problem. 当我去sitename.com时,浏览器显示Home / Index页面没有任何问题。

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. 但是当我尝试去sitename.com/Home/Index或其他控制器如sitename.com/Admin/Login nginx会抛出404 Not Found错误。 What should be the problem? 应该是什么问题?

Here is my Startup.cs/Configure method. 这是我的Startup.cs/Configure方法。

    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 这是我的网站配置来自sites-available文件夹

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 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 {

    }

Remove try_files $uri $uri/ =404; 删除try_files $uri $uri/ =404; as it's testing if a certain url exists on the file system and if not return 404. 因为它正在测试文件系统上是否存在某个URL,如果没有返回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. /Home/Index是一个路由,它不映射到现有文件而是映射到控制器操作,因此您得到404错误。

To help someone searching on Google 帮助在Google上搜索的人

I was getting 404, but I realized that ASP Net only accepts 1 server by name 我得到404,但我意识到ASP Net只接受1台服务器的名字

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: 示例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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM