Moving on from developing .NET using Visual Studio Community Mac, I started working on the necessary configuration for actually running on a Mac host (and by extension, a non-Windows host).
The main task here is to configure Nginx to assist Kestrel (the .NET Core web server). Kestrel currently excels are running the .NET Core application but is not fully-featured enough to do a good job with other things, like security and assets like images, hence the requirement for using two web servers.
The setup for Nginx to run in this reverse-proxy configuration is pretty straightforward.
MAMP configuration
I use MAMP to package up Apache, MySQL, and Nginx for development work, and it helps keep things fairly painless to configure. However, the basic configuration for MAMP does not allow Nginx to reverse proxy a custom address and port, which is what’s needed to direct requests to Kestrel.
Configuration Steps
The solution is to edit the template that MAMP uses to actually generate the nginx.conf file. Here are the steps:
1. In the MAMP menu, select File | Edit Template > Nginx (nginx.conf). This opens up the template file used to actually generate the nginx.conf. You’ll see various predefined macros in red.
2. Add a section to the template file and leave the rest alone – I chose to add a new “server { }” definition inside the “http {” section. and above the first existing “server { }” definition. This adds a new listener on port 9999 and passes on all queries to Kestrel listening on port 5000.
server { #listen over http on port 9999 on localhost listen 9999; server_name localhost; #Kestrel server location / { proxy_pass http://127.0.0.1:5000/; } }
3. Start (or restart) Nginx through MAMP and all should be well. The actual nginx.conf file generated by MAMP using the above template can be found in /Library/Application Support/appsolute/MAMP PRO/conf/. It may help to double check this config file to make sure your changes are correct and being seen by Nginx.
To run your solution, simply run Kestrel and your .NET Core app through Visual Studio, or by using the dotnet run command line instruction inside your project folder.
Nginx will now proxy all calls to Kestrel. Open up your site on localhost:9999
There are further optimizations you can add to the nginx.conf file. Foremost is to configure static file requests to be handled by Nginx rather than Kestrel for performance reasons.