Publish ASP.NET Core 2.x application to Ubuntu

Briefly explain how to publish ASP.NET Core applications to Linux (Ubuntu) servers, which are all documents.

Server structure#

ASP.NET Core 2.x has two kinds of servers:

Both of these servers can directly serve applications (also called Edge Server), or they can be placed behind [Load Balancing] (https://cloud.tencent.com/product/clb?from=10680) or reverse proxy (Reverse Proxy).

If Edge Server is used this way:

In this case, you need to add the certificate to Kestrel or HTTP.sys to enable HTTPS.

But this is not easy to expand in the future, because it can't do load balancing, and it can't send traffic to other servers.

And if you put kestrel behind a reverse proxy, such as IIS or Nginx:

In this case, Reverse Proxy can be used as a load balancer, and it can also be used as the only place to configure certificates for HTTPS.

Today I will introduce this method.

HTTPS

The https request from the outside is sent to Reverse Proxy, and then it will use HTTP to forward the request to Kestrel Server, with some special headers when forwarding.

In this case, tasks such as certificate configuration, encryption, and HTTPS are all handed over to the Proxy Server.

These responses are returned by the Proxy using HTTPS:

At this time, the Proxy Server or load balancer will carry several headers on the HTTP connection from the inner layer to Kestrel, so that my Kestrel knows whether the request was originally HTTPS.

Mainly these three Headers:

In the ASP.NET Core code, we need to use the ForwardHeaders middleware and modify it in the Startup.Configure method:

Note that its location should be before UseHttpsRedirection, UseAuthentication, UseMvc, UseStaticFiles.

The configuration in the middleware means that ASP.NET Core is allowed to view the XForwardedFor and XForwardedProto headers.

Publish to Ubuntu

Install .NET Core

First you need to install .NET Core Runtime: https://www.microsoft.com/net/download

After clicking, select the corresponding steps according to your Linux distribution:

Finally, execute dotnet --info to verify the installation is successful:

Install Nginx

In addition, you need to install Nginx, check the official website document directly: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.1&tabs=aspnetcore2x# install-nginx

After installation, visit this page: http://your ip address/index.nginx-debian.html, if you see the following effects, the installation is successful:

Build the source code on the server and release##

Then is the release program, there are two ways to release:

Since I build and publish directly on the server, I need to install the .NET Core SDK: https://www.microsoft.com/net/learn/get-started-with-dotnet-tutorial

Then you can use the release command: dotnet publish --configuration Release.

The published files are under bin/Release/netcoreapp.*/publish*.

Then copy all the files under publish to my target folder:

In my target directory, there are these files:

If you execute dotnet test.dll, this program will run on localhost:5000:

Configure Nginx

Then we come back to configure Nginx, enter /etc/nginx/sites-available, there is a Default file in it, rename it, and then we create a new Default file:

After saving, execute sudo nginx -t to verify this configuration file.

Then execute nginx -s reload to restart nginx.

Then you need to run the published program again: dotnet test.dll:

When I use the URL to access port 80, it will automatically jump to port 5001, causing the connection to fail:

This is because HTTPS Redirection is used by default in the project. Because I don’t have a certificate, for the sake of demonstration, I commented out the code related to HTTPS Redirection before publishing:

After repeating the above steps, you can access normally through port 80 of the website:

NGINX configuration certificate and HTTPS

Configure HTTPS and certificate related content directly to see the official document: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.1&tabs=aspnetcore2x #configure-ssl

After adding proxy.conf and editing nginx.conf, restart nginx.

According to the operation, if you cannot use https to access the website normally after running, it may be caused by the inability to bind port 443.

Check the nginx error log: /var/log/nginx/error.log, if the following error occurs:

You can execute the following commands to solve:

sudo fuser -k 443/tcp****service nginx restart

Then visit https URL again:

In this way, you can access the https website normally.

Recommended Posts

Publish ASP.NET Core 2.x application to Ubuntu
How to install Graylog 1.x on Ubuntu 14.04.
ubuntu 16 install asp.net
Publish the node.js project under Ubuntu to heroku
How to deploy Clojure web application on Ubuntu 14.04
Remote connection to Ubuntu 19.1
How to upgrade to Ubuntu 20.04
Use Jexus 5.8.2 to deploy and run Asp.net core under Centos