负载均衡实例

1.语法模块 ngx_http_upstream_module

Syntax:    upstream name { ... }
Default:    —
Context:    http

upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

 

 

2.环境准备

主机 外网IP 内网IP 身份
lb01 10.0.0.4 172.16.1.4 SLB
web01 172.16.1.7   web
web02 172.16.1.8   web

 

3.操作web01

1)配置nginx

 

 

[root@web01 ~]# vim /etc/nginx/conf.d/linux.node.com.conf
server {
    listen 80;
    server_name linux.node.com;
    charset utf-8;

    server_name linux.node.com;
    charset utf-8;

    location / {
        root /code/node;
        index index.html;
    }
}

 

 

[root@web01 ~]# mkdir -p  /code/node
[root@web01 ~]# echo "我是web01......" > /code/node/index.html

2)配置hosts

 

 

 

 

 

 3)重启

 

 

[root@web01 ~]# systemctl restart nginx

4)访问

 

 

4.操作web02

 

 

[root@web02 ~]# vim /etc/nginx/conf.d/linux.node.com.conf
server {
    listen 80;
    server_name linux.node.com;
    charset utf-8;

    location / {
        root /code/node;
        index index.html;
    }
}

 

 

[root@web03 conf.d]# mkdir /code/node
[root@web03 conf.d]# echo "我是web03......" > /code/node/index.html

 

 

 

 

[root@web02 ~]# systemctl restart nginx

 

 

5.配置负载均衡配置文件

 

 

[root@lb01 ~]# vim /etc/nginx/conf.d/node_proxy.conf 
upstream web {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}

server {
    listen 80;
    server_name linux.node.com;

    location / {
        proxy_pass http://web;
        include proxy_params;
    }
}

 

6.配置优化文件

 

 

[root@lb01 ~]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

 

 

[root@lb01 ~]# systemctl restart nginx

7.配置本地hosts

 

 

 

 

8.访问页面测试

点击F5会转到web01,再点击会变成web02,就实现了负载均衡

 

 

posted @ 2020-08-31 20:46  六月OvO  阅读(179)  评论(0编辑  收藏  举报