Setp0 要實現什么
架設一臺master
代理主機,由這臺機器統籌管理多臺子代理機器,這樣客戶端只需要統一維護一個master
機器即可。
Step1 Squid的基本安裝和配置
Squid是一款出色的緩存代理服務器,也用作正向和反向代理,同時支持橫向分布式擴展,所以在這里選擇squid作為master搭建代理集群
Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-requested web pages. Squid has extensive access controls and makes a great server accelerator. It runs on most available operating systems, including Windows and is licensed under the GNU GPL.
在ubuntu下,squid的安裝十分簡單,只需要執行下面這個命令即可:
sudo apt-get install squid3
Squid的配置在/etc/squid/squid.conf
下面,只需要改變下面幾個配置即可。
#允許的客戶端ip
acl allcomputers src 0.0.0.0/0.0.0.0
#將http_access deny all注釋或者刪除
#http_access deny all
http_port 12345
如果要為你的代理服務器設置訪問權限(用戶名和密碼驗證),那么添加以下配置:
#配置用戶名密碼,后面會生成passwords文件
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated allcomputers
生成密鑰文件:
sudo apt-get install apache2-utils
sudo htpasswd -c -d /etc/squid/passwords your_user_name
sudo chmod o+r /etc/squid3/passwords
啟動服務:
service squid start
Step3 配置集群機器代理
到此為止,master
機器已經配置好了,接下來在所有的從屬機器執行下面命令:
sudo apt-get update
sudo apt-get install tinyproxy
sed -i "s/Port 8888/Port 30303/g" /etc/tinyproxy.conf
sed -i "s/Allow 127.0.0.1/Allow ${your_master_ip}/g" /etc/tinyproxy.conf
service tinyproxy start
Step4 將子代理加入master
最后,在master
機器配置集群,在squid
的配置文件中添加以下配置,注意name
不能重復:
cache_peer ${your_cluster_ip} parent 30303 0 no-query weighted-round-robin weight=2 connect-fail-limit=2 allow-miss max-conn=5 name=${your_name}
重啟squid服務:
service squid restart
使用
我們統一使用唯一的IP作為代理即可,比如:
curl -X http://username:password@your_master_ip:port GET targetUrl
總結
通過結合squid和tinyproxy,輕松的搭建一個代理池集群服務,客戶端只需要維護一個master地址即可,而不必每次都獲取一套新的代理地址。