keepalived +nginx 配置

#MASTER  config:  172.16.20.116

[root@localhost conf]# more /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     songshuyou@opzoon.com
   }
   notification_email_from songshuyou@opzoon.com
   smtp_server mail.opzoon.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script check_nginx {
   script "/etc/keepalived/nginx_pid.sh"
   interval 2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens192
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    notify_master "/etc/keepalived/master.sh"
    notify_backup "/etc/keepalived/backup.sh"
    notify_fault "/etc/keepalived/fault.sh"
   
   track_script {
     check_nginx
   }
    
    virtual_ipaddress {
        172.16.20.118/22 dev ens192
   }
}

#BACKUP CONFIG: 172.16.20.117

[root@localhost keepalived]# more keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     songshuyou@opzoon.com
   }
   notification_email_from face-test@opzoon.com
   smtp_server mail.opzoon.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script check_nginx {
   script "/etc/keepalived/nginx_pid.sh"
   interval 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 80
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    notify_master "/etc/keepalived/master.sh"
    notify_backup "/etc/keepalived/backup.sh"
    notify_fault "/etc/keepalived/fault.sh"

    track_script {
     check_nginx
   }    
    virtual_ipaddress {
        172.16.20.118/22 dev ens192
   }

}


[root@localhost keepalived]# more master.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-nginx-state.log
echo "[Master]" >> $LOGFILE
date >> $LOGFILE
[root@localhost keepalived]# more backup.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-nginx-state.log
echo "[Backup]" >> $LOGFILE
date >> $LOGFILE
[root@localhost keepalived]# more fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-nginx-state.log
echo "[Fault]" >> $LOGFILE

date >> $LOGFILE




[root@localhost keepalived]# more nginx_pid.sh
#!/bin/bash
#while :
#do
nginxpid=`lsof -i:80 |wc -l`
if [ $nginxpid -eq 0 ];then
   /usr/bin/systemctl restart nginx.service
#   sleep 5
   nginxpid=`lsof -i:80 |wc -l`
   echo $nginxpid
     if [ $nginxpid -eq 0 ];then
          /usr/bin/systemctl stop keepalived.service
     fi
fi
#sleep 5
#done

##############################nginx  config############################

[root@localhost opt]# vim /opt/opzoonface/openresty/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#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  65536;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    tcp_nodelay on;
    client_body_buffer_size  512k;
    proxy_connect_timeout    5;
    proxy_read_timeout       60;
    proxy_send_timeout       5;
    proxy_buffer_size        16k;
    proxy_buffers            4 64k;
    proxy_busy_buffers_size 128k;
    proxy_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;

    proxy_temp_path     /home/data/tmp;
    proxy_cache_path    /home/data/cache   levels=1:2   keys_zone=cache_one:200m inactive=2d max_size=30g;
    worker_connections  65536;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    tcp_nodelay on;
    client_body_buffer_size  512k;
    proxy_connect_timeout    5;
    proxy_read_timeout       60;
    proxy_send_timeout       5;
    proxy_buffer_size        16k;
    proxy_buffers            4 64k;
    proxy_busy_buffers_size 128k;
    proxy_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;

    proxy_temp_path     /home/data/tmp;
    proxy_cache_path    /home/data/cache   levels=1:2   keys_zone=cache_one:200m inactive=2d max_size=30g;
    upstream tomcatsite {
       ip_hash;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    tcp_nodelay on;
    client_body_buffer_size  512k;
    proxy_connect_timeout    5;
    proxy_read_timeout       60;
    proxy_send_timeout       5;
    proxy_buffer_size        16k;
    proxy_buffers            4 64k;
    proxy_busy_buffers_size 128k;
    proxy_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;

    proxy_temp_path     /home/data/tmp;
    proxy_cache_path    /home/data/cache   levels=1:2   keys_zone=cache_one:200m inactive=2d max_size=30g;
    upstream tomcatsite {
       ip_hash;
       server 172.16.20.116:8080;
       server 172.16.20.117:8080;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://tomcatsite;
    }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

     server{
        listen 9090;
        server_name localhost;

        client_max_body_size 1024M;
        keepalive_timeout   1800s;
        location / {
            root /home/data/pic;
            client_body_temp_path  /home/data/client_temp;

            dav_methods  PUT DELETE;
            create_full_put_path   on;
            dav_access             group:rw  all:r;

            limit_except  GET {
                allow   all;
                #deny all;
            }
        }
        access_log off;
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值