zoukankan      html  css  js  c++  java
  • Nginx负载均衡


    环境:

      负载:192.16.1.1(MASTER),192.168.1.2(SLAVE),VIP:192.168.1.250

      web:192.16.1.3

      #因电脑条件有限,只能这么做了,用一台来模拟多个后端,

    1.Nginx安装

    http://www.cnblogs.com/wazy/p/8108824.html  #这里三台都要装

    2.keepalived安装

    http://www.cnblogs.com/wazy/p/8118625.html #这个只需要装负载的两台

    3.配置调整

      负载:(两台都这样,可能有些许要改动的地方)

      nginx:

    user  www;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log warn;
    
    pid        /usr/local/nginx/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    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  /var/log/nginx/access.log  main;
    
        sendfile        on;
        keepalive_timeout  65;
        #upstream字段,设定负载均衡的服务器列表
        upstream server1 {
            server 192.168.1.3:8080;
            server 192.168.1.3:8081;
        }
        upstream server2 {
            server 192.168.1.3:8082;  #条件有限,用一台来模拟多个后端
            server 192.168.1.3:8083;
        }
        server {
            listen 80; #侦听aaa.com的80端口
            server_name aaa.com;
            access_log /var/log/nginx/aaa.com_access_log main; #指定aaa.com的日志
            location / {
                    root html; #定义服务器的默认网站根目录位置
                    index index.html index.htm;  #定义首页索引文件的名称
                    proxy_pass http://server1;  #请求转向server1 定义的服务器列表
                    proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                    client_max_body_size 100m; #允许客户端请求的最大单文件字节数
            }
            error_page 500 502 503 504 /50x.html; #定义错误页面
            location = /50x.html {
                    root html;
            }
            location ~^/(WEB-INF)/ {
                    deny all;
            }
       }
    
       server {
            listen 80; #侦听bbb.com的80端口
            server_name bbb.com;
            access_log /var/log/nginx/bbb.com_access_log main; #指定bbb.com的日志
            location / {
                    root html;   #定义服务器的默认网站根目录位置
                    index index.html index.htm;  #定义首页索引文件的名称
                    proxy_pass http://server2;  #请求转向server2 定义的服务器列表
                    proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                    client_max_body_size 100m; #允许客户端请求的最大单文件字节数
            }
            error_page 500 502 503 504 /50x.html; #定义错误页面
            location = /50x.html {
                    root html;
            }
            location ~^/(WEB-INF)/ {
                    deny all;
            }
       } 
    }
    

      keepalived:

    #首先要写一个检测nginx是否运行的脚本
    vi /usr/local/nginx/keep/check_nginx.sh 
    #!/bin/bash
    
    echo "test" >> /usr/local/nginx/keep/check_keep.log
    if [[ ! -e /usr/local/nginx/run/nginx.pid ]];then
            ps -ef|grep keep |grep -v grep|awk -F' ' '{print $2}'|xargs kill
    else
            PID=`cat /usr/local/nginx/run/nginx.pid`
            counts=`ps -ef|grep $PID|grep -v grep|wc -l`
            if [[ $counts -eq 0 ]];then
                    ps -ef|grep keep |grep -v grep|awk -F' ' '{print $2}'|xargs kill
            fi
    fi
    #pid文件位置自己看看是否正确
    
    #然后再来设置keepalived
    ! Configuration File for keepalived
    
    global_defs {
            router id 10
    }
    
    vrrp_script check_nginx {
            script "bash /usr/local/nginx/keep/check_nginx.sh"
            interval 2
            weight -4
            fall 2
            rise 2
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 51
        priority 100
        virtual_ipaddress {
            192.168.1.250
        }
        track_script {
            check_nginx
        }
    }
    #这里priority 另一个要比这个低,一个做主,一个做备
    

      策略:

    #这里负载均衡开启80端口
    #web后端只对负载开启80端口
    -A INPUT -s 192.168.1.1/32 -m tcp -p tcp --dport 8080:8083 -j ACCEPT
    -A INPUT -s 192.168.1.2/32 -m tcp -p tcp --dport 8080:8083 -j ACCEPT
    #centos6
    
    
    firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.1.1 port port=8080 protocol=tcp accept'
    firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.1.1 port port=8081 protocol=tcp accept'
    firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.1.1 port port=8082 protocol=tcp accept'
    firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.1.1 port port=8083 protocol=tcp accept'
    #centos7
    然后测试就行了
    

      

      


  • 相关阅读:
    windows系统切换jdk,修改java_home无效情况
    Cannot instantiate interface org.springframework.context.ApplicationListener
    MySQL分组查询获取每个学生前n条分数记录(分组查询前n条记录)
    ASP.NET Web API 使用Swagger生成在线帮助测试文档,支持多个GET
    EF TO MYSQL 无法查询中文的解决方法
    HttpWebRequest post请求获取webservice void数据信息
    This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案
    MySQL 5.7.13解压版安装记录 mysql无法启动教程
    C# udpclient 发送数据断网后自动连接的方法
    汽车XX网站秒杀抢购代码
  • 原文地址:https://www.cnblogs.com/wazy/p/8194126.html
Copyright © 2011-2022 走看看