一、动静分离网站集群架构
实验环境准备:
/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服务器集群
根据用户访问的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 default服务器集群
web02上进行环境部署:
[root@web02 ~]# mkdir /html/www/upload
[root@web02 ~]# echo "upload-web集群_10.0.0.8" >/html/www/upload/index.html
web01上进行环境部署:
[root@oldboy01 html]# mkdir /html/www/static
[root@oldboy01 html]# echo static-web集群_10.0.0.7 >/html/www/static/index.html
web03上进行环境部署:
echo "default-web集群_10.0.0.9" >/html/www/index.html
第二个历程: 编写负载均衡配置文件
[root@lb01 /etc/nginx/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.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/static/
static-web集群_10.0.0.7
www.test.com/upload/
upload-web集群_10.0.0.8
www.test.com
default-web集群_10.0.0.9
总结: 实现网站集群动静分离
01. 提高网站服务安全性
02. 管理操作工作简化
03. 可以换分不同人员管理不同集群服务器
二、根据用户访问的终端信息显示不同页面
根据用户的user_agent显示不同页面
第一个历程: 准备架构环境
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
第二个历程: 编写负载均衡配置文件
[root@lb01 /etc/nginx/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.test.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;
}
}
第三个历程: 打开浏览器进行测试
www.test.com/test.html
如果是iPhone显示:iphone_access 10.0.0.7
如果是谷歌浏览器显示:google_access 10.0.0.8
IE和360显示:default_access 10.0.0.9