3

I have a .NET Core 3.1 web app running on an Ubuntu 20.04 server through Apache 2.4.41. When I visit the web address, I'm served the website - links work fine, but I'm getting 404 file not found errors for images, styles and JavaScript.

Example from journalctl Kestrel service log:

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

My static file directory structure is default:

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

My Configure method is mostly default:

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?}");
    });
}

Website's config in '/etc/apache2/sites-enabled' is:

<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>

Any ideas? I noticed the GET URL shown in the service log is HTTP rather than HTTPS, does that affect anything?

1
  • The HTTP request is due to reverse proxy config by (proxypass, proxypassreverse). It will not cause 404 error, please use Trace as LogLevel to see the details in the Asp.Net Core application. Commented Jul 1, 2020 at 1:13

1 Answer 1

2

TL;DR: Restart kestrel service file & apache with:

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

or

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

I had the exact same problem. Here was my solution:

If everything worked in development with the following code in Program.cs,

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

Then I use the following for Kestrel on my Ubuntu instance:

public static IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
       {
          webBuilder.UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseUrls("http://*:5000")
            .UseStartup<Startup>();
       });
Sign up to request clarification or add additional context in comments.

2 Comments

Yes! Thanks. I was unable to serve staticfiles for my asp.net core webapp running on Apache2 (Debian server) and your code solved my problem. So it looks like it's a Kestrel issue! Just wanna add, for Directory.GetCurrentDirectory() to work, you need to add using System.IO to your .cs
in prod on Linux, for me it worked with .UseContentRoot(AppContext.BaseDirectory) AND writing the script as shown in this answer stackoverflow.com/a/69292799/7873901

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.