Linux环境下搭建Nginx实现动静分离的负载均衡集群

Nginx 的 upstream 负载的5种方式,目前最常用前3 种方式 1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除 2)、weight 指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况 3)、IP_hash 每个请求按访问 IP 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题 4)、fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配 5)、url_hash(第三方) 按访问url的hash结果来分配请求,使同样的url定向到同一个后端服务器,后端服务器为缓存时比较有效

用户可以通过访问代理服务器去访问网站的内容

ip主机名角色
192.168.88.67server67nginx代理服务器
192.168.88.69server69apache服务器
192.168.88.70server70apache服务器

搭建nginx代理服务器

[root@server67 yum.repos.d]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
[root@server67 yum.repos.d]# cd /usr/local/src/
[root@server67 src]# rz
​
[root@server67 src]# ll
总用量 816
-rw-r--r-- 1 root root 833473 11月 29 16:47 nginx-1.8.1.tar.gz
[root@server67 src]#  tar -zxvf nginx-1.8.1.tar.gz
[root@server67 src]# cd /usr/local/src/nginx-1.8.1/
[root@server67 nginx-1.8.1]# ./configure --prefix=/usr/local/nginx  --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module  --with-http_mp4_module
#启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法) 
#启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
#启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
#启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
#启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
#启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)
[root@server67 nginx-1.8.1]# make && make install
[root@server67 nginx-1.8.1]# useradd -u 1800 -s /sbin/nologin nginx
[root@server67 nginx-1.8.1]# id nginx
uid=1800(nginx) gid=1800(nginx) 组=1800(nginx)
[root@server67 nginx-1.8.1]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  #启动nginx
[root@server67 nginx-1.8.1]# netstat -pantul | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12563/nginx: master
[root@server67 nginx-1.8.1]# echo '/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf & ' >> /etc/rc.local ##加入开机启动项
[root@server67 nginx-1.8.1]# cd /usr/local/nginx/conf
[root@server67 conf]# mv nginx.conf nginx.conf.bak
[root@server67 conf]# grep -v "#" nginx.conf.bak > nginx.conf   #配置nginx成为代理服务器
[root@server67 conf]# vim nginx.conf
(1)在首行添加 user nginx nginx;
(2)在location / { } 中添加以下内容  #定义分发策略
location / {
            root   html;
            index  index.html index.htm;
     
        if ($request_uri ~* \.html$){
                   proxy_pass http://htmlservers;
           }   
        if ($request_uri ~* \.php$){
                   proxy_pass http://phpservers;
           }   
                   proxy_pass http://picservers;
 
      }
(3)最末尾添加服务器组
upstream  htmlservers {  
         server 192.168.88.69:80;  
         server 192.168.88.70:80;  
 }
 upstream  phpservers {
         server 192.168.88.69:80;
         server 192.168.88.70:80;
​
 }
 upstream  picservers {
         server 192.168.88.69:80;
         server 192.168.88.70:80;
​
 }
 
[root@server67 conf]# /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
[root@server67 conf]# cat nginx.conf
user nginx nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
​
​
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
​
    keepalive_timeout  65;
​
​
    server {
        listen       80;
        server_name  localhost;
​
​
​
        location / {
            root   html;
            index  index.html index.htm;
            
            if ($request_uri ~* .html$){
                                         proxy_pass http://htmlservers;
                                       }  
               if ($request_uri ~* .php$){
                   proxy_pass http://phpservers;
           }  
                   proxy_pass http://picservers;
}
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }            
upstream  htmlservers {  
         server 192.168.88.69:80;  
         server 192.168.88.70:80;
 }
 upstream  phpservers {
         server 192.168.88.69:80;
         server 192.168.88.70:80;
​
 }
 upstream  picservers {
         server 192.168.88.69:80;
         server 192.168.88.70:80;
 }
}
[root@server67 conf]# /usr/local/nginx/sbin/nginx -s reload
​
注:
Nginx负载的5种策略设置方法:
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 
upstream backserver { 
server 192.168.88.69; 
server 192.168.88.70; 
} 
 
2、指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 
upstream backserver { 
server 192.168.88.69 weight=1; 
server 192.168.88.70 weight=2; 
} 
 
3、IP绑定 IP_hash
每个请求按访问IP的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 
upstream backserver { 
IP_hash; 
server 192.168.88.69:80; 
server 192.168.88.70:80; 
} 
 
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。 
upstream backserver { 
server server1; 
server server2; 
fair; 
} 
 
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 
upstream backserver { 
server squid1:3128; 
server squid2:3128; 
hash $request_uri; 
hash_method crc32; 
}

配置后端服务器: server69

[root@server69 yum.repos.d]# yum install -y httpd php
[root@server69 yum.repos.d]# echo 192.168.88.69 > /var/www/html/index.html
[root@server69 html]# cat index.php 
192.168.88.69-php
<?php
phpinfo();
?>
[root@server69 html]# ll
总用量 756
-rw-r--r-- 1 root root     14 11月 29 18:58 index.html
-rw-r--r-- 1 root root     38 11月 29 18:58 index.php
-rw-r--r-- 1 root root 763002 11月 29 18:59 test.png

配置后端服务器: server70

[root@server70 html]# yum install -y httpd php
[root@server70 html]# echo 192.168.88.70 > /var/www/html/index.html
[root@server70 html]# cat >> /var/www/html/test.php <<eof
192.168.88.70-php
<?php
phpinfo();
?>
eof
[root@server70 html]# ll
总用量 36
-rw-r--r-- 1 root root    14 11月 29 19:17 index.html
-rw-r--r-- 1 root root    37 11月 29 19:17 index.php
-rw-r--r-- 1 root root 28345 11月 29 19:00 test.png

实现

在浏览器访问代理服务器:http://192.168.88.67/index.html

在浏览器访问代理服务器:http://192.168.88.67/index.php

在浏览器访问代理服务器:http://192.168.88.67/test.png

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值