正向代理、反向代理

正向代理:

自己知道要找谁,听过中间商

反向代理:

皇上找妃子,他不知道找谁,找个太监帮忙

皇上只找太监

反向代理就是负载平衡Using nginx as HTTP load balancer

 nginx.org/en/docs

反向代理的方法:http://nginx.org/en/docs/http/load_balancing.html

round-robin:按顺序分任务(默认)

 

least-connected:哪个请求最小就分给谁任务

ip-hash:客户端的IP一样,就只往一台分。例如我刚上完京东,过几分钟再上京东,由于IP一样,不用重新登录。

 

加权:

练习:

(一)round-robin:按顺序分任务(默认)

1、先克隆虚拟机,调低内存为512M,然后都打开

 

2、逐个查IP

[root@bogon ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.88.128  netmask 255.255.255.0  broadcast 192.168.88.255

[root@bogon ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.88.131  netmask 255.255.255.0  broadcast 192.168.88.255

[root@bogon ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.88.132  netmask 255.255.255.0  broadcast 192.168.88.255

3、连接Xshell

 

4、写网页内容

(1)先找网页的根目录/var/www/html

[root@bogon ~]# vim /etc/nginx/nginx.conf

(2)在默认打开的页面上写内容

在学习1中,输入:

[root@bogon ~]# vim /var/www/html/index.html

写web1

在学习2中,输入:

[root@bogon ~]# vim /var/www/html/index.html

写web2

5、加载nginx

systemctl reload nginx

打开网页看看:

 

 

6、按照网上写的插入内容

events {     

    worker_connections 1024;

}

http {

    upstream pythonweb {

        server 192.168.88.131;

        server 192.168.88.132;

                    }

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

。。。。。。。。

        location / {

          proxy_pass http://pythonweb;

        }

7、加载nginx

[root@bogon ~]# systemctl reload nginx

[root@bogon ~]#

 

(二)least-connected方法: 哪个请求最小就分给谁任务

http {

    upstream pythonweb {

        least_conn;

        server 192.168.88.131;

        server 192.168.88.132;

                    }

(三)ip-hash:客户端的IP一样,就只往一台分。例如我刚上完京东,过几分钟再上京东,由于IP一样,不用重新登录。

http {

    upstream pythonweb {

        ip_hash;

        server 192.168.88.131;

        server 192.168.88.132;

                    }

(四)加权法:页面显示为刷新3次都是web1,才有1次web2

http {

    upstream pythonweb {

        server 192.168.88.131 weight=3;

        server 192.168.88.132;

                    }

原文地址:https://www.cnblogs.com/jensenxie/p/7860818.html