1. 程式人生 > >squid正反向代理及平衡輪詢

squid正反向代理及平衡輪詢

squid正向代理

原理:一臺不能上網的機器通過代理可以訪問不能訪問的網站
這裡我拿我的真機來做代理,虛擬機器通過代理訪問網站
真機Operating System: Red Hat Enterprise Linux Server 7.2 (Maipo)

squid安裝和配置更改

真機(可以上網)IP:172.25.254.56

 [root@dream software]# yum install -y squid                     ###下載squid
 [root@dream software]# vim /etc/squid/squid.conf
55 # And finally deny all other access to this proxy 56 http_access allow all ###允許所有人連線 57 58 # Squid normally listens to port 3128 59 http_port 3128 60 61 # Uncomment and adjust the following to add a disk cache directory. 62 cache_dir ufs /var/spool/squid 100
16 256 ###100M,/var/spool/squid中16個一級目錄,256個2級目錄 [root@dream squid]# systemctl start squid [root@dream 00]# systemctl stop firewalld

這裡寫圖片描述
如果防火牆未關:開啟3128埠訪問許可權

在虛擬機器中的瀏覽器:

IP:172.25.254.125
這裡寫圖片描述

這裡寫圖片描述

測試

瀏覽器的主機ping www.baidu.com 網路不通,但是瀏覽器能夠上網

這裡寫圖片描述

squid反向代理

這裡我用2臺虛擬機器,一臺IP:172.25.254.125 一臺IP:172.25.254.225

IP:172.25.254.125

注意:這臺虛擬機器的httpd服務要關閉

 [root@server ~]# yum install -y squid
 [root@server ~]# vim /etc/squid/squid.conf                            ###可以/usr/share/doc/squid-3.3.8/squid.conf.documented檢視幫助
  55 # And finally deny all other access to this proxy
  56 http_access allow all
  57 
  58 # Squid normally listens to port 3128
  59 http_port 80 vhost vport                                          ###通過80埠訪問,虛擬使用者,虛擬埠                               
  60 cache_peer 172.25.254.225 parent 80 0 no-query                    ###通過80訪問172.25.254.156,0沒有後備,no-query:告訴伺服器沒有後備
  61
  62 # Uncomment and adjust the following to add a disk cache directory.
  63 cache_dir ufs /var/spool/squid 100 16 256
 [root@server ~]# systemctl start squid
 [root@server ~]# systemctl stop firewalld.service

IP:172.25.254.225

 [root@mysql ~]# vim /var/www/html/index.html
 <h1>172.25.254.156</h1>
 [root@mysql ~]# systemctl start httpd 

測試

http://172.25.254.125 ###會訪問到172.25.254.225的http的預設釋出檔案

這裡寫圖片描述

平衡輪詢

IP:172.25.254.125

 vim /etc/squid/squid.conf
 55 # And finally deny all other access to this proxy
 56 http_access allow all
 57 
 58 # Squid normally listens to port 3128
 59 http_port 80 vhost vport
 60 cache_peer 172.25.254.225 parent 80 0 no-query originserver round-robin name=webserver1     ###originserver:指定name=webserver1,代表所以資訊,以round-robin(輪詢)方式工作
 61 cache_peer 172.25.254.56 parent 80 0 no-query originserver round-robin name=webserver2
 62 cache_peer_domain webserver1 webserver2 www.dream.com                                      ###www.dream.com做輪詢                 
 63 # Uncomment and adjust the following to add a disk cache directory.
 64 cache_dir ufs /var/spool/squid 100 16 256
 systemctl restart squid.service

測試

在訪問的瀏覽器的主機中/etc/hosts加入172.25.254.125 www.dream.com
瀏覽器輸入www.dream.com重新整理可發現顯示的內容會變化

這裡寫圖片描述

這裡寫圖片描述