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

Ubuntu-18.04 下使用Nginx搭建高可用,高并发的asp.net core集群,第1张

概述一.实现前的准备 以下是实现简单负载均衡的思路,图中的服务器均为虚拟机 三台Linux服务器,一台用作Nginx负载均衡(192.168.254.139),另外两台用作Asp.Net Core应用程序承载的服务器(192.168.254.140,192.168.254.141) 一台用作于Client的Windows服务器。 二.环境搭建 1.Asp.Net Core程序  就是一个新建的空web 一.实现前的准备 以下是实现简单负载均衡的思路,图中的服务器均为虚拟机

三台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集群全部内容,希望文章能够帮你解决Ubuntu-18.04 下使用Nginx搭建高可用,高并发的asp.net core集群所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1242544.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存