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


一.實現前的准備

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

  • 三台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
        # Don't 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 Apache's document root
        # concurs with nginx's 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時的權重進行轉向,到現在,一個簡單的集群已經搭建。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM