欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

nginx+keeplive

最编程 2024-08-02 18:33:36
...

nginx master 192.168.0.5

nginx backup 192.168.0.6

nginx_vip 192.168.0.100

web1 192.168.0.7

web2 192.168.0.8


nginx_master和nginx_backup 安装代码

tar -zxvf pcre-8.31.tar.gz  安装pcre 让安装Nginx支持rewrite
cd pcre-8.31
./configure
make
make install
添加用户组
groupadd www
useradd -g www www
tar -zxvf nginx-0.8.55.tar.gz
cd nginx-0.8.55
mkdir -p /usr/local/nginx
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module
make
make install

安装过程中出现

/usr/local/nginx/sbin/nginx: error whileloading shared libraries: libpcre.so.1: cannot open shared object file: No suchfile or directory

解决办法:

ldd$(which /usr/local/nginx/sbin/nginx)

linux-gate.so.1 =>(0x008a8000)

libpthread.so.0 => /lib/libpthread.so.0 (0x00643000)

libcrypt.so.1 => /lib/libcrypt.so.1 (0x06806000)

libpcre.so.1 => not found

libssl.so.6 => /lib/libssl.so.6 (0x062e5000)

libcrypto.so.6 => /lib/libcrypto.so.6(0x00ae2000)

libdl.so.2 => /lib/libdl.so.2 (0x0063d000)

libz.so.1 => /usr/lib/libz.so.1 (0x0065d000)

libc.so.6 => /lib/libc.so.6 (0x004cc000)

/lib/ld-linux.so.2 (0x004ae000)

libgssapi_krb5.so.2 =>/usr/lib/libgssapi_krb5.so.2 (0x06236000)

libkrb5.so.3 => /usr/lib/libkrb5.so.3 (0x06052000)

libcom_err.so.2 => /lib/libcom_err.so.2 (0x00c41000)

libk5crypto.so.3 => /usr/lib/libk5crypto.so.3 (0x0612b000)

libresolv.so.2 => /lib/libresolv.so.2(0x00a06000)

libkrb5support.so.0 => /usr/lib/libkrb5support.so.0 (0x06212000)

libkeyutils.so.1 => /lib/libkeyutils.so.1 (0x00de8000)

libselinux.so.1 => /lib/libselinux.so.1 (0x006c5000)

libsepol.so.1 => /lib/libsepol.so.1(0x0067d000)

解决办法:(启动缺少libpcre.so.1块)ln -s/usr/local/lib/libpcre.so.1 /lib/ 32位

ln -s/usr/local/lib/libpcre.so.1 /lib64/ 64位

安装完之后发现一个模块忘记安装了

解决办法

/usr/local/nginx/sbin/nginx -V  查看已安装模块情况
cd /home/nginx-0.8.55
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -V

如果不行的话,请高手指点

MASTER和BACKUP都这样安装

下面是NGINX.CONF

user  www www;
worker_processes  2;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  51200;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60;
    tcp_nodelay on;
                                                                                      
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types    text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
                                                                                      
    upstream backend
    {
        ip_hash;
        server 192.168.0.7:80;
        server 192.168.0.8:80;
    }
    server {
        listen       80;
        server_name  www.zyg.com;
        location / {
            root   /var/www/html;
            index  index.html index.htm index.php;
            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://backend;
        }
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location /nginx {
            access_log off;
            stub_status on;
        }
                                                                                          
    }
}

nginx安装完毕

开始安装keepalived

tar -zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
mkdir -p /usr/local/keepalived
./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/

配置文件

[root@localhost logs]# cat /etc/keepalived/keepalived.conf
!Confiuration File for keeplived
global_defs {
   notification_email {
        zyg455178278@sina.com
   }
   notification_email_from keepalived@ chtopnet.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   route_id LVS_DEVEL
}
vrrp_instance VI_1 {
   state BACKUP
   interface eth0
   virtual_router_id 51
   mcast_src_ip 192.168.0.5
   priority 100
   advert_int 1
   authentication {
     auth_type PASS
     auth_pass chtopnet
   }
   virtual_ipaddress {
     192.168.0.100
   }
}

日志文件

Nov 15 23:04:25 localhost Keepalived_vrrp: Configuration is using : 62250 Bytes
Nov 15 23:04:25 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Nov 15 23:04:25 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
Nov 15 23:04:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) forcing a new MASTER election
Nov 15 23:04:27 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Nov 15 23:04:28 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Nov 15 23:04:28 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Nov 15 23:04:28 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.0.100
Nov 15 23:04:28 localhost avahi-daemon[3695]: Registering new address record for 192.168.0.100 on eth0.
Nov 15 23:04:33 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.0.100

查看ipaddr是否有VIP

[root@localhost logs]# ip addr
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:09:32:67 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.5/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.100/32 scope global eth0
    inet6 fe80::20c:29ff:fe09:3267/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

最后模拟测试故障通过

推荐阅读