nginx学习

节点规划:

master:172.16.23.120    node1:172.16.23.121   node2:172.16.23.122    zabbix:172.16.23.131

以master节点作为nginx负载均衡进行配置如下:

[root@master conf]# cat nginx.conf|grep upstream -C 20|egrep -v "#|^$"
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream backend { 
        server node1;
        server node2;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://backend; 
        }

 然后测试访问为均衡访问:

[root@master conf]# for i in `seq 1 4`;do curl http://master;done
node1
node2
node1
node2

 现在将upstream里面的后端机器进行backup主备模式进行配置:

[root@master conf]# cat nginx.conf|grep upstream -C 20|egrep -v "#|^$"
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream backend { 
        server node1;
        server node2 backup;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://backend; 
        }

 看上面标红的设置,将node2设置为backup,这样设置的目的是:当node1和node2服务都正常的话,backup模式也就是node2不会进行提供服务,而当node1服务异常后,node2也就是backup备才会进行提供服务

[root@master conf]# for i in `seq 1 4`;do curl http://master;done
node1
node1
node1
node1

 现在将node1进行停止,然后再访问master:

[root@node1 nginx]# ./sbin/nginx -s stop
[root@node1 nginx]# ps -ef|grep nginx
root      82341 125945  0 15:32 pts/0    00:00:00 grep --color=auto nginx

 现在进行访问测试:

[root@master conf]# for i in `seq 1 4`;do curl http://master;done
node2
node2
node2
node2

 而当node1服务恢复正常后,访问master后继续node1而不再是node2备节点了

 

下面将展示两个机房的访问关系:

master:nginx负载均衡,后端挂两个服务:node1,node2

zabbix-server:nginx负载均衡,后端挂两个服务:node1,node2

node3:nginx负载均衡,后端挂master以及zabbix-server

具体配置如下:

由node3进行最外层的提供服务,涉及两个idc,node1在idc1机房,node2在idc2机房

[root@node3 conf]# cat nginx.conf|grep upstream -C 30|egrep -v "#|^$"
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend { 
        server master;
        server zabbix-server;
    }
    
    upstream idc1 {
        server master;
    }
    upstream idc2 {
        server zabbix-server;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_set_header X-Real-IP $remote_addr;
            if ($remote_addr ~ "172.16.23.121")
            {
                proxy_pass http://idc1;
                break;
            }
            if ($remote_addr ~ "172.16.23.122")
            {
                proxy_pass http://idc2;
                break;
            }
            proxy_pass http://backend;
        }

 而master配置如下,master优先访问node1,也就是idc1机房,idc2机房作为backup,当idc1挂掉后才会访问idc2也就是node2,backup关键字的作用

[root@master conf]# cat nginx.conf|grep upstream -C 30|egrep -v "#|^$"
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream backend { 
        server node1;
        server node2 backup;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

 而zabbix-server的配置,zabbix-server优先访问idc2机房的node2,idc1的node1作为backup,当idc2的node2出现异常才会访问idc1的node1服务

[root@zabbix-server conf]# cat nginx.conf|grep upstream -C 20|egrep -v "#|^$"
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend { 
        server node1 backup;
        server node2;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://backend;
        }

 以上配置完成后在node1所属idc1机房进行访问http://node3:

[root@node1 nginx]# curl http://node3
node1
[root@node1 nginx]# curl http://node3
node1

 在node2所属机房idc2进行访问:

[root@node2 html]# curl http://node3
node2
[root@node2 html]# curl http://node3
node2

 现在将node1的服务进行stop,然后在node1进行访问,也就是idc1不能提供服务,那么会去访问idc2机房的node2应用:

[root@node1 nginx]# ./sbin/nginx -s stop
[root@node1 nginx]# ps -ef|grep nginx
root      84131 125945  0 18:06 pts/0    00:00:00 grep --color=auto nginx
[root@node1 nginx]# curl http://node3
node2
[root@node1 nginx]# curl http://node3
node2

 而当idc1机房的node1服务正常恢复后,那么访问依然会回到idc1机房的node1应用:

[root@node1 nginx]# ./sbin/nginx
[root@node1 nginx]# ps -ef|grep nginx
root      84145      1  0 18:07 ?        00:00:00 nginx: master process ./sbin/nginx
nobody    84146  84145  0 18:07 ?        00:00:00 nginx: worker process
root      84148 125945  0 18:07 pts/0    00:00:00 grep --color=auto nginx
[root@node1 nginx]# curl http://node3
node1
[root@node1 nginx]# curl http://node3
node1

 

nginx根据Ip或者ip网段转发到指定的后端服务器

[root@node3 conf]# cat nginx.conf|grep upstream -C 30|egrep -v "#|^$"
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend { 
        server master;
        server zabbix-server;
    }
    
    upstream idc1 {
        server master;
    }
    upstream idc2 {
        server zabbix-server;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_set_header X-Real-IP $remote_addr;
            if ($remote_addr ~ "172.16.23.121")
            {
                proxy_pass http://idc1;
                break;
            }
            if ($remote_addr ~ "172.16.23.122")
            {
                proxy_pass http://idc2;
                break;
            }
            proxy_pass http://backend;
        }

 

posted on 2020-10-11 18:09  wadeson  阅读(222)  评论(0编辑  收藏  举报