根据用户访问的uri进行负载均衡
所在路径 | IP地址 | 路径 | 集群 |
---|---|---|---|
/upload | 192.168.100.101 | html/www/upload | upload服务器集群 |
/static | 192.168.100.102 | html/www/static | static服务器集群 |
/ | 192.168.100.103 | html/www | default服务器集群 |
192.168.100.104 | 负载均衡服务器 |
web01上进行环境部署:
[root@webl ~]# mkdir /html/www/static
[root@web1 ~]# echo "static-web集群_192.168.100.101" >/html/www/static/index.html
web02上进行环境部署:
[root@web02 ~]# mkdir /html/www/upload
[root@web02 ~]# echo "upload-web集群_192.168.100.102" >/html/www/upload/index.html
web03上进行环境部署:
[root@web3 ~]# echo "default-web集群_192.168.100.103" >/html/www/index.html
第二个历程: 编写负载均衡配置文件
[root@lb01 conf.d]# cat lb.conf
upstream static {
server 192.168.100.101:80;
}
upstream upload {
server 192.168.100.102:80;
}
upstream default {
server 192.168.100.103:80;
}
server {
listen 80;
server_name www.xxs.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;
}
}
[root@lb01 conf.d]# curl www.xxs.com
default-web集群_192.168.100.103
[root@lb01 conf.d]# curl www.xxs.com/upload
upload-web集群_192.168.100.102
[root@lb01 conf.d]# curl www.xxs.com/static
static-web集群_192.168.100.101
总结: 实现网站集群动静分离
01. 提高网站服务安全性
02. 管理操作工作简化
03. 可以换分不同人员管理不同集群服务器
根据用户的终端信息显示不同页面
web01:
echo "iphone_access 192.168.100.101" >/html/www/index.html
web02:
echo "google_access 192.168.100.102" >/html/www/index.html
web03:
echo "default_access 192.168.100.103" >/html/www/index.html
编写负载均衡配置文件
[root@lb01 conf.d]# cat lb.conf
upstream app {
server 192.168.100.101:80;
}
upstream web {
server 192.168.100.102:80;
}
upstream default {
server 192.168.100.103:80;
}
server {
listen 80;
server_name www.oldboy.com;
location / {
if ($http_user_agent ~* iphone) {
proxy_pass http://app;
}
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;
}
}