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时的权重进行转向,到现在,一个简单的集群已经搭建。

转载于:https://www.cnblogs.com/hhzblogs/p/9818099.html

Ubuntu-18.04-x64 是一款基于Debian Linux的操作系统版本。它是由Ubuntu社区开发和维护的,主要用于个人电脑和服务器。 Ubuntu-18.04-x64 提供了现代化的桌面环境,采用了GNOME 3作为默认桌面,为用户提供了友好且直观的操作界面。它还支持多种自定义选项,让用户能够根据自己的需要对桌面进行个性化设置。 作为一款开源操作系统,Ubuntu-18.04-x64 提供了丰富的软件资源。用户可以通过Ubuntu软件中心轻松安装和管理数千种免费的应用程序。此外,用户还可以通过终端命令行使用包管理器apt-get来安装其他软件。 Ubuntu-18.04-x64 还提供了强大的安全性。它内置了防火墙和安全更新功能,可以及时修补系统漏洞,以防止恶意软件和网络攻击。同时,它还支持文件加密和用户帐户管理,用于增强用户数据的安全性和隐私。 对于开发人员和系统管理员而言,Ubuntu-18.04-x64 是一个理想的选择。它提供了广泛的开发工具和环境,包括Python、C++、Java等编程语言和Apache、MySQL、PHP等服务器软件。此外,Ubuntu-18.04-x64 还提供了轻量级容器技术Docker和Kubernetes,方便用户进行应用程序的部署和管理。 总结来说,Ubuntu-18.04-x64 拥有强大的功能和高度的可定制性,适用于不同类型的用户。无论是高级用户、开发人员还是普通用户,都能够在Ubuntu-18.04-x64 上获得良好的使用体验和稳定的系统性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值