一、nginx代理
Nginx通过upstream和proxy_pass实现了负载均衡。本质上也是Nginx的反向代理功能,只不过后端的server为多个。
Nginx负载均衡主要配置方式有:轮询、weight 、ip_hash、fair(第三方)、url_hash(第三方)
1. 新建一个负载均衡配置文件
vim /usr/local/nginx/conf/vhost/load.conf
添加以下内容:
upstream baidu_com {ip_hash; server 14.215.177.38:80; server 14.215.177.39:80;}server{ listen 80; server_name www.baidu.com; location /{ proxy_pass http://baidu_com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
以上以百度为例进行配置,ip可以通过big命令进行获取。如果没有big命令需要进行安装:yum install -y bind-utils
[root@node1 vhost]# dig www.baidu.com; <<>> DiG 9.9.4-RedHat-9.9.4-73.el7_6 <<>> www.baidu.com;; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42045;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 4096;; QUESTION SECTION:;www.baidu.com. IN A;; ANSWER SECTION:www.baidu.com. 583 IN CNAME www.a.shifen.com.www.a.shifen.com. 292 IN A 14.215.177.38www.a.shifen.com. 292 IN A 14.215.177.39;; Query time: 1 msec;; SERVER: 183.60.83.19#53(183.60.83.19);; WHEN: Thu Jun 06 16:12:43 CST 2019;; MSG SIZE rcvd: 101
2. 测试
测试访问www.baidu.com如果能通说明代理成功
curl -x 127.0.0.1:80 www.baidu.com -I