基于Squid和TinyProxy搭建代理集群

Setp0 要實現什么

架設一臺master代理主機,由這臺機器統籌管理多臺子代理機器,這樣客戶端只需要統一維護一個master機器即可。

001.png

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地址即可,而不必每次都獲取一套新的代理地址。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,869評論 18 139
  • feisky云計算、虛擬化與Linux技術筆記posts - 1014, comments - 298, trac...
    不排版閱讀 3,904評論 0 5
  • Redis Cluster Specification 1 設計目標和理由 1.1 Redis Cluster g...
    近路閱讀 4,276評論 0 12
  • 臨行前,我就這樣發誓:放心吧,今年我一定行。但在日記里,寫給自己的卻只是一句希望:希望今年能遇到那個對的她。嗯,只...
    遺棄小屋閱讀 604評論 2 1
  • 半夜看了公眾號 明天準備給皮蛋買靜電貼 水畫板 美樂手指繪畫箱 有時候會想 這些究竟是皮蛋需要的 好帥我想買的 很...
    小手摸大xiang閱讀 434評論 0 0