【问题标题】:Unable to load static files from .NET Core web app using Ubuntu/Apache无法使用 Ubuntu/Apache 从 .NET Core Web 应用程序加载静态文件
【发布时间】:2020-06-24 21:14:53
【问题描述】:

我有一个通过 Apache 2.4.41 在 Ubuntu 20.04 服务器上运行的 .NET Core 3.1 Web 应用程序。当我访问该网址时,我得到了该网站 - 链接工作正常,但我收到 404 file not found 图像、样式和 JavaScript 错误。

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

请求开始 HTTP/1.1 GET http://example.com/favicon.ico
信息:Microsoft.AspNetCore.Hosting.Diagnostics[2]
请求在 0.3686 毫秒 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,这有什么影响吗?

【问题讨论】:

  • HTTP 请求是由于 (proxypass, proxypassreverse) 的反向代理配置。不会造成404错误,请使用TraceLogLevel在Asp.Net Core应用中查看详情。

标签: apache ubuntu asp.net-core static-files kestrel-http-server


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多