Question

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

Posted June 22, 2020 141 views
Nginx

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;
        }
edited by MattIPv4
Submit an answer

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