The Simplest Reverse Proxy for ASP.NET Core Using Docker
In my earlier post about hosting an ASP.NET core application in docker for production use I fudged a little bit in terms of what it means to production-worthy. Using the dotnet run command starts the web application on the Kestral web server. Kestral is a fine web server for development and maybe production use in an intranet environment but should not be exposed to the internet in a production environment.
You need a reverse proxy. Microsoft has two pages of documentation about this.
The titles for these pages are misleading since the web app is not hosted by Nginix (or Apache). The ASP.NET Core application is running in Kestral and the reverse proxy server is in front managing the requests, logging, SSL termination, etc. Additionally this documentation is neither simple nor straightforward.
Here what follows is THE most painless way for setting up a reverse proxy server for an ASP.NET Core application server running in a docker container.
Overall you need to spin up two additional docker containers. First container is the reverse-proxy server itself, based on nginx. Second container is optional and manages the SSL cerficates from letsencrypt. Documentation for both containers is quality. I followed exactly.
(1) Start the Reverse Proxy Container
(This command is slightly different if you have purchased an SSL certificate separately. See the documentation)
(2) Start the Certificate Management Container
(3) Add a few environment variables to the docker-compose file for your ASP.NET Core web application
(4) Start the container for your Web Application
(5) Wait for the magic to happen
It might take a while for the certificate management container to get the SSL certificate fetched and configured for your web application.
If you’re impatient here are a couple of other commands to check on things.
BONUS: These containers will support/redirect multiple applications for domains/subdomains on the same host.