Linux +. NetCore + Nginx build cluster, coreng.pdf

Source: Internet
Author: User
Tags dotnet

Linux +. NetCore + Nginx build cluster, coreng.pdf

This article will share with you how to build a load cluster in Linux + NetCore + Nginx. After netcore2.0 is released, I have been reading the documentation on the official website and learning about new things, A follower from 1.0 to 2.0 can only summarize one sentence: Version 2.0, more APIs, more documents, and new razor templates I like. The main points of this article are as follows:

  • Quick installation of nginx in linux
  • Generated for the nercore project: win7-x64 and ubuntu.16.04-x64 runtime packages respectively
  • Nginx +. netcore reverse proxy example
  • Deploy a cluster (Linux + NetCore + Nginx)
  • Obtain the ip address of the user accessing the cluster and the ip address of the server where the response service is located.
Quick installation of nginx in linux

How to install nginx in linux, the solution to this problem should have been available many years ago. Here, we will explain the content that old bloggers don't care. Maybe you can jump to the second point directly;

First, you need a computer or virtual machine with a linux system installed. I am going to use ubuntu.16.04-x64 as a linux system. Then you need to be able to connect to the Internet. Then, you can directly download it using the following command under the root permission, install the nginx service: sudo apt-get install nginx; you can use the nginx service only after executing the preceding command. to verify whether nginx is successfully installed or available, run the following command to enable the nginx service: sudo service nginx start. Then, the browser can directly access port 80 of the linux Server: http: // 172.16.21.66: 80. A successful interface will not be displayed;

After running the get and install commands, you can find the folder in the default installation path:/etc/nginx. The configuration file to be modified is: /ect/nginx/sites-available/default, configure in this configuration fileReverse Proxy, cluster, CacheAnd other column operations;

Generate the: win7-x64 and ubuntu.16.04-x64 runtime packages for the netcore project, respectively

The example in this article is a project based on the razor template. Of course, I have not changed anything, but I still want to keep the new Code. 2.0 many articles have described how to generate a package before, here we also use one of the methods;

First open the project file (. csproj end file ).Right-click the project and choose edit xxx. csproj.Add the following nodes to the Project node:

  <PropertyGroup>    <RuntimeIdentifiers>win7-x64;ubuntu.16.04-x64</RuntimeIdentifiers>  </PropertyGroup>

The configuration here is complete. Let's just do this. Then we need to execute two commands respectively: dotnet restore and dotnet publish. We need to execute the following command:

64-bit windows operating package: dotnet restore, dotnet publish-f netcoreapp2.0 -- runtime win7-x64

Ubuntu.16.04-run the x64 platform package: dotnet restore, dotnet publish-f netcoreapp2.0 -- runtime ubuntu.16.04-x64

If nothing happens, two release packages will be generated successfully like me:

When executing the preceding command, pay attention to the following two points:

  • The value of the <TargetFramework> netcoreapp2.0 </TargetFramework> node in the csproj project file must correspond to your target sdk. Otherwise, the node will become stuck.
  • When you execute the restore command, the following error message is displayed:

Don't panic, this isWhy the nuget package is not successfully downloadedSolution: Change the network or directly go to the https://www.nuget.org/packages/runtime.linux-x64.Microsoft.NETCore.App/2.0.0 through a network to download and copy to your local;

Let's take a look at the services that run on Windows and ubunt respectively:

We can see that these two are different ip addresses and ports. This is used to pave the way for the subsequent cluster. The example needs to be;

Nginx +. netcore reverse proxy example

It is very easy to use nginx for proxy. Through the above steps, we have prepared all the work. The following only needs the nginx configuration file installed on the ubuntu System (here: add the following configuration to the location node in/ect/nginx/sites-available/default: proxy_pass: http: // 172.16.21.8: 1120. This 172.16.21.8 is the site on windows just tested, the ip address of the ubunt system where nginx is located corresponds to 172.16.21.66. We want to access the proxy ip address of nginx (port 80 by default), as shown in the following flowchart:

In order to better demonstrate the effect, we need to post a picture below:

Proxy is to replace the ip address or domain name used by the user from the user's perspective, and then forward the request to the real site. It is rare to use nginx to configure proxy for a site separately, generally, either static resource caching or cluster creation is required. The following is an example: linux + netcore + nginx to build a cluster. I hope you can continue reading this article;

Deploy a cluster (Linux + NetCore + Nginx)

Here we are about to enter the main part of today's video. As we have already generated the netcore runtime packages for windows and ubuntu, we will use them directly here, A lot of configuration work is required for a cluster built with nginx, such as resource cache and ing Address Configuration. nginx has many configurations. Today, we will only move closer to the simple one; let's take a look at the secondary flowchart:

This is what we need to accomplish. The server load will forward user requests through port 80 of the unbunt system where nginx is located. The windows site and ubunt site project are the same, the netcore release package generated above. The user accesses the proxy... 66: 80 => forward requests to sites on different servers (two test sites can be expanded without limit );

Next, let's take a look at how nginx can be configured for load balancing. The following configuration information:

# Cache begin cache file proxy_temp_path/var/www/proxy_tmp; proxy_cache_path/var/www/proxy_cache levels = keys_zone = my_cache: 200 m inactive = 600 m max_size = 30g; # cache end # Cluster site configuration upstream shenniu. services {server 172.16.21.66: 1121 fail_timeout = 60 s; server 172.16.21.8: 1120 fail_timeout = 60 s;} server {# proxy listening port listen 80 default_server; listen [::]: 80 default_server; root/var/www/html; server_name _; # _ default ip + port access, _ Can be replaced with the access domain name, such as: shenniu.core.com # cache file Route location ~. * (\. (Js | css | jpg | svg). * {proxy_pass http://shenniu.services ; Proxy_cache_valid 200; proxy_cache my_cache; expires 3d;} # Cluster site Route location/{proxy_pass http://shenniu.services ;# http://shenniu.services Corresponding to the name proxy_http_version 1.1; proxy_set_header Upgrade $ http_upgrade; proxy_set_header Connection keep-alive; your Host $ host; your $ http_upgrade; your X-Forwarded-For $ balance ;}}

Through the above nginx configuration, let's re-load the configuration file command for nginx, such as sudo nginx-t reload. Then we access the nginx proxy address in the browser: http: // 172.16.21.66/, as shown in the following git:

As shown in the figure, the ip address of the server does not stop changing back and forth at the windows and ubutn sub-sites. When the load reaches this point, the nginx configuration is successful, you can read the notes in my configuration file or go to the official website to view them;

Obtain the ip address of the user accessing the cluster and the ip address of the server where the response service is located.

To obtain the real ip address of a user, you need to set something For the load. In nginx configuration, proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for is configured in location; this statement can pass the Request of the program. otherwise, the ip address in the Request can only be the ip address of the proxy server;

Some may ask why. A: Because users access our services through proxy servers, the access requests received by these services are from proxy servers, therefore, the obtained ip address is the Proxy Server ip address;

To solve this problem, you only needX-Forwarded-For netcore2.0, you only needStartup. csIn this way:

1 app.UseForwardedHeaders(new ForwardedHeadersOptions2 {3    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto4 });5 app.UseAuthentication();

Then we can get the real ip address of the user on any interface. The test case is as follows:

Client: <br/> Request. httpContext: @ Request. httpContext. connection. remoteIpAddress <br/> Request. headers: @ Request. headers ["X-Forwarded-For"] <br/> server: <br/> Request. httpContext: Obtain the ip address of the server where the response service is located: @ Request. httpContext. connection. localIpAddress <table class = "table"> <thead> <tr> <th> key </th> <th> value </th> </tr> </thead> <tbody> @ foreach (var item in Request. headers) {<tr> <td> @ item. key </td> <td> @ item. value </td> </tr >}</tbody> </table>
@ Request. headers ["X-Forwarded-For"]. Note that if there is a multi-tier proxy X-Forwarded-For, the obtained real ip addresses will be appended one by one. This will not be demonstrated here;
If this article provides you with help, you may wish to give me a thumbs up. Thanks for your support !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.