一.实现前的准备

  • 以下是实现简单负载均衡的思路,图中的服务器均为虚拟机

  • 三台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=;
server 192.168.254.141 weight=;
} server {
listen default_server;
# listen [::]: default_server deferred;
# SSL configuration
#
# listen ssl default_server;
# listen [::]: 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 .
try_files $uri $uri/ =;
} # 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.-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:;
#} # 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 = ; i < ; 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集群的更多相关文章

  1. Ubuntu 18.04 下 Redis 环境搭建

    一.安装 Redis ① 下载 wget http://download.redis.io/releases/redis-3.2.8.tar.gz ② 解压 tar -zxvf redis-3.2.8 ...

  2. Ubuntu 18.04 下配置 HAXM 加速 Android模拟器

    Ubuntu 18.04 下配置 HAXM 加速 Android模拟器 最近在vmware环境下搭建ubuntu18.04开发环境,开始发现总是运行android模拟器在console提示加载如下错误 ...

  3. Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)

    Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具) 最近因为项目关系,收朋友之托,想制作秀场网站,但是因为之前一直没有涉及到这 ...

  4. Ubuntu 18.04 下 emscripten SDK 的安装

    Ubuntu 18.04 下 emscripten SDK 的安装http://kripken.github.io/emscripten-site/docs/getting_started/downl ...

  5. Ubuntu 18.04下安装Steam顶级在线游戏平台

    Ubuntu 18.04下安装Steam顶级在线游戏平台 原创: 聆听世界的鱼 Linux公社 今天 Steam是由Valve公司开发的顶级在线游戏平台,是目前全球最大的综合性数字发行平台之一.它让你 ...

  6. 在Ubuntu 18.04 下安装mysql,没有初始密码,重设root密码

    在Ubuntu 18.04 下安装mysql 不知道是由于mysql更新为新版还是.Ubuntu18.04中的特性,安装过程中没有设置密码的环节,在网络上找了半天,总算解决了!特此记录下来,以便以后查 ...

  7. ubuntu 18.04下Chromium设置为系统代理

    前言 在ubuntu 18.04下挂上ss后firefox能直接上google了但是chromium上不去 会出现下面两种情况 # This site can't be reached xxxxxx ...

  8. Ubuntu 18.04下Couldn't connect to Docker daemon at http+docker://localunixsocket解决办法

    一台服务器系统为:Ubuntu 18.04 LTS,上面建了git裸仓库,用于开发吧代码push到这里.同时WEB测试环境通过docker也部署在这台.通过git钩子post-receive,当有新代 ...

  9. 在ubuntu 18.04下,无线网卡无驱动,连不上wifi,显示wifi没有适配器的解决方法

    近来因为做东西要用到linux环境,所以自己的笔记本在win10的系统上又安装了ubuntu 18.04版本的双系统,但是安装好以后,没有无线网卡的驱动,显示wifi没有适配器等字样,很纠结,前后研究 ...

随机推荐

  1. ActiveMQ HelloWorld入门

    在P2P的消息模型中,双方通过队列交流,一个队列只有一个生产者和一个消费者.a.消息生产者 package com.ljq.durian.test.activemq; import javax.jms ...

  2. LDA Gibbs Smapling理解

    即排除当前词的主题分配,根据其他词的主题分配和观察到的单词来计算当前词主题的概率公式 里面用到了伽马函数的性质 当Gibbs sampling 收敛后,我们需要根据最后文档集中所有单词的主题分配来计算 ...

  3. C和C++结构体的区别

    C的结构体内不允许有函数存在,C++允许有内部成员函数,且允许该函数是虚函数.所以C的结构体是没有构造函数.析构函数.和this指针的. C的结构体对内部成员变量的访问权限只能是public,而C++ ...

  4. 从n个字符中,找出m个排列组合

    #include <string.h> #include <stdio.h> #include <stdlib.h> void combine(char *valu ...

  5. RabbitMQ上手记录–part 2 - 安装RabbitMQ

    上一篇<<RabbitMQ 上手记录-part 1>>介绍了一些基础知识,整理了一些基础概念.接下来整理一些安装步骤和遇到的问题. 我在CentOS7和Ubuntu16.4上都 ...

  6. CUBA 7 新特性 (下篇)

    上篇我们主要介绍了 CUBA 7 中前端的一些主要功能.这篇我们介绍一下中间件的一变化和新特性. 中间件功能 前面关于新的界面 API 的描述内容比我预期的要多许多,所以在这一节,我会尽量简单点说! ...

  7. CUBA 7 新特性(上篇)

    三年前,我们宣布了 CUBA 框架的第二个公开的主版本.CUBA 6 是改变游戏规则的版本 - 框架的许可从私有化变成了公开的 Apache2.0.那些日子里,我们甚至猜不到这个变化会最终将框架带向何 ...

  8. Feign性能优化注意事项--超时

      Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 1. at feign ...

  9. C#的async和awaiit的一些记录

    一,最近在学习abp的东西.发现其中作者在获取数据时,大量的使用了async和awaiit.好吧,因为不太懂,所以网上搜搜,在这里记录一下. 先来看一段代码.调用UpdateUserAsync会返回一 ...

  10. C# 分页方法

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web; ...