鳥哥學習筆記---Proxy

Proxy緩存因爲是讀取硬盤的內容,所心應用Proxy的瓶頸就是硬盤的讀取。

 
通過上層代理服務器的輔助,達到自動數據分流的效果
 
Apache也可以提供代理服務;
 
配置文件:
1./etc/squid/squid.conf
2./etc/squid/mime.conf   #支持的Internet文件格式
 
相關程序與文件:
1./usr/sbin/squid
2./var/spool/squid       #默認緩存目錄
3./usr/lib64/squid       #squid控制模塊,密碼認證等
 
默認啓動squid程序的用戶爲squid這個賬號(與磁盤調整緩存目錄權限有關)
 
幫助文件:
[root@Centosszm ~]# vi /usr/share/doc/squid-3.1.4/squid.conf.documented
 
 
[root@Centosszm ~]# grep -v '^[#]' /etc/squid/squid.conf
acl manager proto cache_object   #定義manager爲管理功能
acl localhost src 127.0.0.1/32   #定義localhost
acl localhost :1 /128
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32   #定義可連接到本機,也可以用“-”
acl to_localhost dst ::1/128
 
#這個localnet也可以自己改爲其它的名字,一個名字對一種限制(src)
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
 
#例子(src,dstdomain,dst,dstdomain,url_regex,urlpath_regex)
acl denyip src 172.16.128.1-172.16.128.100/32
acl denyrule1 url_regex ^http://www.baidu.com/.*  #^開頭,.*結尾
acl denyrule2 urlpath_regex /sexy.*\.jpg$         #
acl denydomain dstdomain "/etc/squid/dropdomain.txt"
acl denydomain dstdomain .163.com
 
 
 
 
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 denyrule1
http_access deny denyrule2
http_access deny denydomain
http_access deny denyip
http_access allow manager localhost
http_access deny manager
 
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
 
 
 
http_access allow localnet     #放行localnet網絡
http_access allow localhost    #放行localhost網絡
http_access deny all
 
http_port 3128
 
hierarchy_stoplist cgi-bin ?
 
cache_dir ufs /var/spool/squid 100 16 256  #100M緩存    一級目錄  二級目錄
#兩層緩存目錄較佳的配置就是16 256和64 64
 
 
coredump_dir /var/spool/squid
 
#磁盤緩存時間(regex,失效時間、百分比、最大時間)後面兩個爲重新獲取時間
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
 
#add by szm
minimum_object_size 0 KB      #這兩個是緩存頁面大小要求
maximum_object_size 4096 KB
cache_swap_low 90             #高於95%後刪除到佔用90%
cache_swap_high 95
 
access_log /var/log/squid/access.log squid   #日誌
ftp_user Squid@         #FTP匿名登錄時使用的賬號名稱
ftp_passive on          #使用被動FTP連接
 
cache_mgr [email protected]  #管理員郵箱
visible_hostname www.Centosszm.com  #自定義主機名
cache_effective_user squid
cache_effective_group squid
ipcache_size 8          #這個值與cache_mem 8 MB 相同
ipcache_low 90
ipcache_high 95
 
#設置不緩存PHP頁面
acl denyphp urlpath_regex \.php$
cache deny denyphp
 
#上層Proxy與獲取數據分流的設定:
 
#下成是上層Proxy設置
#Proxy 角色:parent     sibling
#Proxy port      icp port
#proxy-only:不緩存,只查詢
#weight=n:權重
#no-query:向上層Proxy要求數據時,可以不需要發送icp數據包
#no-digest:不向附近主機要求建立digest記錄表格
#no-netdb-exchange:不向附近的Proxy主機送出ICMP的數據包要求
cache_peer szm.test.com parent 3128 3130 proxy-only no-query no-digest
 
#要求這臺服務器向哪個域名要求數據(.cn)分流.cn的數據
cache_peer_domain szm.test.com .cn
 
 
#cache_peer_access [上層Proxy主機名] [allow|deny] [acl 名稱]
 與cache_peer_domain相當類似,只是cache_peer_domain直接規範了主機名,而如果你想要設計的並非域名,而是某些特定的IP網段,就需要先用Acl設計一個名稱後,再以cache_peer_access去放行或拒絕讀取了。

 

 

[root@Centosszm ~]# netstat -tulnp | grep squid
tcp        0      0 :::3128                     :::*                        LISTEN      14205/(squid)
udp        0      0 :::37572                    :::*                                    14205/(squid)
 
3130(UDP)僅是負責與鄰近Proxy互相溝通彼此的緩存數據庫的功能,與實際的用戶要求無關。
 
 
增加緩存大小:
[root@Centosszm ~]# vi /etc/squid/squid.conf
cache_dir ufs /srv/squid 200 16 256
[root@Centosszm ~]# mkdir /srv/squid
[root@Centosszm ~]# chmod 750 /srv/squid
[root@Centosszm ~]# chown squid:squid /srv/squid
[root@Centosszm ~]# chcon --reference /var/spool/squid/ /srv/squid
[root@Centosszm ~]# ll -Zd /srv/squid
drwxr-x---. squid squid system_u:object_r:squid_cache_t:s0 /srv/squid
[root@Centosszm ~]# /etc/init.d/squid restart
 
 某些特定的目錄(/home)是不允許建立緩存目錄的。
 
Squid佔用內存計算:
緩存*10%+15(squid本身佔用)+cache_mem大小
 
防火牆設置:
[root@Centosszm ~]# iptables -A INPUT -p tcp -m tcp --dport 3128 -j ACCEPT
 
重新應用配置文件:
[root@Centosszm ~]# vi /etc/squid/dropdomain.txt
[root@Centosszm ~]# service squid reload
 
Proxy服務放在NAT服務器上:透明代理
1.[root@Centosszm ~]# vi /etc/squid/squid.conf
http_port 3128 transparent
 
2.Port 80 轉Port 3128
[root@Centosszm ~]# iptables -t nat -A PREROUTING -i eth1 -s 172.16.128.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128
 
 
關閉所有的緩存;只代理,不緩存:
[root@Centosszm ~]# /etc/init.d/squid stop
[root@Centosszm ~]# rm -rf /var/spool/squid/*
[root@Centosszm ~]# vim /etc/squid/squid.conf
cache_dir ufs /var/spool/squid 100 16 256 read-only
#cache_dir ufs /srv/squid 200 16 256
cache_mem 0 MB
[root@Centosszm ~]# /etc/init.d/squid start
 
 
Proxy認證的設置:
 使用的是squid主動提供的ncsa_auth認證模塊,這個模塊會利用apacher提供的賬號密碼建立命令(htpasswd)所製作的密碼文件作爲驗證依據。
[root@Centosszm ~]# rpm -ql squid | grep ncsa
/usr/lib/squid/ncsa_auth
/usr/share/man/man8/ncsa_auth.8.gz
[root@Centosszm ~]# rpm -ql httpd | grep htpasswd
 
#上面的沒有內容,找看結果如下,應該是這個包獨立出來了
[root@Centosszm ~]# rpm -qf /usr/bin/htpasswd
httpd-tools-2.2.15-5.el6.centos.i686
 
[root@Centosszm ~]# rpm -ql httpd-tools | grep htpasswd
/usr/bin/htpasswd
/usr/share/man/man1/htpasswd.1.gz
 
[root@Centosszm ~]# vim /etc/squid/squid.conf
#通過ncsa_auth讀取squid_user.txt
#啓動5個程序來管理驗證的需求
#歡迎信息
auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/squid_user.txt
auth_param basic children 5
auth_param basic realm Welcome to szm's proxy-only web server   #在放在前面,因爲要加載模塊
 
#proxy_auth是關鍵詞
#REQUIRED指定任何在密碼文件內的用戶都能夠使用驗證的意思
acl squid_user proxy_auth REQUIRED 
http_access allow squid_user
 
[root@Centosszm ~]# htpasswd -c /etc/squid/squid_user.txt szm
New password:
Re-type new password:
Adding password for user szm
[root@Centosszm ~]# cat /etc/squid/squid_user.txt
szm:pWQO53hxTn06w
[root@Centosszm ~]# /etc/init.d/squid reload
 
 
日誌分析軟件:SARG
http://localhost/sarg/
 
[root@Centosszm ~]# wget http://pkgs.repoforge.org/sarg/sarg-2.3.1-1.el6.rft.i686.rpm
yum install gd gd-devel
[root@Centosszm ~]# rpm -ivh sarg-2.3.1-1.el6.rft.i686.rpm
title "Squid User Access Reports"
font_size 12px
charset UTF-8
 
#製作所有日誌文件內的數據報表
[root@Centosszm ~]# sarg
SARG: Records in file: 3410, reading: 100.00%
SARG: (grepday) Fontname /usr/share/sarg/fonts/DejaVuSans.ttf not found
 
#製作4月8號的報表
[root@Centosszm ~]# sarg -d 08/04/2013
SARG: Records in file: 3410, reading: 100.00%
SARG: Period covered by log files: 08/04/2013-08/04/2013
SARG: (grepday) Fontname /usr/share/sarg/fonts/DejaVuSans.ttf not found
 
#上面這兩個命令都會將數據放到/var/www/sarg/ONE-SHOT
[root@Centosszm ~]# sh /etc/cron.daily/sarg
[root@Centosszm ~]# ll /var/www/sarg/ONE-SHOT/
total 16
drwxr-xr-x. 4 root root 4096 Apr  8 21:09 2013Apr08-2013Apr08
drwxr-xr-x. 4 root root 4096 Apr  8 21:07 2013Apr08-2013Apr08.1
drwxr-xr-x. 4 root root 4096 Apr  8 21:08 2013Apr08-2013Apr08.2
drwxr-xr-x. 2 root root 4096 Apr  8 21:07 p_w_picpaths
 
HTTP查閱數據:
http://127.0.0.1/sarg
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章