Exploring .Net Core App Hosting with Caddy Server

I genuinely thought this post was going to be a lot bigger and more complicated.

I recently learned about Caddy Server, the free, open source web server that uses HTTPS by default. It boasts a hardened TLS stack with modern protocols including on-demand TLS, which I think is fantastic given how easy it is to set up.

To get started with a very basic example, I decided to spin up a basic template ASP.Net Core web application. Using Visual Studio 2017, I've basically selected "new ASP.NET Core Web Application (.Net Core)" from the new project menu and clicked "Next" until the project was scaffolded.

Without making any changes, I've then published the site to a folder on my local machine using a new "Folder Profile" publish option. I now have a folder I've named "core" that holds my published .Net Core site, comprising the required DLLs, wwwroot assets and the MVC views. I can test this now by opening Powershell, browsing to that published folder and running the site (e.g. `dotnet WebApplication1.dll`, where "WebApplication1" is the name of my project's main DLL).

At this point, I could run my application as-is, in a production setting if I wanted to, except that I'd need to make a code change to listen on port 80 instead of 5000, the web server might also have permissions complications that would require elevated privileges and that feels icky. Keeping the ports and processes decoupled seems like the better option and I can reverse proxy requests from the web server process to the dotnet runtime.

Install Caddy

Caddy is ridiculously simple to get up and running. Go to https://caddyserver.com/download and download the selected install for your detected OS. I love this attention to detail: even the recommended install is handled for you automatically, but you can still customise the options and plugins before hitting go.

Unzip the download to a folder of your choice, open the folder and create a new file called simply "caddyfile". Don't use a file extension (e.g. "txt"); Caddy looks for "caddyfile" when it first runs.

Open your new file using a text editor and add an entry for our .Net Core site. I'll be running this in a development context, so I'll use ports on my local machine but this works exactly the same way in a production environment, only there'll likely be additional options and domain name entries you will want to configure. For development purposes right now, I'm only interested in having Caddy take care of the web requests for my .Net Core site. I'll accomplish this with a reverse proxy to the dotnet engine.

localhost:8081 { proxy / localhost:5000}

Assuming all has gone well, I can open a new Powershell command, browse to my folder where I've unzipped Caddy and type `.\caddy` to start the server up. Browsing to my new URL (or port number in this case) should bring up our ASP.Net site (I set two different sites up with caddy, as you can see from the screenshot below):

This is ridiculously easy and I'm excited about it because, aside from the obvious "business" applications for this, the low barrier for entry means I can use this in an educational context for kids with barely any technical jargon, convoluted configuration or ramp-up. It just works.