Nginx 부하 균형 설정

설치 환경
Hostname
IP
설명 하 다.
lb01
192.168.123.101
Nginx 메 인 부하 이퀄 라이저
lb02
192.168.123.102
Nginx 부하 이퀄 라이저
web01
192.168.123.103
웹 01 서버
web02
192.168.123.104
웹 02 서버
 
 
 
 
 
 
2. 각각 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

 
3. 테스트 에 사용 할 웹 서비스 설정
[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

 
4. 부하 균형 설정
#   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

 
5. 확장 내용
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;
}

 
 

좋은 웹페이지 즐겨찾기