Nginx(負載平衡)+keepalived(雙機熱備)

來源:互聯網
上載者:User

標籤:


實驗情境:


負載平衡:
LB主:192.168.1.1
LB從;192.168.1.2
VIP:192.168.1.3

web伺服器:
web1:192.168.1.4
web2:192.168.1.5

一,      安裝nginx依賴: 查看是否已經安裝
 rpm -qa |grep gcc  openssl-devel pcre-devel  zlib-devel

二,    分別在主從伺服器上安裝nginx

#tar -zxf nginx-1.1.2.tar.gz
#./configure

#make && make install
修改# vim /usr/local/nginx/conf/nginx.conf
worker_processes  32; (32是你物理機記憶體的二倍)
在http {  } 裡添加

         upstreammyserver{

         server 192.168.1.4;  //web1伺服器位址

         server 192.168.1.5; //web2伺服器位址

}

    server {

                listen      80;

                server_name  myserver;

                #charset koi8-r;

                #access_log  logs/host.access.log  main;

  location / {

              proxy_passhttp://myserver;

               proxy_next_upstream http_500 http_503error timeout invalid_header;

               include /usr/local/nginx/conf/proxy.conf;(這個檔案要自己建立)

                #root  html;

                #index index.html index.htm;

        }

 

建立 touch /usr/local/nginx/conf/proxy.conf
[[email protected] conf]# cat   proxy.conf

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;

client_body_buffer_size 128k;

proxy_connect_timeout 90;

proxy_send_timeout 90;

proxy_read_timeout 90;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;


三、驗證效果

            判斷設定檔是否正確 

#/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /usr/local/nginx/conf/nginx.conf test is successful

更改配置重啟nginx

kill -HUP 主進程號或進程號檔案路徑

或者使用

cd /usr/local/nginx/sbin

./nginx -s reload

          分別在兩台web伺服器上安裝nginx

Web1伺服器上 echo service1 > /usr/local/nginx/html/index.html

Web2伺服器上echo web2 > /usr/local/nginx/html/index.html



四 、問題總結:

問題 :  [error] 33249#0: *46 no live upstreams whileconnecting to upstream, client: 10.16.21.118, server: myserver, request:"GET /favicon.ico HTTP/1.1", upstream: "http://myserver/favicon.ico",host: "10.16.21.20", referrer: "http://10.16.21.20/"
解決方案

1.  查看nginx錯誤記錄檔,檢查nginx.conf 設定檔中配置是否正確

2. Iptables –L關閉防火牆  setenforce 0


分別在主從伺服器上安裝keepalive

//本文參照http://www.open-open.com/lib/view/open1453698551808.html

#tar -zxvfkeepalived-1.2.7.tar.gz

# yum -y install kernel-headers kernel-devel 

#./configure  --prefix=/usr/local/keepalived --sysconfdir=/etc/

--with-kernel-dir=/usr/src/kernels/2.6.32-573.12.1.el6.x86_64

#make && make install
#ln -s /usr/local/sbin/keepalived /sbin/

#chkconfig keepalived on

#vim /etc/keepalived/keepalived.conf (將原來的設定檔備份後修改)

1. global_defs {  

2.     router_id NodeB  

3. }  

4. vrrp_instance VI_1 {  

5.     state Master    #設定為主伺服器  

6.     interface eth0  #監測網路介面  

7.     virtual_router_id 51  #主、備必須一樣  

8.     priority 90   #(主、備機取不同的優先順序,主機值較大,備份機值較小,值越大優先順序越高)  

9.     advert_int 1   #VRRP Multicast廣播周期秒數  

10.    authentication {  

11.     auth_type PASS  #VRRP認證方式,主備必須一致  

12.    auth_pass 1111   #(密碼)  

13. }  

14.virtual_ipaddress {  

15.     192.168.1.3/24  #VRRP HA虛擬位址  

16.}  

啟動keepalived服務

# service keepalived start 或者keepalived -D -f/usr/local/etc/keepalived/keepalived.conf


#ip a

eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

   link/ether 00:0c:29:a8:ff:bb brd ff:ff:ff:ff:ff:ff

   inet 192.168.1.1/24 brd 10.16.21.255 scope global eth0

   inet 192.168.1.3/24 scope global secondary eth0    ///vip地址

   inet6 fe80::20c:29ff:fea8:ffbb/64 scope link

      valid_lft forever preferred_lft forever


 

 

 

Nginx(負載平衡)+keepalived(雙機熱備)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.