asp.net linux 上带有 nginx 路由的核心不工作

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

我创建了一个 ASP.NET Core MVC 应用程序并将其部署到 Linux 服务器中。当我转到 sitename.com 时,浏览器会毫无问题地显示 Home/Index 页面。

但是当我尝试去 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 {

    }

删除 try_files $uri $uri/ =404;,因为它正在测试文件系统上是否存在某个 url,如果不存在,则 return 404.

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

帮助在 Google

上搜索的人

我收到 404,但我意识到 ASP 网络只接受 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;
    }
}