Nginx+keepalived

Nginx+keepalivedjavascript


1、環境:php


centos5.6(64位)、nginx-0.7.5一、keepalived-1.1.15   css

主nginx負載均衡器:192.168.1.205 html

輔nginx負載均衡器:192.168.1.206 java

vip:192.168.1.200node


2、安裝nginxlinux

yum install  gcc gcc-c++ zlib-devel openssl openssl-devel  gd  keyutils  patch perl mhashnginx

#添加運行nginx的用戶和組www  c++

useradd -M -s /sbin/nologin www
web

tar zxvf pcre-7.8.tar.gz  (見附件)

cd pcre-7.8/  

./configure  

make && make install  

wget http://nginx.org/download/nginx-1.4.2.tar.gz

tar zxf nginx-1.4.2.tar.gz

cd nginx-1.4.2

./configure  --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module  

make && make install


mkdir /var/log/nginx

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

user www www;
worker_processes 8;  #工做進程數,爲CPU的核心數或者兩倍
pid /usr/local/nginx/logs/nginx.pid;
events
{
use epoll;   #epoll是多路複用IO(I/O Multiplexing)中的一種方式,可是僅用於linux2.6以上內核,能夠大大提升nginx的性能
worker_connections 65535;  #單個後臺worker process進程的最大併發連接數
}
http{
include       mime.types;
default_type application/octet-stream;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/log/nginx/access.log access;
error_log /var/log/nginx/error.log;
server_names_hash_bucket_size 128;
client_header_buffer_size 128;
large_client_header_buffers 2 128k;
client_max_body_size 10m;
client_body_buffer_size 128k;
client_header_timeout 90s;
client_body_timeout 90s;
send_timeout 90s;
sendfile on;
keepalive_timeout 120;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
proxy_connect_timeout 20;
proxy_send_timeout 90;
proxy_read_timeout 40;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
##開啓gzip壓縮
gzip on;
gzip_min_length 1k;   #設置最小的壓縮值,單位爲bytes.超過設置的min_length的值會進行壓縮,小於的不壓縮.
gzip_buffers     16 64k;   #設置系統的緩存大小,以存儲GZIP壓縮結果的數據流,它能夠避免nginx頻煩向系統申請壓縮空間大小
gzip_http_version 1.1;  #識別http的協議版本(1.0/1.1)
gzip_comp_level 2;   #壓縮等級設置,1-9,1是最小壓縮,速度也是最快的;9恰好相反,最大的壓縮,速度是最慢的,消耗的CPU資源也多
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;  #和http頭有關係,加個vary頭,代理判斷是否須要壓縮
upstream www.test.com
{
ip_hash;
server 192.168.1.230:80;
server 192.168.1.201:80;
}
server {
listen 80;
server_name www.test.com;
location / {
root html ;
index index.php index.jsp index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www.test.com;
}
location /NginxStatus/ {
        stub_status on;
        allow 127.0.0.1;
        access_log on;
        auth_basic "NginxStatus";
        auth_basic_user_file /usr/local/nginx/htpasswd;
}
        error_page   500 502 503 504  /50x.html; //自定義錯誤頁面
                location = /50x.html {
                        root   html;  //錯誤頁面放在/usr/local/nginx/html/目錄下
                }
                location ~ updating.jpg { //錯誤頁面調用的圖片
                        root /usr/local/nginx/html/; //圖片位置
                }
}
}

htpasswd -c -m /usr/local/nginx/htpasswd admin    //admin爲用戶名,建立狀態認證文件。

3、安裝Keepalived,讓其分別做web及Nginx的HA


wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz  

tar zxvf keepalived-1.1.15.tar.gz  

cd keepalived-1.1.15  

./configure --prefix=/usr/local/keepalived  

make  

make install  

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/  

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/  

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/  

mkdir /etc/keepalived  

cd /etc/keepalived/  


vim keepalived.conf  

! Configuration File for keepalived
global_defs {
      router_id DR
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    mcast_src_ip 192.168.1.205  //主nginx的IP地址
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
    }
    virtual_ipaddress {
        192.168.1.200 //vip地址
    }
}


#service keepalived start

[root@nginx01 mnt]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue

   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

   inet 127.0.0.1/8 scope host lo

   inet6 ::1/128 scope host

      valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

   link/ether 00:0c:29:37:10:34 brd ff:ff:ff:ff:ff:ff

   inet 192.168.1.205/24 brd 192.168.1.255 scope global eth0

   inet 192.168.1.200/32 scope global eth0    //主DR上有VIP1.200

   inet6 fe80::20c:29ff:fe37:1034/64 scope link

      valid_lft forever preferred_lft forever

3: sit0: <NOARP> mtu 1480 qdisc noop

   link/sit 0.0.0.0 brd 0.0.0.0

輔DR的配置文件

! Configuration File for keepalived
global_defs {
      router_id DR
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    mcast_src_ip 192.168.1.206
    priority 10
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
    }
    virtual_ipaddress {
        192.168.1.200
    }
}


4、監控nginx

監控nginx進程腳本,放置在後臺一直監控nginx進程;如進程消失,嘗試重啓nginx,如是失敗則當即停掉本機的keepalived服務,讓另外一臺負載均衡器接手。

vim /root/nginx_pid.sh  

#!/bin/bash
while  :
do
 nginxpid=`ps -C nginx --no-header | wc -l`
 if [ $nginxpid -eq 0 ];then
  /usr/local/nginx/sbin/nginx
  sleep 5
   if [ $nginxpid -eq 0 ];then
   /etc/init.d/keepalived stop
   fi
 fi
 sleep 5
done


將腳本置於後臺運行

nohup /mnt/nginx.sh &


5、測試

tail -f /var/log/messages

關閉keeplived

關閉nginx

6、關閉系統沒必要要的服務

chkconfig bluetooth off

chkconfig cups off  打印服務

chkconfig isdn off 撥號上網服務

chkconfig kudzu off 硬件檢測

chkconfig sendmail off

chkconfig smartd off

chkconfig autofs off

/etc/init.d/avahi-daemon stop

chkconfig avahi-daemon off

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息