超哥笔记--反向代理与集群实例(7)

一 反向代理

1.实验准备,准备2台nginx机器

  机器1 192.168.11.37 用作 web服务器,用作数据返回
  机器2 192.168.11.167 用作nginx反向代理服务器

在windows中访问 代理服务器,然后让代理服务器 去拿 web服务器的数据

  windows > 192.168.11.158 > 192.168.11.37

  windows < 192.168.11.158 < 192.168.11.37

 

2 服务器配置

准备机器1,只是对数据页面的一个返回

#配置信息如下
server {
        listen       80;
        server_name  192.168.11.37;

        #charset koi8-r;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

 

准备机器2,用作nginx的反向代理服务器,这个机器不存数据,只转发请求
配置如下

server {
        listen       80;
        server_name  192.168.11.158;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #在这里进行反向代理配置
        #192.168.11.158/
        location / {
        proxy_pass http://192.168.11.37; #代理服务器
            #root   html;
            #index  index.html index.htm;
        }

}

 

二  nginx的负载均衡,顾名思义,

1.集群是什么
一堆服务器做一件事

2.集群性能很高
淘宝本来的核心支付服务器是小型机,非常昂贵,且难以维护
后来都讲 服务器更换为集群架构
一堆便宜的服务器,维护者一个功能运转


3.高可用
单点机器很可能宕机
集群单机机器宕机,不会影响整体的运转

nginx负载均衡的配置

1.实验如下
准备三台机器

机器1 nginx负载均衡器(发牌的荷官) 192.168.11.158
nginx.conf配置如下

upstream nginx_pools { //负载均衡分配到各个服务器
  server 192.168.11.37 weight=10;
  server 192.168.11.167 ;
}
server {
listen 80;
server_name 192.168.11.158;

#charset koi8-r;

#access_log logs/host.access.log main;
#在这里进行反向代理配置
#192.168.11.158/
location / {
proxy_pass http://nginx_pools;  #负载均衡到nginx_pools
}
}

 

机器2   准备nginx  返回页面数据        192.168.11.37   

nginx.conf配置如下
            server {
                listen       80;
                server_name  192.168.11.37;
                location / {
                    root   /opt/jd;
                    index  index.html index.htm;
                }
                error_page  404              /40x.html;
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                    root   html;
                }
    }

 

机器3 也准备nginx 返回页面数据 192.168.11.167

server {
listen 80;
server_name 192.168.11.167;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

 

 

负载均衡分配方法

1.轮询(不做配置,默认轮询)

2.weight权重(优先级)

3.ip_hash配置,根据客户端ip哈希分配,不能和weight一起用

 

注意事项:

实验开始必须关闭防火墙
iptables -F
sed  -i 's/enforcing/disabled/' /etc/selinux/config

systemctl stop firewalld
systemctl disable firewalld

 

posted @ 2019-03-28 10:17  柳帅  阅读(178)  评论(0编辑  收藏  举报
//替换成自己路径的js文件