首先搭建好三台nginx,我是用VM搭建的
nginx搭建,https://www.cnblogs.com/liubaoqing/p/10507962.html
這里的三台nginx ,ip分別是 192.168.3.41(主) 192.168.3.42 192.168.3.43 安裝好后 curl 192.168.3.41 或者瀏覽器 這樣分別訪問下nginx的歡迎頁面
如果訪問不了的,記得關閉防火牆 ,或者允許80 訪
systemctl stop firewalld firewall-cmd --zone=public --add-port=80/tcp --permanent
然后編輯nginx 的歡迎頁面,做好標識,以便區分是那台服務器
vi /usr/share/nginx/html/index.html
我192.168.3.43 對應的p3, 192.168.3.41 對應的p1, 192.168.3.42 對應的p2
然后配置 41這台主服務器
vim /etc/nginx/conf.d/default.conf
upstream 192.168.3.41{ server 192.168.3.42:80 weight=1; server 192.168.3.43:80 weight=1; } server { listen 80; server_name 192.168.3.41; charset utf8; client_max_body_size 50m; client_body_buffer_size 256k; location / { proxy_pass http://192.168.3.41; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; } }
重啟nginx
service nginx restart
nginx 默認是輪詢的 ,weight=1 是權重分配 ,都是1表示平均分配,如果42這台weight =2 而 43 為1 ,表示42的權重大,訪問轉42 的多 ,還有一個是哈希算法 ip_hash;
192.168.3.42 和192.168.3.43打開nginx的配置文件default.conf , 然后server_name 改為各自的ip ,最后重啟
訪問192.168.3.41
起效果了 ,大功告成