首页 > 解决方案 > 使用 asp.net core 3.1 + linux nginx 找不到资源(静态文件问题?)

问题描述

我正在尝试在我的树莓派 4 上运行应用程序 asp.net core 3.1 MVC。该应用程序及其资源文件(img、css ...)在 IIS 调试模式(Windows)下运行良好。

我按照本教程设置了 linux 环境:tuto 该应用程序在 linux 中启动良好,但页面上没有资源。

所以我检查了关于静态文件的微软文档页面:静态文件 和其他教程。大多数情况下,他们解释了如何更改资源目录路径,但这不是我的问题。我只是想先看看它与 wwwroot 一起使用。

如您所见,我没有更改 startup.cs 中的任何内容:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

在 program.cs 中都没有:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

nginx配置文件内容:

server {
    listen        80 default_server;
    server_name   _;
    location / {
        proxy_pass         http://localhost:5000;
        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;
    }
}

请问您有什么想法,如何解决?感谢您阅读本文:)

标签: asp.net-corenginxnginx-configstatic-filesasp.net-core-staticfile

解决方案


问题已解决:静态文件应位于 /home/user 而不是我设置应用程序的位置


推荐阅读