Combined with Docker Compose, Nginx reverse proxy and load balancing are used for ASP.NET 5 applications

[Editor's Note] This article describes the use of Nginx in ASP.NET 5, which provides a load balancing mechanism that runs locally and uses Docker Compose technology.

ASP.NET 5, for different operating systems, as well as different web services such as IIS , there are many different hosting options. Filip W has a lot of blog posts that are running on the ASP.NET 5-based server . Here, I mainly describe the use of NGINX in ASP.NET 5 , which provides a load balancing mechanism that runs locally and uses Docker Compose technology.

great! # Aspnet5 + #nginx + #docker + #docker compose, making up a simple load balancing 🙂 https://t.co/YnJamDubIS pic.twitter.com/pBOWDDnVHR
– Tugberk Ugurlu (@tourismgeek) 2016.1.26

In the previous. NET web development, we have not had these options. For example, you can be very skilled in the Mono project using ASP.NET Web API application, and exposed to the bottom of Nginx. However, ASP.NET 5 makes these options really true.

We get the results as shown below, I put the example here :
01.jpg

Applies to APS.NET 5 on RC1

I have a very simple APS.NET 5 application that outputs a Hello message that lists the environment variables on that host. The structure of the project is as follows:

  Tugberk @ ubuntu: ~ / apps / aspnet-5-samples / nginx-lb-sample $ tree 
The
├── docker-compose.yml
├── docker-nginx.dockerfile
├─ docker-webapp.dockerfile
├── global.json
├ ─ ─ nginx.conf
├── NuGet.Config
├── README.md
└ ─ ─ WebApp
├── hosting.json
├── project.json
└─-- Startup.cs

I did not put the source here, but you can find it here . However, I have to emphasize that the number of Server URLs exposed to K5 through the Kestrel should be. For Docker use, we need to configure "0.0.0.0" instead of localhost or 127.0.0.1. Mark Rendle has a lot of description to illustrate this problem, I have given the following document hosting.json also covered the problem:

  { 
"Server": "Microsoft.AspNet.Server.Kestrel",
"Server.urls": "http: / / v.0.0.0:5090"
}

Application of Docker in ASP.NET 5 Application

The next step is to run the ASP.NET 5 application under Docker. There is an ASP.NET 5 Docker image in the Docker Hub repository, especially simple. In addition, Mark Rendle has three very insightful articles on the use of ASP.NET 5, Docker and Linux, Part1 , Part2 and Part3 . It is strongly recommended that you read these articles. I have an instance of its Dockerfile (pointing to the file ) as follows:

  FROM microsoft / aspnet: 1.0.0-rc1-update1 

COPY ./WebApp/project.json / app / WebApp /
COPY ./NuGet.Config / app /
COPY ./global.json / app /
WORKDIR / app / WebApp
RUN ["dnu", "restore"]
ADD ./WebApp / app / WebApp /

EXPOSE 5090
ENTRYPOINT ["dnx", "run"]

This is the simple Dockerfile file running under Docker running ASP.NET 5, the following generates the Docker mirror and runs the mirror:

  Docker build -f docker-webapp.dockerfile -t hellowebapp. 
Docker run -d -p 5090: 5090 hellowebapp

The container is now running in split mode, and you can access the HTTP terminal from the host.
02.png
Now you can do whatever you want to do with this Container. Recompile, stop, remove, and so on.

Nginx and Docker Compose

The last part of the chapter is Nginx and Docker Compose. Nginx is a free, open source, high-performance HTTP server and reverse proxy for people who do not understand Nginx. In the actual development environment, you do not want to expose Kestrel directly to the outside. Instead, you can put Kestrel behind a mature Web server, such as Nginx, IIS, or Apache Web Server .

There are two good videos that you can look at about Kerstrel and Linux hosts. From the video you can know why put Kestrel behind a Web server. I strongly recommend that you look at these videos before using them in Linux.

  • ASP.NET 5: Kestrel
  • ASP.NET 5: Considerations for Production Linux Environments

Docker Compose, from a different perspective is a completely different tool type, define and run a multi-container Docker application. With Compose, you can use a Compose file to configure your own application service. This is a perfect combination for the project we want to achieve here because we have at least three containers to run:

  • ASP.NET 5 Application 1: An instance of ASP.NET 5 application
  • ASP.NET 5 Application 2: Another instance of ASP.NET 5 application
  • Nginx container: A Nginx process, proxy request to ASP.NET 5 application

First start configuring Nginx so that it can run under Docker. Nginx mirroring already exists on the Docker Hub. We will use the mirror and use the following Nginx configuration:

  Worker_processes 4; 

Events {worker_connections 1024;}

Http {
Upstream web-app {
Server webapp1: 5090;
Server webapp2: 5090;
}

Server {
Listen 80

Location / {
Proxy_pass http: // web-app;
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;
}
}
}

The configuration file in addition to some common configuration, it is important to load balancing and reverse proxy configuration. In this configuration file, the Nginx server receives requests from port 80 and then proxies these requests to webapp1: 5090 and webapp2: 5090. Learn more about NGINX reverse proxy and load balancing , from which you can know how to configure proxy and load balancing, and the example above is enough.

In the NGINX configuration, there is an important part. Kestrel in RC1 has a very troublesome problem , the problem has been resolved in the RC2 . In order to temporarily solve this problem, you need to set the "Connection: keep-alive" header, which is why we do NGINX configuration "proxy_set_header Connection keep-alive;" instructions.

The following is the Nginx Dockerfile file ( Github address ):

  FROM nginx 
COPY ./nginx.conf /etc/nginx/nginx.conf

At this point, you might ask webapp1 and webapp2 (what we've already described in Nginx configuration) to what variables are mapped. These are DNS references that are used to run ASP.NET 5 applications. When we reference webapp1 and webapp2 in the Docker Compose file, the DNS mapping will take effect automatically. Finally, the following is the organization of the Docker Compose file content ( Github address ):

  Webapp1: 
Build:.
Dockerfile: docker-webapp.dockerfile
Container_name: hasample_webapp1
Ports:
- "5091: 5090"

Webapp2:
Build:.
Dockerfile: docker-webapp.dockerfile
Container_name: hasample_webapp2
Ports:
- "5092: 5090"

Nginx
Build:.
Dockerfile: docker-nginx.dockerfile
Container_name: hasample_nginx
Ports:
- "5000: 80"
Links:
- webapp1
- webapp2

You can see the definition of the third container, we link the two containers previously defined to the Nginx container. Or, you might want to view Service Discovery in the Docker context to replace the link.
Now that we are ready, just run two docker-compose commands (under the Docker Compose file directory) to start the application and run:

  Docker-compose build 
Docker-compose up

Up to now, we can see three running containers, you can also visit localhost from the host terminal: 5000, you can see the load is distributed to two ASP.NET 5 applications on the container.
03.gif
Excellent! However, this is just a simple example of the demonstration, the purpose is to illustrate the local operation of a application like this is very easy. This may not provide performance guarantee for all containers running in a box. My next step is to incorporate HAProxy into load balancing.

Source: NGINX Reverse Proxy and Load Balancing for ASP.NET 5 Applications with Docker Compose (translation: Shi Yuezhen)

    Heads up! This alert needs your attention, but it's not super important.