一、安装环境

Hostname IP 说明
lb01 192.168.123.101 Nginx 主负载均衡器
lb02 192.168.123.102 Nginx 辅负载均衡器
web01 192.168.123.103 web01 服务器
web02 192.168.123.104 web02 服务器

 

 

 

 

 

 

二、分别在 4 台机器上安装 Nginx

#!/bin/bash
yum install -y pcre pcre-devel openssl openssl-devel gcc gcc-c++ ncurses-devel perl wget
useradd nginx -s /sbin/nologin -M
iptables -F
setenforce 0
cd /usr/local/src
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar xf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ..
/usr/local/nginx/sbin/nginx

 

三、配置用于测试的 Web 服务

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  1;    events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" ;
    access_log  logs/access.log  main;
    server {
        listen       80;
        server_name  bbs.abc.com;       
        location / {
            root   html/bbs;       
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.abc.com;           
        location / {
            root   html/www;                
            index  index.html index.htm;
        }
    }   
}

mkdir /usr/local/nginx/html/{www,bbs}   /usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
echo "www.abc.com web01" >> /usr/local/nginx/html/www/index.html
echo "bbs.abc.com web01" >> /usr/local/nginx/html/bbs/index.html
echo "192.168.123.103 www.abc.com" >> /etc/hosts
echo "192.168.123.103 bbs.abc.com" >> /etc/hosts

mkdir /usr/local/nginx/html/{www,bbs}  /usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
echo "www.abc.com web02" >> /usr/local/nginx/html/www/index.html
echo "bbs.abc.com web02" >> /usr/local/nginx/html/bbs/index.html
echo "192.168.123.104 www.abc.com" >> /etc/hosts
echo "192.168.123.104 bbs.abc.com" >> /etc/hosts

 

四、配置负载均衡

# 在 lb01 上配置 Nginx 负载均衡,代理 www.abc.com,节点为 web01 和 web02[root@lb01 ~]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;    upstream www_server_pools {                  # 定义 Web 服务器池,包含两个节点
        server 192.168.123.103:80 weight=1;
        server 192.168.123.104:80 weight=1;
    }
    server {
        listen 80;
        server_name www.abc.com;        location / {                            # 访问 www.abc.com,请求发送给 www_server_pools 里面的节点
            proxy_pass http://www_server_pools;
           include proxy.conf                  # 包含代理配置文件        }
   } }

[root@lb01 conf]# cat proxy.conf
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

/usr/local/nginx/sbin/nginx -/usr/local/nginx/sbin/nginx -s reload
echo "192.168.123.101 www.abc.com" >> /etc/hosts

# 测试[root@lb01 ~]# curl www.abc.com
www.abc.com web01
[root@lb01 ~]# curl www.abc.com
www.abc.com web02
[root@lb01 ~]# curl www.abc.com
www.abc.com web01
[root@lb01 ~]# curl www.abc.com
www.abc.com web02

 

五、扩展内容

location /static/ {         
    proxy_pass http://static_pools;
    include proxy.conf;
}
location /upload/ {
    proxy_pass http://upload_pools;    
    include proxy.conf;
}

location / {
    if ($http_user_agent ~* "MSIE") {     
        proxy_pass http://static_pools;    
    }
    if ($http_user_agent ~* "Chrome") {    
        proxy_pass http://upload_pools;
    }
    proxy_pass http://default_pools;       
    include proxy.conf
}

location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
    proxy_pass http://static_pools;        
    include proxy.conf;
}
location ~ .*.(php|php3|php5)$ {
    proxy_pass http://dynamic_pools;       
    include proxy.conf;
}