天天看點

squid安裝與相關配置

一、安裝squid

yum install -y squid

一般情況下系統自帶的squid已經夠用了

./configure --prefix=/usr/local/squid \

--disable-dependency-tracking \

--enable-dlmalloc \

--enable-gnuregex \

--disable-carp \

--enable-async-io=240 \

--with-pthreads \

--enable-storeio=ufs,aufs,diskd,null \

--disable-wccp \

--disable-wccpv2 \

--enable-kill-parent-hack \

--enable-cachemgr-hostname=localhost \

--enable-default-err-language=Simplify_Chinese\

--with-build-environment=POSIX_V6_ILP32_OFFBIG\

--with-maxfd=65535 \

--with-aio \

--disable-poll \

--enable-epoll \

--enable-linux-netfilter \

--enable-large-cache-files \

--disable-ident-lookups \

--enable-default-hostsfile=/etc/hosts \

--with-dl \

--with-large-files \

--enable-removal-policies=heap,lru \

--enable-delay-pools \

--enable-snmp \

--disable-internal-dns

二、安裝完後,可以檢視squid版本:

squid –v

三、配置squid:

rm -rf /etc/squid/squid.conf(不使用預設檔案)

vim /etc/squid/squid.conf

 http_port 3128

 aclmanager proto cache_object

 acllocalhost src 127.0.0.1/32 ::1

 aclto_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1

 acllocalnet src 10.0.0.0/8     # RFC1918possible internal network

 acllocalnet src 172.16.0.0/12  # RFC1918possible internal network

 acllocalnet src 192.168.0.0/16 # RFC1918 possible internal network

 aclSSL_ports port 443

 aclSafe_ports port 80 8080         # http

 aclSafe_ports port 21          # ftp

 aclSafe_ports port 443         # https

 aclCONNECT method CONNECT

 http_access allow manager localhost

 http_access deny manager

 http_access deny !Safe_ports

 http_access deny CONNECT !SSL_ports

 http_access allow localnet

 http_access allow localhost

 http_access allow all

 cache_dir aufs /data/cache 1024 16 256

 cache_mem 128 MB

 hierarchy_stoplist cgi-bin ?

 coredump_dir /var/spool/squid

 refresh_pattern^ftp:           1440    20%    10080

 refresh_pattern ^gopher:        1440   0%      1440

 refresh_pattern -i (/cgi-bin/|\?) 0     0%     0

 refresh_pattern \.(jpg|png|gif|mp3|xml)1440    50%     2880   ignore-reload

 refresh_pattern .               0       20%    4320

配置檔案中有幾處要簡單描述一下:

第一行的“http_port 3128”這個指的是:squid服務啟動後将要監聽的端口,也可以是80. “cache_dir”這個用來指定本地磁盤上的緩存目錄,後邊的1024為大小,機關是M,具體根據你的磁盤大小決定。

“cache_mem”它用來規定緩存占用記憶體的大小,即把緩存的東西存到記憶體裡,具體也需要根據你機器的記憶體定,如果你的機器隻是跑Squid服務,那麼留給系統512M記憶體外,其他可以都分給squid, 但做實驗的虛拟機一共才300M記憶體,是以隻分了128M.

配置檔案儲存好後,可以先檢測一下是否有文法錯誤:

squid–kcheck

在啟動前還得再做一件事,就是初始化緩存目錄:

mkdir/data/cache

chown-R squid:squid /data/cache/

squid–z

初始化完成後,就可以啟動squid了:

/etc/init.d/squidstart

四、使用curl指令測試即可:

curl-xlocalhost:3128  http://www.baidu.com

有時,我們會有這樣的需求,就是想限制某些域名不能通過代理通路,或者說隻想代理某幾個域名,這如何做呢?在squid.conf中找到:

aclCONNECT method CONNECT

在其下面添加四行:

aclhttp proto HTTP

aclgood_domain dstdomain .baidu.com

http_accessallow http good_domain

http_accessdeny http !good_domain

重新開機squid再來測測看:

/etc/init.d/squidrestart

curl-xlocalhost:80 http://www.baidu.com -I

如果要設定黑名單呢?道理是一樣的:

aclbad_domain dstdomain .sina.com .souhu.com

http_accessallow http !bad_domain

http_accessdeny http bad_domain

重新開機squid後,測試:

curl-xlocalhost:80 http://www.sina.com/ -I

curl-xlocalhost:80 http://www.baidu.com/ -I

反向代理:

過程其實和前面的正向代理沒有什麼太大差別,唯一的差別是配置檔案中一個地方需要改動一下。需要把:

http_port3128

改為:

http_port80  accel vhost vport

然後再增加你要代理的後端真實伺服器資訊:

cache_peer123.125.119.147 parent 80 0 originserver name=a

cache_peer61.135.169.125 parent 80 0 originserver name=b

cache_peer_domaina www.qq.com

cache_peer_domainb www.baidu.com

如果是squid要代理一台web上的所有域名,那麼就寫成這樣:

cache_peer192.168.10.111 80 0 originserver

後面連cache_peer_domain 也省了。

本文轉自 sykmiao 51CTO部落格,原文連結:http://blog.51cto.com/syklinux/1733173,如需轉載請自行聯系原作者

繼續閱讀