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

时间:2017-05-04 11:03:41

标签: linux nginx asp.net-core asp.net-core-mvc asp.net-core-1.1

我已经创建了一个ASP.NET Core MVC应用程序并将其部署到Linux服务器中。当我去sitename.com时,浏览器显示主页/索引页面没有任何问题。

但是当我尝试去sitename.com/Home/Index或其他控制器(如sitename.com/Admin/Login时,nginx会抛出404 Not Found错误。应该是什么问题?

这是我的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?}");
        });
    }

以下是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;
   }

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 {

    }

2 个答案:

答案 0 :(得分:7)

删除try_files $uri $uri/ =404;,因为它正在测试文件系统上是否存在某个网址,如果没有返回404。

/Home/Index是一个路由,它不会映射到现有文件而是映射到控制器操作,因此会出现404错误。

答案 1 :(得分:0)

帮助某人在Google上搜索

我得到404,但我意识到ASP Net仅按名称接受1台服务器

示例不可能

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;
    }
}

示例确定:

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;
    }
}