1.根据URI实现负载均衡
1.1架构设计
用户请求(URI) | 处理请求服务器 | 站点目录 | 功能 |
---|---|---|---|
/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 | 默认 |
1.2服务器设置
1.2.1web02进行环境部署
[root@web01-172.16.1.8 ~]# mkdir /html/www/upload -p [root@web01-172.16.1.8 ~]# echo "upload-web集群-10.0.0.8" >> /html/www/upload/shuai.html [root@web01-172.16.1.8 ~]# cat /html/www/upload/shuai.html upload-web集群-10.0.0.8 [root@web02-172.16.1.8 /etc/nginx/conf.d]# vim blog.conf server{ listen 80; server_name www.ashuai.com; location / { root /html/www/; index shuai.html index.html; } }
1.2.2web01进行环境部署
[root@web01-172.16.1.7 ~]# mkdir /html/www/static -p [root@web01-172.16.1.7 ~]# echo "static-web集群-10.0.0.7" >> /html/www/static/shuai.html [root@web01-172.16.1.7 ~]# cat /html/www/static/shuai.html static-web集群-10.0.0.7 [root@web01-172.16.1.7 /etc/nginx/conf.d]# vim blog.conf server{ listen 80; server_name www.ashuai.com; location / { root /html/www/; index shuai.html index.php; } }
1.2.3web03进行环境部署
[root@web01-172.16.1.7 ~]# mkdir /html/www -p [root@web01-172.16.1.7 ~]# echo "default-web集群-10.0.0.9" >> /html/www/shuai.html [root@web01-172.16.1.7 ~]# cat /html/www/shuai.html default-web集群-10.0.0.9 [root@web03-172.16.1.9 /etc/nginx/conf.d]# vim blog.conf server{ listen 80; server_name www.ashuai.com; location / { root /html/www; index shuai.html index.html; } }
1.2.4配置负载均衡
[root@lb01-10.0.0.5 /etc/nginx/conf.d]# vim proxy.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.ashuai.com; location /upload { proxy_pass http://upload; proxy_set_header Host $host; } location /static { proxy_pass http://static; proxy_set_header Host $host; } location / { proxy_pass http://default; proxy_set_header Host $host; } } [root@lb01-10.0.0.5 /etc/nginx/conf.d]# nginx -s reload
2.根据用户访问的终端显示不同页面
http_user_agent
2.1 架构设计
用户请求端 | 处理请求服务器 | 站点目录 |
---|---|---|
iPhone | 10.0.0.8:80 | /html/www/ |
10.0.0.7:80 | /html/www/ | |
360 | 10.0.0.9:80 | /html/www |
2.2服务器设置
2.2.1web01环境部署
echo "iphone_access 10.0.0.7" >/html/www/shuai.html
2.2.2web02环境部署
echo "google_access 10.0.0.8" >/html/www/shuai.html
2.2.3web03环境部署
echo "360browser_access 10.0.0.7" >/html/www/shuai.html
2.2.4编写负载均衡配置文件
[root@lb01-10.0.0.5 /etc/nginx/conf.d]# cat proxy.conf upstream iphone { server 10.0.0.7:80; } upstream google { server 10.0.0.8:80; } upstream default { server 10.0.0.9:80; } server { listen 80; server_name www.ashuai.com; location / { if ($http_user_agent ~* iphone){ proxy_pass http://iphone; } if ($http_user_agent ~* Chrom){ proxy_pass http://google; } proxy_pass http://default; proxy_set_header Host $host; } }