Reverse proxies and .Net Core MVC

So one of the other difficulties I had with in standing up the wedding website was that it had to go through Nginx as a reverse proxy. I don’t really know the ins and outs of Kestrel as a server, so exposing that the internet just seemed inherently risky. Nginx I know, though (and I’ve gotta say, much easier to configure then web servers I’ve used in the past).

So I looked up what I had to do to set up the configuration, and found that Microsoft actually laid out a pretty good guide on how to do this (I’d post what my configuration is here but that just seems unwise…).

After getting everything configured, it still wasn’t working. Nginx was going to the right path and I could pull up the page locally, but every time I tried accessing it remotely, it 404’d. What gives??

Well, it turned out it was because I changed the base path for the application. Since this used to be on it’s own domain by itself, the base path was just the root. So my first thought was just to change the routing. Simple enough (though very hacky…): just add “wedding-website” to the route and modify the view file paths to do the same.

Well, I started discovering that wasn’t exactly as simple as I thought. Even though I had the routing right and some of the static paths right, a lot of the images, fonts and other statics were still breaking and it was starting to look like I would have to modify much more then that.

So I looked into ways to solve this, and apparently the supported solution was extremely simple: Just use the BasePath middleware. For example, if you wanted to host your app behind a reverse proxy and set the location as /test, adding the following below to the Configure function in Startup.cs will configure the routes and tilde paths to utilize it as the base path:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  app.UsePathBase("/test");

If you have paths that don’t utilize the tilde in your views or js, you’ll have to change those manually. But other than that, it fixed like 90% of my issues getting the site to work behind a reverse proxy.

I didn’t see much about this on the internet so I figured I’d just comment on it.

This article was written by thomas

Leave a Reply

Your email address will not be published. Required fields are marked *