负载均衡企业实践应用
1) 根据用户访问的uri信息进行负载均衡 第一个历程: 架构环境规划 /upload 集群-10.0.0.7:80 html/www/upload upload服务器集群 /static 集群-10.0.0.8:80 html/www/static static服务器集群 / 集群-10.0.0.9:80 html/www default服务器集群 web01上进行环境部署: [root@web01 html]# mkdir /html/www/upload [root@web01 html]# echo upload-web集群_10.0.0.7 >/html/www/upload/index.html web02上进行环境部署: [root@web02 ~]# mkdir /html/www/static [root@web02 ~]# echo "static-web集群_10.0.0.8" >/html/www/static/index.html web03上进行环境部署: echo "default-web集群_10.0.0.9" >/html/www/index.html 第二个历程: 编写负载均衡配置文件 [root@lb01 conf.d]# cat 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.oldboy.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; } } web01: server { location / { root /html/www; index index.html index.htm; } } web02: server { location / { root /html/www; index index.html index.htm; } } web03: server { location / { root /html/www; index index.html index.htm; } }
测试:浏览分别访问www.oldboy.com www.oldboy.com/upload www.oldboy.com/static查看显示页面
2) 根据用户访问的终端信息显示不同页面 第一个历程: 准备架构环境 iphone www.oldboy.com --- iphone_access 10.0.0.7:80 mobile移动端集群 谷歌 www.oldboy.com --- google_access 10.0.0.8:80 web端集群 IE 360 www.oldboy.com --- default_access 10.0.0.9:80 default端集群 web01: echo "iphone_access 10.0.0.7" >/html/www/oldboy.html web02: echo "google_access 10.0.0.8" >/html/www/oldboy.html web03: echo "default_access 10.0.0.9" >/html/www/oldboy.html 第二个历程: 编写负载均衡配置文件 [root@lb01 conf.d]# cat lb.conf 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.oldboy.com; location / { if ($http_user_agent ~* iphone) { proxy_pass http://mobile; } if ($http_user_agent ~* Chrome) { proxy_pass http://web; } 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; } }