本文主要介紹下在騰訊雲上搭建squid代理伺服器,用於訪問國外網站或者為爬蟲提供代理ip,以及簡單介紹下如何基於騰訊雲提供的SDK,批量開啟或者銷燬代理伺服器例項。
Squid是一個高效能的代理快取伺服器,Squid支援FTP、gopher、HTTPS和HTTP協議。和一般的代理快取軟體不同,Squid用一個單獨的、非模組化的、I/O驅動的程式來處理所有的客戶端請求。
下面是搭建步驟:
1、yum安裝軟體,並設定squid開機自啟
yum install -y squid yum install -y httpd systemctl enable squid.service
2、建立squid代理的訪問使用者,並設定好密碼
htpasswd -c /etc/squid/passwd 使用者名稱
需要輸入兩次密碼
3、配置squid.conf,並重啟代理伺服器
# # Recommended minimum configuration: # # Example rule allowing access from your local networks. # Adapt to list your (internal) IP networks from where browsing # should be allowed acl localnet src 10.0.0.0/8 # RFC1918 possible internal network acl localnet src 172.16.0.0/12 # RFC1918 possible internal network acl localnet src 192.168.0.0/16 # RFC1918 possible internal network acl localnet src fc00::/7 # RFC 4193 local private network range acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT # # Recommended minimum Access Permission configuration: # # Deny requests to certain unsafe ports http_access deny !Safe_ports # Deny CONNECT to other than secure SSL ports http_access deny CONNECT !SSL_ports # Only allow cachemgr access from localhost http_access allow localhost manager http_access deny manager # We strongly recommend the following be uncommented to protect innocent # web applications running on the proxy server who think the only # one who can access services on "localhost" is a local user #http_access deny to_localhost # # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS # # Example rule allowing access from your local networks. # Adapt localnet in the ACL section to list your (internal) IP networks # from where browsing should be allowed http_access allow localnet http_access allow localhost # And finally deny all other access to this proxy #http_access deny all # Squid normally listens to port 3128 http_port 808 # Uncomment and adjust the following to add a disk cache directory. #cache_dir ufs /var/spool/squid 100 16 256 # Leave coredumps in the first cache dir coredump_dir /var/spool/squid # # Add any of your own refresh_pattern entries above these. # refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern -i (/cgi-bin/|\?) 0 0% 0 refresh_pattern . 0 20% 4320 cache_mem 128 MB maximum_object_size 16 MB cache_dir ufs /var/spool/squid 100 16 256 access_log /var/log/squid/access.log auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd auth_param basic children 5 auth_param basic realm Squid auth_param basic credentialsttl 5 hours acl ncsa_users proxy_auth REQUIRED http_access allow ncsa_users http_access deny all visible_hostname Squid.org cache_mgr abingtech@163.com
systemctl restart squid.service
4、從上面的配置檔案可以看出,訪問埠為808,需要在雲伺服器的安全組中開放埠
5、可以在瀏覽器設定中配置代理伺服器或者使用代理工具進行測試驗證
為了管理方便,採用基於騰訊雲SDK進行開發,我使用的是Java語言,程式碼很簡單就不進行過多介紹,有興趣的同學可以從碼雲
【https://gitee.com/abingtech/proxy.git】上pull,這裡主要說明下需要注意的點:
1、申請SecretId和SecretKey
2、搭建好代理伺服器後,需要在騰訊雲上手工製作好映象,作為批量建立例項的模版
3、由於騰訊雲設定的每次返回列表最大limit是100,這裡需要自己手工處理分頁的情況
另外,雲伺服器例項建議買按流量計費的模式,這樣不用隨時可以銷燬,要用啟動下就行了。