Nginx负载均衡实战

三台安装配置完成的nginx机器:
10.0.0.30 lb01      负载均衡器
10.0.0.40 www01     节点1
10.0.0.50 www02     节点2

负载均衡服务器配置:

10.0.0.30 lb01 
upstream为负载均衡池
 upstream www_pools {
    server 10.0.0.40 weight=1;    #添加后台节点机器
    server 10.0.0.50 weight=1;    #可以为多台

}

    server {
        listen       80;
        server_name  www.junwu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        proxy_pass http://www_pools;   #location定位通过proxy_pass传递到负载均衡池  
       }
10.0.0.40 www01     节点1:
此节点配置了两台虚拟主机
server {
        listen       80;
        server_name  www.bbq.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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;
        }
    }
    server {
        listen       80;
        server_name  www.mxj.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/mxj;
            index  index.html index.htm;
        }
 配置域名解析后检查页面访问

  [root@www01 nginx-1.25.1]# curl www.bbq.com
  <meta charset=utf8>
  这是网站www.bbq40.com
  [root@www01 nginx-1.25.1]# curl www.mxj.com
  <meta charset=utf8>
  这是网站www.mxj40.com

 
10.0.0.50 www02     节点2
此节点同样配置了两台虚拟主机
server {
        listen       80;
        server_name  www.bbq.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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;
        }
    }
    server {
        listen       80;
        server_name  www.mxj.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/mxj;
            index  index.html index.htm;
        }
配置域名解析后检查页面访问

  [root@www02 nginx-1.25.1]# curl www.bbq.com
  这是网站www.bbq50.com
  [root@www02 nginx-1.25.1]# curl www.mxj.com
  这是网站www.mxj50.com

 

负载均衡效果

 

[root@lb01 conf]# curl www.junwu.com
这是网站www.bbq50.com
[root@lb01 conf]# curl www.junwu.com
<meta charset=utf8>
这是网站www.bbq40.com
[root@lb01 conf]# curl www.junwu.com
这是网站www.bbq50.com
[root@lb01 conf]# curl www.junwu.com
<meta charset=utf8>
这是网站www.bbq40.com
[root@lb01 conf]# curl www.junwu.com
这是网站www.bbq50.com

 

从测试结果中可以看到,请求是逐一分发给两个节点服务器了,实现了负载均衡,

但是为何只看到bbq的页面内容,也就是定义的虚拟主机一,而没有出现mxj呢?

【图解答案】

如何解决上述问题

其根本原因是,用户访问域名时候确实是www.junwu.com,请求首先是发给了Nginx反向代理服务器

问题是:

  • 向代理服务器(lb01)重新发起请求时,默认并没有在请求头里告诉节点服务器要找哪一个虚拟主机【www.bbq.com】还是【bbs.mxj.com】
  • 因此后端节点服务器接收到请求之后,并没有主机头信息,默认把请求发给了第一个虚拟主机去处理(以www01的nginx.conf中的配置,也就是bbs站点内容了)

解决办法:

  • 在反向代理时候,添加主机头信息,明确告诉节点服务器找哪个虚拟主机
proxy_set_header Host $host;

在代理服务器向节点服务器发送HTTP请求头中添加host主机头信息后,若是后端服务器配置了多个虚拟主机,也就可以根据主机头的信息,来进行匹配决定发给哪一个虚拟主机【bbq还是mxj】。

负载均衡nginx.conf修改如下,修改location的配置·

 server {
        listen       80;
        server_name  www.junwu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://www_pools;
#添加该参数,在向后端发请求的时候,就会保留客户端的主机头信息,发给节点服务器
            proxy_Set_header Host $host;
}

注意要添加DNS域名解析

[root@lb01 conf]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.30 www.junwu.com 
10.0.0.30 www.bbq.com www.mxj.com 

 








posted @ 2023-06-25 00:44  Junwu’sblog  阅读(32)  评论(0编辑  收藏  举报