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

两台服务器如何互相做nginx负载均衡

时间:2016-05-31 14:10:47      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:nginx 负载均衡

环境介绍

       192.168.127.131  nginx负载均衡 兼web服务器

       192.168.127.130  nginx负载均衡 兼web服务器

每台服务器上面有两个web站点分别是

www.123.com

www.hu.com 

说明:

准备用dns轮询解析来实现负载均衡,并且在两台server上面还同时启用了Nginx负载均衡,两台服务器是互为负载。前提是两台web服务端的网站内容一致。


配置过程:

在130负载均衡上面的操作:

上面编辑一个配置文件 vi /usr/local/nginx/conf/vhost/ld.conf 

##www.123.com


 upstream 123{
     server 192.168.127.130:81;      
     server 192.168.127.131:81 ;    
ip_hash;
}server {
     listen 80;
     server_name  www.123.com;
     location / {       proxy_pass http://www/;       proxy_set_header Host $host;  }
}
upstream hu {
   server 192.168.253.131:82;
   server 192.168.253.130:82;   #dir
   ip_hash;
}
server {
    listen 80;
    server_name www.hu.com;
    location / {
        proxy_pass http://aaa/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

————————————————————————————————————————————————

更改 nginx负载服务器上面各个web主机的默认端口  (红色部分字体 )

server
{
    listen 192.168.127.130:82;
    server_name www.hu.com;
    index index.html index.htm index.php;
    root /usr/local/nginx/html;
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }
}

—————————————————————————————————————————————

server
{
    listen 192.168.127.130:81 ;
    server_name www.123.com;
    index index.html index.htm index.php;
    root /data/www2;
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name;
    }
}

131上面的配置:



server
{
    listen 192.168.127.131:81 ;
    server_name www.123.com;
    index index.html index.htm index.php;
    root /data/www2;
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name;
    }
}

#####################################################################

server
{
    listen 192.168.127.131:81 ;
    server_name www.123.com;
    index index.html index.htm index.php;
    root /data/www2;
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www2$fastcgi_script_name;
    }
}

#########################################################################################

同样的在 上面编辑一个配置文件 vi /usr/local/nginx/conf/vhost/ld.conf 

##www.123.com


 upstream 123{
     server 192.168.127.130:81;      
     server 192.168.127.131:81 ;    
ip_hash;
}server {
     listen 80;
     server_name  www.123.com;
     location / {       proxy_pass http://www/;       proxy_set_header Host $host;  }
}
upstream hu {
   server 192.168.253.131:82;
   server 192.168.253.130:82;   
   ip_hash;
}
server {
    listen 80;
    server_name www.hu.com;
    location / {
        proxy_pass http://aaa/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }


最后分别启动nginx服务并查看端口是否正常启动


测试:分别在两个网站的目录里添加1.txt测试文件,里面写上不同的内容然后用浏览器访问 www.hu.com/1.txt,刷新几次

同样的,www.123.com/1.txt也是这样


本文出自 “渐行渐远” 博客,请务必保留此出处http://825536458.blog.51cto.com/4417836/1784794

两台服务器如何互相做nginx负载均衡

标签:nginx 负载均衡

原文地址:http://825536458.blog.51cto.com/4417836/1784794

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