I need to configure a website in Nginx that I can use a custom domain with, alongside other websites, not the default site.
I have a DigitalOcean Droplet (VM) with Nginx. I have a few HTML-only websites hosted there. Now I want to host an ASP.NET Core MVC application. I generated a MVC application (using dotnet new mvc) and pushed it to a GitHub repository and the application is built and deployed to the Droplet using a workflow. It is built and published using:
dotnet build project.csproj --configuration Release --no-restore
dotnet publish project.csproj -c Release -r linux-x64 --self-contained true
When I execute it (in the Droplet VM), it executes and I assume it works but of course the VM has no GUI so I am unable to browse to it that way.
The problem is that all the articles I find seem to try to cover all possibilities. I am not sure how much of that is relevant to me. There are two pieces; there is the ASP.Net in the MVC application and there is the Nginx configuration. I am connfused about what I need to do in the MVC application; must I modify the code that is generated? Normally when we create an application it works without modification. In this case, do I need to modify it to use it in Nginx?

Sam HobbsPosted Aug 7, 2023, 10:51 PM
Thank you. The important thing is that I need to configure a port. Must I use a different port for each application (website) using ASP.Net in the Nginx instance?
I do not have a
CreateHostBuildermethod. The following is the entire contents of myProgram.cs. I think it is the same as it was when it was created bydotnet new mvc. Your sample is using older code, correct? Perhaps that is part of the reason I cannot understand other code samples.There is at least one article that indicates I must execute tha application (Kestrel in it) as well as configure Nginx but you do not say that I need to and I hope I understand you that I do not need to do that.
I am already publishiing the application to a directory like
/var/www/myapp. I do have an Nginx server block in/etc/nginx/sites-availableand a symbolic link to it in/etc/nginx/sites-enabled/.The nameservers are pointing to DigiatlOcean and the domain is added to the Droplet so the A records are pointing to it.
Saravanan GanesanPosted Aug 7, 2023, 5:59 AM
Hi Sam ,
To host an ASP.NET Core MVC application with Nginx on your DigitalOcean Droplet, you need to perform a few steps to configure both the ASP.NET Core application and Nginx.
ASP.NET Core Application Configuration:
First, make sure your ASP.NET Core application is configured to listen on a port that Nginx can proxy requests to. By default, ASP.NET Core applications listen on port 5000. You can modify the
Program.csfile in your MVC application to use a different port if needed. OpenProgram.csand modify theCreateHostBuildermethod as follows:public static IHostBuilder CreateHostBuilder(string[] args) =>();
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup
webBuilder.UseUrls("http://localhost:5000"); // Change the port if needed
});
Build and Publish the ASP.NET Core Application: You mentioned that you are already building and publishing the application using
dotnet buildanddotnet publish. Ensure that the output of the publish process is placed in a folder that can be served by Nginx. Typically, you would publish the application to a directory like/var/www/myapp.Nginx Configuration: Next, you need to configure Nginx to proxy requests to your ASP.NET Core application. Open the Nginx configuration file for the specific domain you want to use for your ASP.NET Core application. This configuration file is usually found in the
/etc/nginx/sites-available/directory. Create a new configuration file or modify an existing one. For example, if you want to useexample.com, you could create a file calledexample.comin thesites-availabledirectory.a basic Nginx configuration that you can use as a starting point:
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:5000; // Use the same port as in your ASP.NET Core application
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
In this configuration, Nginx listens on port 80 for requests coming to
example.comandwww.example.com, and it proxies those requests to your ASP.NET Core application running onlocalhostport 5000.eating or modifying the Nginx configuration file, you need to create a symbolic link to enable the site. Run the following commands:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Test and Reload Nginx: Test the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Domain DNS Configuration: Finally, make sure that your domain (
example.com) points to the IP address of your DigitalOcean Droplet. You can do this by updating the DNS settings for your domain through your domain registrar's control panel.That's it! Your ASP.NET Core MVC application should now be accessible through your custom domain (
example.com) via Nginx.