WTM(ASP.NET Core)使用nginx搭建负载均衡集群

        为了提升网站的性能,我们可以采用搭建负载均衡集群方案和读写分离。前面我的博客讲述了WTM框架配置读写分离,现在我们继续在前面的基础上搭建负载均衡集群。

 一、环境搭建

        我们这里在本地用VMware Workstation建立了3台WindowsServer2012服务器,2台服务器都安装SqlServer2017开发版本数据库。1台服务器安装Nginx,配置反向代理。

                                                                                             服务器信息

二、下载并配置Windows版本Nginx。

1、下载选择主要版本。

2、传送到服务器并解压到合适位置,我这里是虚拟机就在C盘解决了。然后在nginx\conf文件下打开Nginx.conf文件。

2.1添加转发网址,关于Nginx的一般配置,请参照博客Nginx +iis反向代理。对于各项Nginx详细参数配置请参照博客Nginx upstream的5种权重分配方式

 upstream www.aaa.com  { 
        server  192.168.137.129:808 weight=1; #第一个测试网站
        server  192.168.137.128:808 weight=1;  #第二个测试网站
    } 

2.2、配置反向代理指向地址(这里的www.aaa.com可以随便修改)

proxy_pass  http://www.aaa.com;

2.3、如果出现80端口被占用的情况,则修改 

listen       80;#这个原来是80端口,如果80已经被占用需要进行修改,任意指定未使用端口

2.4、总体配置如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
 
    upstream www.aaa.com  { 
        server  192.168.137.129:808 weight=1; #第一个测试网站
        server  192.168.137.128:808 weight=1;  #第二个测试网站
    } 

    server {
        listen       80;#这个原来是80端口,如果80已经被占用需要进行修改
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://www.aaa.com;#反向代理指向地址        

            
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3、双击启动 Nginx.exe 文件位于nginx根目录下。启动任务管理器,查询Nginx是否运行。

4、如果node1服务器和node2服务器部署WTM应用正常。则访问http://localhost:80(或自定义端口号),即可轮询访问到

 server  192.168.137.129:808 
 server  192.168.137.128:808 。

修改敢问策略,参照博客Nginx upstream的5种权重分配方式

在主机输入浏览器中访问192.168.137.130,即可通过node3反向代理服务器,访问到node1web服务,node2web服务。实现负载均衡。

三、集群扩张

只需要将部署好的网站在upstream添加,配置好访问策略即可。

upstream www.aaa.com  { 
        server  192.168.137.129:808 weight=1; #第一个测试网站
        server  192.168.137.128:808 weight=1;  #第二个测试网站  
        server  XXX.YYY.ZZZ.QQQ:PORT weight=1;  #第三个测试网站   
        server  XXX.YYY.ZZZ.QQQ:PORT weight=1;  #第四个测试网站  
        ***                                     #第N个测试网站      
    }  

猜你喜欢

转载自blog.csdn.net/sxy_student/article/details/106885600