6.14

根据用户的浏览器类型进行判断并分配不同的服务器

准备环境

[root@lb01 ~]# curl  10.0.0.7/lidao.html
this is pc website
[root@lb01 ~]# curl  10.0.0.8/lidao.html
this is mobile website

配置

创建两个池塘分别是移动端主机和默认主机 然后在下面做判断 如果浏览器是Android或IOS 为移动端放入移动端池塘 如果不是则放入默认池塘

    #gzip  on;
     upstream defaule{
     server 10.0.0.7:80 weight=2 max_fails=3 fail_timeout=20s;
     }
     upstream mobile{
     server 10.0.0.8:80 weight=2 max_fails=3 fail_timeout=20s;
     }
#    include /etc/nginx/conf.d/*.conf;
     server{
     listen 80;
     server_name www.oldboy.com;
     location / {
     if ($http_user_agent ~* "Android|IOS") { 
     proxy_pass http://mobile;
      }
     proxy_pass http://defaule  ;
     }
}   
}   

访问网站的动静分离

等缓解服务器的压力支持更高的访问量

使用不同的uri进行转发

    #gzip  on;
     upstream upload{
     server 10.0.0.7:80 weight=2 max_fails=3 fail_timeout=20s;
     }
     upstream statis{
     server 10.0.0.8:80 weight=2 max_fails=3 fail_timeout=20s;
     }
#    include /etc/nginx/conf.d/*.conf;
     server{
     listen 80;
     server_name www.oldboy.com;
     location /upload {
     proxy_pass http://upload  ;
     }
     location /statis {
     proxy_pass http://statis  ;
     }
}
}

访问upload上传的目录会自动找到10.0.0.7服务器 访问静态页面找10.0.0.8

更精确的划分 基于文件扩展名划分 静态页面

<location ~* ".(ng|jpg|jpeg|gif|bmp|html|css)$"

负载均衡轮询算法

默认轮询为顺序访问
加权轮询 weight
最小连接数 least conn 根据后端服务器连接数分配任务
ip_hash :只要客户端IP地址相同以后都被分配到同一台服务器

未完待续ing。。。。。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容