1. 程式人生 > >Ubuntu-18.04 下使用Nginx搭建高可用,高並發的asp.net core集群

Ubuntu-18.04 下使用Nginx搭建高可用,高並發的asp.net core集群

style document pass width pro mat exceptio 就是 unix

一.實現前的準備

  • 以下是實現簡單負載均衡的思路,圖中的服務器均為虛擬機

技術分享圖片

  • 三臺Linux服務器,一臺用作Nginx負載均衡(192.168.254.139),另外兩臺用作Asp.Net Core應用程序承載的服務器(192.168.254.140,192.168.254.141)
  • 一臺用作於Client的Windows服務器。

二.環境搭建

1.Asp.Net Core程序

就是一個新建的空web應用程序,然後修改了下Startup的中間件,分別部署到2臺Ubuntu上。

    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { //await context.Response.WriteAsync("this is first web application");
await context.Response.WriteAsync("this is second web application"); }); } }

2.配置Nginx服務器

  • 安裝Nginx
    • 在Ubuntu終端使用 sudo apt-get install nginx 命令安裝Nginx。
    • 然後通過 sudo service nginx start 命令啟動服務
    • 確認瀏覽器顯示Nginx默認登錄頁。可在http://192.168.254.139/index.nginx-debian.html 訪問登錄頁面。
  • 配置Nginx
    • 若要將 Nginx 配置為反向代理以將請求轉接到 ASP.NET Core 應用,請修改 /etc/nginx/sites-available/default。 sudo vi /etc/nginx/sites-available/default
    • 設置proxy_pass後面的參數
    • 新增upstream,表示要轉接的服務器集合,upstream後面的名字要與proxy_pass後面的參數相對應
    • 修改完之後:wq保存,然後 sudo nginx -t 來驗證配置文件的語法有沒有錯誤,如果配置文件測試成功,可以通過運行 sudo nginx -s reload 強制 Nginx 選取更改
upstream cluster.com{
        server 192.168.254.140 weight=1;
        server 192.168.254.141 weight=1;
}       

server {
        listen 80 default_server;
        # listen [::]:80 default_server deferred;
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Dont use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _example.com *.example.com;

        location / {
                proxy_pass         http://cluster.com;
                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;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apaches document root
        # concurs with nginxs one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

2.Client控制臺應用程序

    class Program
    {
        static void Main(string[] args)
        {
            HttpClient httpClient = new HttpClient();
            for (int i = 0; i < 1000; i++)
            {
                string result = httpClient.GetStringAsync("http://192.168.254.139").Result;
                Console.WriteLine(result);
            }
            Console.ReadKey();
        }
    }

結果如下:

技術分享圖片

由結果可知,Nginx服務器會根據我們配置Nginx時的權重進行轉向,到現在,一個簡單的集群已經搭建。

Ubuntu-18.04 下使用Nginx搭建高可用,高並發的asp.net core集群