John Singh
John Singh

Reputation: 21

Setting up an ASP Net Core app with subdirectories

I'm trying to set up an ASP Net Core app in a subdirectory (http://<website>/app), but I'm having an issue with the app not recognizing it's URL base should start with /app/. So when I make requests to static content, or actions it's acting as if the base is "/" and not "/app". (ex: http://<website>/app/<static_content> is what I need, but the app requests http://<website>/<static_content>)

NGINX is set up like this:

server {
listen 80 default_server;
server_name <IP_Address>;
location /app/ {
    proxy_pass         http://localhost:4000/;
    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;
}}

My program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .UseUrls("http://localhost:4000");
    }

and my startup.cs contains this:

app.UsePathBase("/app");
app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
                RequestPath = "/app/wwwroot"
            });

EDIT: I got it working by removing the ending slash on the proxy_pass!

server {
    listen 80 default_server;
    server_name <IP_Address>;

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

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

both apps now seem to be making the proper requests.

Upvotes: 2

Views: 3351

Answers (2)

Nick Kovalsky
Nick Kovalsky

Reputation: 6452

core 3.1 in Configure insure you have set app.UsePathBase("/api or whatever subdir");

Upvotes: 0

Edward
Edward

Reputation: 29986

For responding request from / to /app, try code below in Startup.cs

            app.Map("/app",
            subApp =>
            {
                subApp.UseStaticFiles();
                subApp.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });

            }
        );

Upvotes: 1

Related Questions