安裝:
下載安裝包 http://pan.baidu.com/s/1mitvwpE
解壓 tar -xzvf file.tar.gz
編譯:
進入sbin目錄 執行
./configure --prefix=/usr/local/squid --sysconfdir=/etc --enable-gnuregex --enable-icmp --enable-snmp --enable-default-err-language="Simplify_Chinese" --enable-kill-parent-hack --enable-cache-digests --enable-underscore --enable-poll --enable-async-io=240 --enable-arp-acl --enable-delay-pools --enable-follow-x-forwarded-for --with-large-files --with-default-user=squid
這個過程會花費一些時間。之后執行 make ; make install;
進入 /usr/local/squid/sbin/squid
squid -z 初始化緩存目錄
squid reload 重新加載配置
squid restart 重新啟動
squid -k parse 檢查squid.conf 配置文件,如有錯誤會提示
squid -N -d1 前台啟動,打印啟動信息
squid -s 后台啟動,並存儲啟動日志
squid -k shutdown 停止服務
ok,檢查本地訪問:
切換至squid/bin目錄,執行 squidclient www.sina.com 查看返回信息。
配置文件:
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 cao src xx.xx.xx.xx #cao是名字 隨便起 后是允許的ip
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
http_access deny !Safe_ports
http_access allow cao #允許上邊定義的cao地址
# 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
http_access allow all
http_port xx.xx.xx.xx:xx #服務器地址和設置一個任意端口號,默認3128
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256
coredump_dir /usr/local/squid/var/cache/squid
dns_nameservers 114.114.114.114 #dns
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
最后附上配置詳解:
#acl all src 0.0.0.0/0.0.0.0 and http_access allow all選項定義了一個訪問控制列表。詳細情況參見和Squid軟件 #攜帶的文檔。這里的訪問控制列表允許所有對代理服務的訪問,因為這里該代理是加速web服務器。 acl all src 0.0.0.0/0.0.0.0 #允許所有IP訪問 acl manager proto http #manager url協議為http acl localhost src 127.0.0.1/255.255.255.255 #允午本機IP acl to_localhost dst 127.0.0.1 #允午目的地址為本機IP acl Safe_ports port 80 # 允許安全更新的端口為80 acl CONNECT method CONNECT #請求方法以CONNECT http_access allow all #允許所有人使用該代理.因為這里是代理加速web服務器 http_reply_access allow all #允許所有客戶端使用該代理 acl OverConnLimit maxconn 16 #限制每個IP最大允許16個連接,防止攻擊 http_access deny OverConnLimit icp_access deny all #禁止從鄰居服務器緩沖內發送和接收ICP請求. miss_access allow all #允許直接更新請求 ident_lookup_access deny all #禁止lookup檢查DNS http_port 8080 transparent #指定Squid監聽瀏覽器客戶請求的端口號。 hierarchy_stoplist cgi-bin ? #用來強制某些特定的對象不被緩存,主要是處於安全的目的。 acl QUERY urlpath_regex cgi-bin \? cache deny QUERY cache_mem 1 GB #這是一個優化選項,增加該內存值有利於緩存。應該注意的是: #一般來說如果系統有內存,設置該值為(n/)3M。現在是3G 所以這里1G fqdncache_size 1024 #FQDN 高速緩存大小 maximum_object_size_in_memory 2 MB #允許最大的文件載入內存 memory_replacement_policy heap LFUDA #動態使用最小的,移出內存cache cache_replacement_policy heap LFUDA #動態使用最小的,移出硬盤cache cache_dir ufs /home/cache 5000 32 512 #高速緩存目錄 ufs 類型使用的緩沖值最大允午1000MB空間, #32個一級目錄,512個二級目錄 max_open_disk_fds 0 #允許最大打開文件數量,0 無限制 minimum_object_size 1 KB #允午最小文件請求體大小 maximum_object_size 20 MB #允午最大文件請求體大小 cache_swap_low 90 #最小允許使用swap 90% cache_swap_high 95 #最多允許使用swap 95% ipcache_size 2048 # IP 地址高速緩存大小 2M ipcache_low 90 #最小允許ipcache使用swap 90% ipcache_high 95 #最大允許ipcache使用swap 90% access_log /var/log/squid/access.log squid #定義日志存放記錄 cache_log /var/log/squid/cache.log squid cache_store_log none #禁止store日志 emulate_httpd_log on #將使Squid仿照Web服務器的格式創建訪問記錄。如果希望使用 #Web訪問記錄分析程序,就需要設置這個參數。 refresh_pattern . 0 20% 4320 override-expire override-lastmod reload-into-ims ignore-reload #更新cache規則 acl buggy_server url_regex ^http://.... http:// #只允許http的請求 broken_posts allow buggy_server acl apache rep_header Server ^Apache #允許apache的編碼 broken_vary_encoding allow apache request_entities off #禁止非http的標分准請求,防止攻擊 header_access header allow all #允許所有的http報頭 relaxed_header_parser on #不嚴格分析http報頭. client_lifetime 120 minute #最大客戶連接時間 120分鍾 cache_mgr sky@test.com #指定當緩沖出現問題時向緩沖管理者發送告警信息的地址信息。 cache_effective_user squid #這里以用戶squid的身份Squid服務器 cache_effective_group squid icp_port 0 #指定Squid從鄰居服務器緩沖內發送和接收ICP請求的端口號。 #這里設置為0是因為這里配置Squid為內部Web服務器的加速器, #所以不需要使用鄰居服務器的緩沖。0是禁用 # cache_peer 設置允許更新緩存的主機,因是本機所以127.0.0.1 cache_peer 127.0.0.1 parent 80 0 no-query default multicast-responder no-netdb-exchange cache_peer_domain 127.0.0.1 hostname_aliases 127.0.0.1 error_directory /usr/share/squid/errors/Simplify_Chinese #定義錯誤路徑 always_direct allow all # cache丟失或不存在是允許所有請求直接轉發到原始服務器 ignore_unknown_nameservers on #開反DNS查詢,當域名地址不相同時候,禁止訪問 coredump_dir /var/log/squid #定義dump的目錄 max_filedesc 2048 #最大打開的文件描述 half_closed_clients off #使Squid在當read不再返回數據時立即關閉客戶端的連接。 #有時read不再返回數據是由於某些客戶關閉TCP的發送數據 #而仍然保持接收數據。而Squid分辨不出TCP半關閉和完全關閉。 buffered_logs on #若打開選項“buffered_logs”可以稍稍提高加速某些對日志文件的寫入,該選項主要是實現優化特性。 #防止天涯盜鏈,轉嫁給百度 acl tianya referer_regex -i tianya http_access deny tianya deny_info tianya #阻止baidu蜘蛛 acl baidu req_header User-Agent Baiduspider http_access deny baidu #限制同一IP客戶端的最大連接數 acl OverConnLimit maxconn 128 http_access deny OverConnLimit #防止被人利用為HTTP代理,設置允許訪問的IP地址 acl myip dst 222.18.63.37 http_access deny !myip #允許本地管理 acl Manager proto cache_object acl Localhost src 127.0.0.1 222.18.63.37 http_access allow Manager Localhost cachemgr_passwd 53034338 all http_access deny Manager #僅僅允許80端口的代理 acl all src 0.0.0.0/0.0.0.0 acl Safe_ports port 80 # http http_access deny !Safe_ports http_access allow all #Squid信息設置 visible_hostname happy.swjtu.edu.cn cache_mgr ooopic2008@qq.com #基本設置 cache_effective_user squid cache_effective_group squid tcp_recv_bufsize 65535 bytes #2.6的反向代理加速配置 cache_peer 127.0.0.1 parent 80 0 no-query originserver #錯誤文檔 error_directory /usr/local/squid/share/errors/Simplify_Chinese #單台使用,不使用該功能 icp_port 0 hierarchy_stoplist cgi-bin ? acl QUERY urlpath_regex cgi-bin \? .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe cache deny QUERY acl apache rep_header Server ^Apache broken_vary_encoding allow apache refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern . 0 20% 4320 cache_store_log none pid_filename /usr/local/squid/var/logs/squid.pid emulate_httpd_log on logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh cache_log /usr/local/squid/var/logs/cache.log access_log /usr/local/squid/var/logs/access.log combined coredump_dir /usr/local/squid/var/cache cache_dir ufs /usr/local/squid/var/cache 10000 16 256 dns_children 32 hosts_file /etc/hosts cache_mem 400 MB cache_swap_low 90 cache_swap_high 95 maximum_object_size 32768 KB maximum_object_size_in_memory 4096 KB emulate_httpd_log on #防止盜鏈 acl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$ acl mystie1 referer_regex -i happy.swjtu.edu.cn http_access allow mystie1 picurl acl nullref referer_regex -i ^$ http_access allow nullref acl hasref referer_regex -i .+ http_access deny hasref picurl