linux下配置squidProxy 伺服器

來源:互聯網
上載者:User

標籤:linux   squid   centos   

linux下配置squid

1、什麼是squid

Squid cache(簡稱為Squid)是一個流行的自由軟體(GNU通用公用許可證)的Proxy 伺服器和Web快取服務器。Squid有廣泛的用途,從作為網頁伺服器的前置cache伺服器緩衝相關請求來提高Web伺服器的速度,到為一組人共用網路資源而緩衝全球資訊網,網域名稱系統和其他網路搜尋,到通過過濾流量協助網路安全,到區域網路通過代理上網。Squid主要設計用於在Unix一類系統運行。

Squid的發展曆史相當悠久,功能也相當完善。除了HTTP外,對於FTP與HTTPS的支援也相當好,在3.0 測試版中也支援了IPv6。

squid可以做代理也可以做緩衝;

squid緩衝不僅可以節省寶貴的頻寬資源,也可以大大降低伺服器的I/O

squid不僅可以做正向 Proxy,又可以做反向 Proxy。

正向 Proxy,squid後面是用戶端,用戶端上網要通過Squid去上;反向 Proxy,squid後面是伺服器,伺服器返回給使用者資料需要走squid。

正向 Proxy用在企業的辦公環境中,員工上網需要通過squid代理來上網,這樣可以節省網路頻寬資源。而反向 Proxy用來搭建網站靜態項(圖片、html、流媒體、js、css等)的快取服務器,它用於網站架構中。


2、搭建squid正向 Proxy

官方網站為 http://www.squid-cache.org/  

安裝命令:yum install -y squid

squid -v  查看版本以及編譯參數(Squid Cache: Version 3.1.10)

> /etc/squid/squid.conf    清空設定檔;

vim /etc/squid/squid.conf

加入如下配置:

http_port 3128acl manager proto cache_objectacl localhost src 127.0.0.1/32 ::1acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1acl localnet src 10.0.0.0/8     # RFC1918 possible internal networkacl localnet src 172.16.0.0/12  # RFC1918 possible internal networkacl localnet src 192.168.0.0/16 # RFC1918 possible internal networkacl SSL_ports port 443acl Safe_ports port 80 8080acl Safe_ports port 21acl Safe_ports port 443acl CONNECT method CONNECThttp_access allow manager localhosthttp_access deny managerhttp_access deny !Safe_portshttp_access deny CONNECT !SSL_portshttp_access allow localnethttp_access allow localhosthttp_access allow allcache_dir aufs /data/cache 1024 16 256cache_mem 128 MBhierarchy_stoplist cgi-bin ?coredump_dir /var/spool/squidrefresh_pattern ^ftp:           1440    20%     10080refresh_pattern ^gopher:        1440    0%      1440refresh_pattern -i (/cgi-bin/|\?) 0     0%      0refresh_pattern \.(jpg|png|gif|mp3|xml) 1440    50%     2880    ignore-reloadrefresh_pattern .               0       20%     4320######################################################### 到此結束

配置解釋:

acl Safe_ports port 80 8080         # http的連接埠

acl Safe_ports port 21          # ftp的連接埠

acl Safe_ports port 443         # https的連接埠

cache_dir aufs /data/cache 1024 16 256    #緩衝空間1024M大小 16個一級目錄,256個子目錄

cache_mem 128 MB    #緩衝可以使用的記憶體大小;放在記憶體中訪問資料速度快;


mkdir  /data/cache    #建立緩衝目錄

chown -R squid:squid /data/cache    #更改緩衝目錄許可權

squid -z    #初始化緩衝目錄,squid新版本3.1可以省略

/etc/init.d/squid start    #啟動squid服務

squid  -k check    #可以檢測設定檔是否有錯;可以簡寫為-kche

squid -k rec    #可以重新載入配置,reconfig的簡寫;

service squid restart    #重啟squid服務;重啟經常性的很慢,可以先killall squid,在啟動服務;


檢測設定檔,報錯:Could not determine this machines public hostname. Please configure one or set ‘visible_hostname‘.沒有定義公用主機名稱,需要配置visible_hostname 可視化主機名稱;(squid出問題,會在瀏覽器顯示squid的主機名稱)

在設定檔中加入:visible_hostname yonglinux 就不會報錯;

[[email protected] ~]# squid -k check2015/05/25 03:09:18| WARNING: Could not determine this machines public hostname. Please configure one or set ‘visible_hostname‘.2015/05/25 03:09:18| WARNING: Could not determine this machines public hostname. Please configure one or set ‘visible_hostname‘.squid: ERROR: No running copy


在另一台linux進行測試:curl -x192.168.22.30:3128 www.qq.com    

指定Proxy 伺服器192.168.22.30的3128連接埠訪問網站,前提保證Proxy 伺服器能訪問網站;

設定Proxy 伺服器的作用是讓區域網路的使用者訪問網站速度快,另一方面可以控制使用者訪問哪些網站;上班期間禁止員工看視頻,購物;


訪問圖片,測試緩衝,緩衝的時間,X-Cache為HIT擊中,說明squid緩衝起作用;第一次為MISS;

[[email protected] ~]# curl -x192.168.22.30:3128 ‘http://www.51cto.com/images/home/images/logo.jpg‘ -IHTTP/1.0 200 OKServer: TengineDate: Sun, 24 May 2015 13:42:43 GMTContent-Type: image/jpegContent-Length: 5309Last-Modified: Wed, 22 Jan 2014 07:55:12 GMTExpires: Sun, 31 May 2015 13:42:43 GMTCache-Control: max-age=604800Load-Balancing: web39Accept-Ranges: bytesAge: 29661X-Cache: HIT from yonglinuxX-Cache-Lookup: HIT from yonglinux:3128Via: 1.0 yonglinux (squid/3.1.10)Connection: keep-alive


設定squidProxy 伺服器只代理某幾個網域名稱  

設定網域名稱白名單,允許baidu sohu可以訪問,其他都拒絕;

vim /etc/squid/squid.conf    下面的內容加入到squid設定檔acl下面;

acl http proto HTTP

acl good_domain dstdomain .baidu.com .sohu.com

http_access allow http good_domain

http_access deny http !good_domain


使用curl測試白名單,baidu、sohu返回狀態代碼為200 OK,qq不在白名單則返回403;

[[email protected] ~]# curl -x192.168.22.30:3128 www.sohu.com -IHTTP/1.0 200 OKContent-Type: text/htmlDate: Sun, 24 May 2015 13:57:32 GMTServer: SWSVary: Accept-EncodingCache-Control: no-transform, max-age=120Expires: Sun, 24 May 2015 13:59:32 GMTLast-Modified: Sun, 24 May 2015 13:57:21 GMTX-RS: 11172604.20347654.12509576FSS-Cache: HIT from 9861864.17726194.11198816X-Cache: MISS from yonglinuxX-Cache-Lookup: MISS from yonglinux:3128Via: 1.0 yonglinux (squid/3.1.10)Connection: keep-alive[[email protected] ~]# curl -x192.168.22.30:3128 www.qq.com -IHTTP/1.0 403 ForbiddenServer: squid/3.1.10Mime-Version: 1.0Date: Sun, 24 May 2015 22:04:30 GMTContent-Type: text/htmlContent-Length: 3254X-Squid-Error: ERR_ACCESS_DENIED 0Vary: Accept-LanguageContent-Language: enX-Cache: MISS from yonglinuxX-Cache-Lookup: NONE from yonglinux:3128Via: 1.0 yonglinux (squid/3.1.10)Connection: keep-alive


限制某些網域名稱不能通過代理訪問

設定網域名稱黑名單,不允許訪問taobao.com jd.com;其他的都允許;

vim /etc/squid/squid.conf  下面的內容加入到squid設定檔acl下面

acl http proto HTTP

acl bad_domain dstdomain .taobao.com .jd.com

http_access deny http bad_domain


使用curl測試黑名單,taobao、jd返回狀態代碼為403,51cto不在黑名單返回200 OK;

[[email protected] ~]# curl -x192.168.22.30:3128 www.taobao.com -IHTTP/1.0 403 ForbiddenServer: squid/3.1.10Mime-Version: 1.0Date: Sun, 24 May 2015 21:35:22 GMTContent-Type: text/htmlContent-Length: 3266X-Squid-Error: ERR_ACCESS_DENIED 0Vary: Accept-LanguageContent-Language: enX-Cache: MISS from yonglinuxX-Cache-Lookup: NONE from yonglinux:3128Via: 1.0 yonglinux (squid/3.1.10)Connection: keep-alive[[email protected] ~]# curl -x192.168.22.30:3128 www.jd.com -IHTTP/1.0 403 ForbiddenServer: squid/3.1.10Mime-Version: 1.0Date: Sun, 24 May 2015 21:35:32 GMTContent-Type: text/htmlContent-Length: 3254X-Squid-Error: ERR_ACCESS_DENIED 0Vary: Accept-LanguageContent-Language: enX-Cache: MISS from yonglinuxX-Cache-Lookup: NONE from yonglinux:3128Via: 1.0 yonglinux (squid/3.1.10)Connection: keep-alive
[[email protected] ~]# curl -x192.168.22.30:3128 www.51cto.com -IHTTP/1.0 200 OKServer: TengineDate: Sun, 24 May 2015 13:31:21 GMTContent-Type: text/htmlVary: Accept-EncodingLoad-Balancing: web39X-Cache: MISS from yonglinuxX-Cache-Lookup: MISS from yonglinux:3128Via: 1.0 yonglinux (squid/3.1.10)Connection: keep-alive


使用IE瀏覽器測試,需要設定Proxy 伺服器,功能表列——工具——Internet選項——串連——區域網路設定,勾選Proxy 伺服器——進階,填寫squidProxy 伺服器地址和連接埠號碼;

650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6D/68/wKioL1Vj6y3RSTBnAAJ-49855ng024.jpg" title="share3.jpg" alt="wKioL1Vj6y3RSTBnAAJ-49855ng024.jpg" />

訪問jd.com taobao.com,提示錯誤,訪問被拒絕,由之前定義的可視化主機名稱發出的;訪問其他網站正常;

650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6D/6C/wKiom1Vj6bCiiV_LAAKzSI7wupg889.jpg" title="share4.jpg" alt="wKiom1Vj6bCiiV_LAAKzSI7wupg889.jpg" />


3、搭建squid反向 Proxy

vim /etc/squid/squid.conf  #如下變更

之前增加的網域名稱白/黑名單相關配置去掉;

http_port 3128 改為 http_port 80 accel vhost vport

增加如下內容:

cache_peer 14.17.42.40 parent 80 0 originserver name=a

cache_peer 180.97.33.107 parent 80 0 originserver name=b

cache_peer_domain a www.qq.com

cache_peer_domain b www.baidu.com


監聽的3128連接埠改為80連接埠;IE瀏覽器Proxy 伺服器的連接埠也要更改為80;

14.17.42.40為ping www.qq.com的ip地址;

如果是squid要代理一台web上的所有網域名稱,那麼就寫成這樣: cache_peer 192.168.10.111 80 0 originserver  #只需要這一行,cache_peer_domain  可以省略;

/etc/init.d/squid restart


IE瀏覽器測試反向 Proxy,訪問baidu.com、qq.com可以訪問,其他網站提示:目前無法將您的請求進行轉送操作

650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6D/6C/wKiom1Vj6fXgdvn8AAMQf9jMj4k031.jpg" title="share5.jpg" alt="wKiom1Vj6fXgdvn8AAMQf9jMj4k031.jpg" />

使用curl測試

[[email protected] ~]# curl -x192.168.22.30:80 www.qq.com -IHTTP/1.0 200 OKServer: squid/3.4.1Date: Sun, 24 May 2015 14:22:47 GMTContent-Type: text/html; charset=GB2312Vary: Accept-EncodingVary: Accept-EncodingExpires: Sun, 24 May 2015 14:23:47 GMTCache-Control: max-age=60Vary: Accept-EncodingVary: Accept-EncodingX-Cache: HIT from shenzhen.qq.comX-Cache: MISS from yonglinuxX-Cache-Lookup: MISS from yonglinux:80Via: 1.0 yonglinux (squid/3.1.10)Connection: keep-alive

訪問qq.com返回HIT from shenzhen.qq.com,說明qq.com本身也做了反向 Proxy;



squid還有很多配置,後續會陸續更新;

本文出自 “模範生的學習部落格” 部落格,請務必保留此出處http://8802265.blog.51cto.com/8792265/1655196

linux下配置squidProxy 伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.