Nginx-负载均衡

根据用户的uri信息进行负载分担

1、架构环境规划

web01     10.0.0.7
web02     10.0.0.8
web03     10.0.0.9
lb01      10.0.0.5
lb02      10.0.0.6
/upload	  集群-10.0.0.8:80  html/www/upload    upload服务器集群
/static   集群-10.0.0.7:80  html/www/static    static服务器集群
/         集群-10.0.0.9:80  html/www           default服务器集群

web01上进行环境部署:

[root@web01 html]# mkdir /html/www/static
[root@web01 html]# echo static-web集群_10.0 .0.7 >/html/www/static/test.html

web02上进行环境部署:

[root@web02 ~]# mkdir /html/www/upload
[root@web02 ~]# echo "upload-web集群_10.0.0.8" >/html/www/upload/test.html

web03上进行环境部署:

[root@web02 ~]# echo "default-web集群_10.0.0.9" >/html/www/test.html	

2、编写负载均衡配置文件

vim /etc/nginx/conf.d/lb.conf

upstream upload {
   server 10.0.0.8:80;
}
upstream static {
   server 10.0.0.7:80;
}
upstream default {
   server 10.0.0.9:80;
}

server {
    listen       80;
    server_name  www.test.com;
    location / {
       proxy_pass http://default;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_next_upstream error timeout http_404 http_502 http_403;
    }
    location /upload {
       proxy_pass http://upload;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_next_upstream error timeout http_404 http_502 http_403;
    }
    location /static {
       proxy_pass http://static;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_next_upstream error timeout http_404 http_502 http_403;
    }
}	

在浏览器中分别输入看看有什么不同

www.test.com
www.test.com/upload
www.test.com/static

根据用户访问的终端不同显示不同的页面

1、环境准备

iphone   www.test.com  --- iphone_access 10.0.0.7:80  mobile移动端集群
谷歌     www.test.com  --- google_access 10.0.0.8:80  web端集群
IE 360   www.test.com  --- default_access 10.0.0.9:80 default端集群

web01:

echo "iphone_access 10.0.0.7" >/html/www/test.html

web02:

echo "google_access 10.0.0.8" >/html/www/test.html

web03:

echo "default_access 10.0.0.9" >/html/www/test.html

2、编写负载均衡配置文件

upstream web {
   server 10.0.0.8:80;
}
upstream mobile {
   server 10.0.0.7:80;
}
upstream default {
   server 10.0.0.9:80;
}

server {
    listen       80;
    server_name  www.test.com;
    location / {
       if ($http_user_agent ~* iphone) {
          proxy_pass http://mobile;
       }
       if ($http_user_agent ~* Chrome) {
         proxy_pass  http://web;
       }
     
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_next_upstream error timeout http_404 http_502 http_403;
    }
}

用iphone、google、IE分别访问www.test.com看看有什么不同

posted @ 2021-08-30 13:55  Cai_HL  阅读(27)  评论(0编辑  收藏  举报
>