码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx 负载均衡

时间:2018-02-08 20:18:21      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:测试   实验   cal   rem   host   post   sbin   ref   自定义   

Nginx 负载均衡

说明

  • Nginx 基于OSI七层(应用层)
  • 使用Nginx 反向代理 实现负载均衡
  • Nginx负载均衡 如果某台server down机 Nginx不会去访问,自动跳转到正常的机器上去。

实验环境

  • 三台主机:Linux Centos 6.4 32位
  • 调度器Director:192.168.1.160(公网IP)、192.168.1.100(VIP)
  • HTTP真实服务器Real server1:192.168.1.115(公网IP)
  • HTTP真实服务器Real server2:192.168.1.111(公网IP)

实验操作

1、配置Nginx主配置文件、http{}内加入

vim /usr/local/nginx/conf/nginx.conf

技术分享图片
http{
# upstream反向代理 xsk 自定义名字
upstream xsk{
  # server代理的IP:端口 可加入多个 端口默认为80
  # weight权重值 权重范围 0 ~ 100
  server 192.168.1.111 weight=2;
  server 192.168.1.115 weight=1;
}
server {
     # 代理端监听端口
     listen 80;
     # 代理端访问的域名
     server_name www.dir.com;
     location / {
          # 指定http://name/ 要与upstream name 对应一致
          proxy_pass    http://xsk/;
          proxy_set_header Host    $host;
          proxy_set_header X-real-IP   $remote_addr;
          proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
 }}}
主配置文件

2、检测配置文件

技术分享图片
命令:/usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
测试配置文件

3、重启Nginx服务

/etc/init.d/nginx restart
4、测试Nginx负载均衡
技术分享图片
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 2
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 2
测试 加权轮询
技术分享图片
测试 其中一个web故障,就不访问

[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
[root@nginx conf]# curl -xlocalhost:80 www.dir.com
LVS 1
测试 单点故障

 

Nginx 负载均衡

标签:测试   实验   cal   rem   host   post   sbin   ref   自定义   

原文地址:https://www.cnblogs.com/xiangsikai/p/8432566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!