Report this

What is the reason for this report?

Asp.Net Core Web Application Static Files gives 404 on Nginx

Posted on June 22, 2020

I am getting Failed to load resource: the server responded with a status of 404 (Not Found) for my static resources under wwwroot folder after the deployment of ASP.NET Core 2.2 mvc web application on Ubuntu 18.04/Nginx in digitalocean. I am getting text from the database and css/js from wwwroot/css & wwwroot/js but fail to load all the resources which resides under wwwroot folder. Application is working perfectly on IIS but getting error while deploying on Linux environment.

Here is the folder structure of my wwwroot.

wwwroot
         /css 
         /js
         /lib
         /Files(Contains folder and files from tinymce editor)
         /templates
                   /template1  
                            /js
                            /cs
         /themes
                /theme1   
                      /css
                      /js
               /theme2

Here is my Configure method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
                {
                    app.UseForwardedHeaders(new ForwardedHeadersOptions
                    {
                        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
                    });
        
                    if (env.IsDevelopment())
                    {
                        app.UseDeveloperExceptionPage();
                        app.UseDatabaseErrorPage();
                    }
                    else
                    {
                        app.UseStatusCodePagesWithRedirects("/Error/{0}");
                        app.UseHsts();
                    }
        
                    app.UseHttpsRedirection();
                    app.UseStaticFiles();
                    app.UseAuthentication();            
                    app.UseCookiePolicy();
                    app.UseSession();
                    app.UseMvc(routes =>
                    {
                        routes.MapRoute(
                            name: "default",
                            template: "{controller=Home}/{action=Index}/{id?}");
        
        
                    });
                    WebRootPath = env.WebRootPath;
                }

Here is Program Class code:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseUrls("http://localhost:5004")
                    .UseStartup<Startup>();

Here is code on /etc/nginx/sites-available/domain

 location / {
    		# First attempt to serve request as file, then as 
    		# directory, then fall back to displaying a 404.
                     proxy_pass http://localhost:5004;
                     proxy_ssl_server_name on;
                     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;
    		  try_files $uri $uri/ =404;
    	}


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

The Nginx configuration you are using is a typical configuration for serving static files directly, but it is not the ideal configuration when using ASP.NET Core, especially when it’s serving static files. Nginx serves as a reverse proxy in front of your ASP.NET Core application which is responsible for serving the static files.

You may try to remove the line try_files $uri $uri/ =404; because when this line is processed, Nginx will try to serve the file itself but the files are not in the directory that Nginx has access to.

Your Nginx configuration should look like:

location / {
    proxy_pass http://localhost:5004;
    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;
}

You should also ensure that your ASP.NET Core application has the necessary permissions to access and serve files from the wwwroot directory.

Moreover, make sure the UseStaticFiles middleware is correctly configured in your Startup.cs file:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // Other code...
    app.UseStaticFiles(); // Should be before UseMvc
    app.UseMvc();
}

Then, you can restart Nginx and your application:

sudo systemctl restart nginx
dotnet your_app.dll

If this does not help, I will suggest checking the error logs of Nginx and your application for more details. The Nginx error log is typically located in /var/log/nginx/error.log. The application logs would depend on how you’ve set it up in ASP.NET Core.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.