我有一个.NET Core 3.1 Web应用程序通过Apache 2.4.41在Ubuntu 20.04服务器上运行。当我访问网址时,我收到了网站 - 链接工作正常,但我收到图像、样式和 JavaScript 的 404 文件未找到错误。
日志中的示例 Kestrel 服务日志:
请求启动 HTTP/1.1 GET http://example.com/favicon.ico
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request 在 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>();
});