无法使用 Ubuntu/Apache 从 .NET Core Web 应用加载静态文件

Unable to load static files from .NET Core web app using Ubuntu/Apache

我通过 Apache 2.4.41 在 Ubuntu 20.04 服务器上安装了 .NET Core 3.1 Web 应用程序 运行。当我访问该网址时,我得到了该网站的服务 - 链接工作正常,但我收到 404 文件未找到图像、样式和 JavaScript.

错误

来自 journalctl Kestrel 服务日志的示例:

Request starting HTTP/1.1 GET http://example.com/favicon.ico
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 0.3686ms 404

我的静态文件目录结构是默认的:

wwwroot
- css
-- site.css
- images
-- image1.jpg
-- image2.jpg
- js
-- site.js
- favicon.ico

我的配置方法是大部分默认:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseForwardedHeaders();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Shared/Error");
        app.UseHsts();
    }
    app.UseStaticFiles();
    app.UseHttpsRedirection();
    app.UseCookiePolicy();
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

“/etc/apache2/sites-enabled”中的网站配置是:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com
</VirtualHost>

<IfModule mod_ssl.c>
        <VirtualHost _default_:443>
                ProxyPreserveHost On
                ProxyPass / http://127.0.0.1:5000/
                ProxyPassReverse / http://127.0.0.1:5000/
                ServerAdmin webmaster@localhost
                ServerName example.com:443
                DocumentRoot /var/www/example.com
                ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
                CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
                SSLEngine on
                SSLCertificateFile      <certificate path>
                SSLCertificateKeyFile   <key path>
                <FilesMatch "\.(cgi|shtml|phtml|php)$">
                    SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                    SSLOptions +StdEnvVars
                </Directory>
        </VirtualHost>
</IfModule>

有什么想法吗?我注意到服务日志中显示的 GET URL 是 HTTP 而不是 HTTPS,这会影响什么吗?

TL;DR: 重新启动 kestrel 服务文件和 apache:

service apache2 restart
service <kestrel service filename>.service restart 

systemctl restart apache2
systemctl restart <kestrel service filename>.service restart

我遇到了完全相同的问题。这是我的解决方案:

如果在 Program.cs

中使用以下代码在开发中一切正常
public static IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
     .ConfigureWebHostDefaults(webBuilder =>
     {
        webBuilder.UseStartup<Startup>();
     });

然后我在 Ubuntu 实例上对 Kestrel 使用以下内容:

public static IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
       {
          webBuilder.UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseUrls("http://*:5000")
            .UseStartup<Startup>();
       });