码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx负载均衡

时间:2018-01-04 18:06:07      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:ddr   后端   pass   defs   ipad   文件的   列表   out   remote   

 


环境:

  负载: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; #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 server1 {
        server 172.16.50.177:8080;
        server 172.16.50.177:8081;
    }
    upstream server2 {
        server 172.16.50.177:8082;
        server 172.16.50.177:8083;
    }
    server {
        listen 80;
        server_name aaa.com;
        access_log /var/log/nginx/aaa.com_access_log main;
        location / {
                root html;
                index index.html index.htm;

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
然后测试就行了

  

  


 

Nginx负载均衡

标签:ddr   后端   pass   defs   ipad   文件的   列表   out   remote   

原文地址:https://www.cnblogs.com/wazy/p/8194126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!