NGINX reverse proxy + keepalived high availability

Node 11 12 do keepalived nginx

node 13 14 httpd

#Install keepalived configuration 
yum -y install keepalived 
#Use yum to install nginx configuration 
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx .noarch.rpm 
yum -y install nginx 

#install apache 
yum -y install httpd
[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.83.11 centos1
192.168.83.12 centos2
192.168.83.13 centos3
192.168.83.14 centos4
建立秘钥
 ssh-keygen 
 ssh-copy-id centos2
 ssh-copy-id centos3
 ssh-copy-id centos4
[root@centos1 ~]# scp /etc/hosts centos2:/etc/hosts
hosts                                                                             100%  246   235.3KB/s   00:00    
[root@centos1 ~]# scp /etc/hosts centos3:/etc/hosts
hosts                                                                             100%  246   220.2KB/s   00:00    
[root@centos1 ~]# scp /etc/hosts centos4:/etc/hosts
hosts  

NGINX load balancing

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    upstream apacheserver{
    server 192.168.108.40; 
    server 192.168.108.50; 
} 
server{ 
    listen 80; 
    server_name www.123.com; 
    location / { 
    proxy_pass http://apacheserver; 
    root html; 
    index index.html index.htm; 
} 
} 
     include /etc/ nginx/conf.d/*.conf; #Add above this will take effect 
}
global_defs { 
    notification_email { # When the keepalived service goes down abnormally, multiple notification emails can be sent to 
      [email protected] # Recipient mailbox 1 
      [email protected] # Recipient mailbox 2 
      [email protected] # Receive Sender's mailbox 3 
    } 
    notification_email_from [email protected] #Email sender 
    smtp_ server 192.168.83.12 #The ip address of the main server. Mail server address 
    smtp_connect_timeout 30 # Timeout 
    router_id LVS_DEVEL # The machine ID is unique within the LAN. The LVS_DEVEL field is viewed in the /etc/hosts file; access to the host through it 
} 
vrrp_script chk_http_port { 
    script "/usr/local/src/nginx_check.sh" #Detection script storage path 
    interval 2 #Detection script execution interval, That is, the detection script will be automatically executed every 2s. 
    weight 2 #weight, if the script detection is true, the server weight will be +2 
} 
vrrp_instance VI_1 {
    state MASTER # Specify the role of keepalived, MASTER is the master, and BACKUP is the backup. On the backup server, you need to change MASTER to BACKUP 
    interface ens33 # The communication port can be seen through ip addr, configure virtual_router_id according to your own machine 
    51 # vrrp instance id The instance id of the keepalived cluster must be consistent, that is, the virtual_router_id of the master and backup machines must have the same 
    priority 90 #Priority, the larger the value, the higher the priority of the request for processing. The main machine and the standby machine take different priorities, the main machine value is larger, and the backup machine > 
value is smaller 
    advert_int 1 #heartbeat interval, the default is 1s. The keepalived multi-machine cluster detects whether the current server is still working normally through the heartbeat. If 
there is no response to sending the heartbeat, the backup server will take over immediately; 
    authentication { #Communication password between servers 
        auth type PASS #Set the authentication type and password, MASTER and BACKUP must use The same password can communicate normally 
        auth pass 1111 
    } 
    virtual_ipaddress { # Custom virtual IP. The custom virtual ip must be set according to the real ip. For example, the real ip is 192.168.91.138, then the virtual ip can 
be set to 192.168.91.139~255, and the first three numbers are consistent 
        192.168.83.100 #Define virtual ip (VIP), can be set more, one per line
        192.168.83.200 # Define virtual ip (VIP), you can set more, one per line 
    } 
}

test

Guess you like

Origin blog.csdn.net/m0_67849390/article/details/130192238