Set Virtual Path to ASP NET Core MVC Application behind nginx reverse proxy Set Virtual Path to ASP NET Core MVC Application behind nginx reverse proxy nginx nginx

Set Virtual Path to ASP NET Core MVC Application behind nginx reverse proxy


Sounds like your reverse proxy works in a way that maps /mvcapp to / as below:

location /mvcapp {    proxy_pass         http://localhost:6009/;    # assume you expose the 6009 port    ...}

If that's the case, that's because the proxy cuts off the /mvcapp prefix. And your MVC app doesn't realize the virtual app path is /mvcapp. To fix that, add a middleware to set the PathBase explicitly:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){    // add this middleware as the first one    app.Use((context, next)=> {        context.Request.PathBase = Environment.GetEnvironmentVariable("ASPNETCORE_APPL_PATH");        return next();    });              //  ... other middlewares}

And pass the ASPNETCORE_APPL_PATH environment when starting the docker. For example, you can pass the environment in the command:

## assume you want to map the port 6009 to 80, ##     and the image name is nginxtocoredocker run -p 6009:80 -e ASPNETCORE_APPL_PATH='/mvcapp'  nginxtocore