راه اندازی و بهینه سازی Squid و کنترل پهنای باند
دوستان عزیز سلام:
برای این منظور می خواستم چند مقاله را به صورت ترجمه شده باز نویسی کنم اما متاسفانه کمبود وقت و مشغولیت های فکری مانع شدند در ضمن گیرایی مطلب هم به حدی ست که حیفم اومد باز هم تعلل مانع شروع بشه....خلاصه سعی بر این شد که با ویرایش کلی و جفت کردن مطالب به هم به زبان انگلیسی ساده مطلبی رو ارائه کنم که امیدوارم با کمک اساتید اگر مشکلی یا ایرادی هم داشت رفع و اصلاح بشه.......
می خواهیم در این چکیده به نصب و پیکر بندی squid پرداخته و با اعمال محدودیت هایی بر پهنای باند آن را در کنترل داشته باشیم......
[LEFT]Installing and Configuring Necessary Software Here, I will explain how to install the necessary software so that we can limit and test the bandwidth usage.
1. Installing Squid with the delay pools feature Squid has a feature called delay pools, which allows us to control download bandwidth. Unfortunately, in most distributions, Squid is shipped without that feature.
So if you have Squid already installed, I must disappoint you -- you need to uninstall it and do it once again with delay pools enabled in the way I explain below.
To get maximum performance from our Squid proxy, it's best to create a separate partition for its cache, called /cache/. Its size should be about 300 megabytes,( depending on our needs).
If you don't know how to make a separate partition, you can create the /cache/ directory on a main partition, but Squid performance can suffer a bit.
We add a safe 'squid' user useradd -d /cache/ -r -s /dev/null squid >/dev/null 2>&1 No one can log in as squid, including root[/LEFT].
[LEFT]We download Squid sources from [url]http://www.squid-cache.org[/url][/LEFT]
[LEFT] For example: [url]http://www.squid-cache.org/Versions/v2/2.5/[/url][/LEFT]
[LEFT]
We unpack everything to /var/tmp:
tar xzpf squid-2.5.STABLE10-src.tar.gz
We compile and install Squid (everthing is in one line) ./configure --prefix=/opt/squid --exec-prefix=/opt/squid --enable-delay-pools --enable-cache-digests --enable-poll --disable-ident-lookups --enable-truncate --enable-removal-policies
make all
make install
2. Configuring Squid to use the delay pools feature
Configure our squid.conf file (located under /opt/squid/etc/squid.conf):
squid.conf Every option in this file is very well documented in the original squid.conf file
The ports our Squid will listen on. http_port 8080 icp_port 3130 cgi-bins will not be cached. acl QUERY urlpath_regex cgi-bin \? no_cache deny QUERY Memory the Squid will use. Well, Squid will use far more than that. cache_mem 16 MB 250 means that Squid will use 250 megabytes of disk space. cache_dir ufs /cache 250 16 256
Places where Squid's logs will go to. cache_log /var/log/squid/cache.log cache_access_log /var/log/squid/access.log cache_store_log /var/log/squid/store.log cache_swap_log /var/log/squid/swap.log
How many times to rotate the logs before deleting them.
logfile_rotate 10
redirect_rewrites_host_header off
cache_replacement_policy GDSF
acl localnet src 192.168.1.0/255.255.255.0
acl localhost src 127.0.0.1/255.255.255.255
acl Safe_ports port 80 443 210 119 70 20 21 1025-65535
acl CONNECT method CONNECT
acl all src 0.0.0.0/0.0.0.0
http_access allow localnet
http_access allow localhost
http_access deny !Safe_ports
http_access deny CONNECT
http_access deny all
maximum_object_size 3000 KB
store_avg_object_size 50 KB
Set these if you want your proxy to work in a transparent way. Transparent proxy means you generally don't have to configure all your client's browsers, but have some drawbacks too. httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
all our LAN users will be seen by external web servers as if they all used Mozilla on Linux. :)
anonymize_headers deny User-Agent
fake_user_agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.6+) Gecko/20011122
To make our connection even faster, we put two lines similar to the ones below. They will point a parent proxy server our own Squid will use. Don't forget to change the server to the one that will be fastest for you! Measure pings, traceroutes and so on. Make sure that http and icp ports are correct.
Uncomment lines beginning with "cache_peer" if necessary. This is the proxy you are going to use for all connections...
cache_peer w3cache.icm.edu.pl parent 8080 3130 no-digest default
...except for the connections to addresses and IPs beginning with "!".
It's a good idea not to use a higher
cache_peer_domain w3cache.icm.edu.pl !.pl !7thguard.net !192.168.1.1
This is useful when we want to use the Cache Manager.
Copy cachemgr.cgi to cgi-bin of your www server.
You can reach it then via a web browser typing
the address [url]http://your-web-server/cgi-bin/cachemgr.cgi[/url]
cache_mgr your@email
cachemgr_passwd secret_password all
This is a name of a user our Squid will work as.
cache_effective_user squid
cache_effective_group squid
log_icp_queries off
buffered_logs on
DELAY POOLS
This is the most important part for shaping incoming traffic with Squid
For detailed description see squid.conf file or docs at [url]http://www.squid-cache.org[/url][/LEFT] [LEFT]
We don't want to limit downloads on our local network.
acl magic_words1 url_regex -i 192.168
We want to limit downloads of these type of files
Put this all in one line
acl magic_words2 url_regex -i ftp .exe .mp3 .vqf .tar.gz .gz .rpm .zip .rar .avi .mpeg .mpe .mpg .qt .ram .rm .iso .raw .wav .mov
We don't block .html, .gif, .jpg and similar files, because they generally don't consume much bandwidth
We want to limit bandwidth during the day, and allow full bandwidth during the night
Caution! with the acl below your downloads are likely to break at 23:59. Read the FAQ in this bandwidth if you want to avoid it.
acl day time 09:00-23:59
We have two different delay_pools
View Squid documentation to get familiar
with delay_pools and delay_class.
delay_pools 2
First delay pool
We don't want to delay our local traffic.
There are three pool classes; here we will deal only with the second.
First delay class (1) of second type (2).
delay_class 1 2
1/-1 mean that there are no limits. delay_parameters 1 -1/-1 -1/-1
magic_words1: 192.168 we have set before
delay_access 1 allow magic_words1
Second delay pool.
we want to delay downloading files mentioned in magic_words2.
Second delay class (2) of second type (2).
delay_class 2 2
The numbers here are values in bytes;
we must remember that Squid doesn't consider start/stop bits
5000/150000 are values for the whole network
5000/120000 are values for the single IP
after downloaded files exceed about 150000 bytes,
(or even twice or three times as much)
they will continue to download at about 5000 bytes/s
delay_parameters 2 5000/150000 5000/120000
We have set day to 09:00-23:59 before.
delay_access 2 allow day
delay_access 2 deny !day
delay_access 2 allow magic_words2
OK, when we have configured everything, we must make sure everything under /opt/squid and /cache directories belongs to user 'squid'.
mkdir /var/log/squid/
chown squid:squid /var/log/squid/
chmod 770 /var/log/squid/
chown -R squid:squid /opt/squid/
chown -R squid:squid /cache/
Now everything is ready to run Squid. When we do it for the first time, we have to create its cache directories
/opt/squid/bin/squid -z
We run Squid and check if everything is working. A good tool to do that is IPTraf; you can find it on [url]http://freshmeat.net[/url]. Make sure you have set the appropriate proxy in your web browsers (192.168.1.1, port 8080 in our example)
/opt/squid/bin/squid
If everything is working, we add /opt/squid/bin/squid line to the end of our initializing scripts. Usually,
it can be /etc/rc.d/rc.local.
Other helpful options in Squid may be /opt/squid/bin/squid -k reconfigure (it reconfigures Squid if we made any changes in its squid.conf file)
/opt/squid/bin/squid -help :) self-explanatory
You can also copy cachemgr.cgi to the cgi-bin directory of your WWW server, to make use of a useful Cache Manager.
3. Solving remaining problems
OK, we have installed Squid and configured it to use delay pools. I bet nobody wants to be restricted, especially our clever LAN users. They will likely try to avoid our limitations, just to download their favourite mp3s a little faster (and thus causing your headache).
I assume that you use IP-masquerade on your LAN so that your users could use IRC, ICQ, e-mail, etc. That's OK, but we must make sure that our LAN users will use our delay pooled Squid to access web pages and use ftp.
We can solve most of these problems by using ipchains (Linux 2.2.x kernels) or iptables (Linux 2.4.x kernels).
Linux 2.2.x kernels (ipchains) We must make sure that nobody will try to cheat and use a proxy server other than ours. Public proxies usually run on 3128 and 8080 ports:
/sbin/ipchains -A input -s 192.168.1.1/24 -d ! 192.168.1.1 3128 -p TCP -j REJECT
/sbin/ipchains -A input -s 192.168.1.1/24 -d ! 192.168.1.1 8080 -p TCP -j REJECT
We must also make sure that nobody will try to cheat and connect to the internet directly (IP-masquerade) to download web pages:
/sbin/ipchains -A input -s 192.168.1.1/24 -d ! 192.168.1.1 80 -p TCP -j REDIRECT 8080
If everything is working, we add these lines to the end of our initializing scripts. Usually, it can be /etc/rc.d/rc.local.
We might think to block ftp traffic (ports 20 and 21) to force our LAN users to use Squid, but it's not a good idea for at least two reasons:
Squid is a http proxy with ftp support, not a real ftp proxy. It can download from ftp, it can also upload to some ftp, but it can't delete/change name of files on remote ftp servers.
When we block ports 20 and 21, we won't be able to delete/change name of files on remote ftp servers.
IE5.5 has a bug -- it doesn't use a proxy to retrieve the ftp directory. Instead it connects directly via IP-masquerade.
When we block ports 20 and 21, we won't be able to browse through ftp directories, using IE5.5.
So, we will block excessive ftp downloads using other methods. We will deal with it in chapter 4.
(2) Linux 2.4.x kernels (iptables) We must make sure that nobody will try to cheat and use a proxy server other than ours. Public proxies usually run on 3128 and 8080 ports:
/sbin/iptables -A FORWARD -s 192.168.1.1/24 -d ! 192.168.1.1 --dport 3128 -p TCP -j DROP
/sbin/iptables -A FORWARD -s 192.168.1.1/24 -d ! 192.168.1.1 --dport 8080 -p TCP -j DROP
We must also make sure that nobody will try to cheat and connect to the internet directly (IP-masquerade) to download web pages:
/sbin/iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
If everything is working, we add these lines to the end of our initializing scripts. Usually, it can be /etc/rc.d/rc.local.
We might think to block ftp traffic (ports 20 and 21) to force our LAN users to use Squid, but it's not a good idea for at least two reasons:
Squid is a http proxy with ftp support, not a real ftp proxy. It can download from ftp, it can also upload to some ftp, but it can't delete/change name of files on remote ftp servers.
When we block ports 20 and 21, we won't be able to delete/change name of files on remote ftp servers.
IE5.5 has a bug -- it doesn't use a proxy to retrieve the ftp directory. Instead it connects directly via IP-masquerade.
When we block ports 20 and 21, our LAN users won't be able to browse through ftp directories, using IE5.5.
So, we will block excessive ftp downloads using other methods[/LEFT].
به امید خدا در ادامه به بهینه سازی و نصب ماژول برایwccp می پردازم.....
[URL=http://linuxsecurity.com/resource_files/firewalls/Bandwidth-Limiting-HOWTO/index.html]منبع و مقاله ای[/URL] که بیشترین سهم را در این چکیده داشت.
پاک کردن Objectی از Cache
همونطوری که مستحضرید squid در حالت عادی اجازه پاک کردن Objectهایی کهcache نموده است را به ما نمیدهد...اگر به دلایلی بخواهیم که Object کش شده ای را حذف کنیم لازم است که تغییراتی در squid.conf اعمال کنیم تا بتوانیم از تکنیکPURGE استفاده کنیم ( squid در حالت عادی اجازه استفاده از این تکنیک را نمی دهد)
بدین شکل عمل می کنیم که:
[CODE]
acl PURGE method purge
acl localhost src 127.0.0.1
http_access allow purge localhost
http_access deny purge
[/CODE]
بدین صورت ما به Localhost اجازه اعمال PURGE را داده ایم حال مثلا می خواهیمObjectهای سایت cnn.com را حذف کنیم :
[CODE]squidclient -m PURGE http://www.cnn.com/ [/CODE]
اگر عملPURGE موفقیت آمیز باشد ``200 OK'' مشاهده می کنیم:
[CODE]HTTP/1.0 200 OK
Date: Tue, 6 sep 2005 16:03:32 GMT
Server: Squid/2.4.1
[/CODE]
و اگر Objectی از این سایت در کش پیدا نشود چنین داریم:
[CODE]HTTP/1.0 404 Not Found
Date: Tue, 6 sep2005 16:03:22 GMT
Server: Squid/2.4.1
[/CODE]
حال به سادگی Object مربوط به هر سایت را میتوان از کش حذف کرد...
امیدوارم که این نکته مفید بوده باشه :)
refresh_pattern ی برای کش کردن Update های میکروسافت
دوستان عزیز:
یکی از مواردی که معمولا به صورت ناخواسته پهنای باند زیادی را به خود اختصاص می دهد Auto update ویندوز می باشد که در شبکه هایی با پهنای باند پایین می تواند مشکل ساز و عاملی دردسر ساز باشد...
اگر از الگوی refresh_pattern در تنظیمات squid استفاده کنیم می توانیم درصد زیادی از به روز شدن ها را نگهداری کنیم و در مصرف پهنای باند صرفه جویی کنیم:
[CODE]maximum_object_size 1280096 KB
refresh_pattern http://*.windowsupdate.microsoft.com/ 0 80% 20160
reload-into-ims
refresh_pattern http://office.microsoft.com/ 0 80% 20160 reload-into-ims
refresh_pattern http://windowsupdate.microsoft.com/ 0 80% 20160
reload-into-ims
refresh_pattern http://wxpsp2.microsoft.com/ 0 80% 20160 reload-into-ims
refresh_pattern http://xpsp1.microsoft.com/ 0 80% 20160 reload-into-ims
refresh_pattern http://w2ksp4.microsoft.com/ 0 80% 20160 reload-into-ims
refresh_pattern http://download.microsoft.com/ 0 80% 20160 reload-into-ims
refresh_pattern http://download.macromedia.com/ 0 80% 20160 reload-into-ims
refresh_pattern ftp://ftp.nai.com/ 0 80% 20160 reload-into-ims
refresh_pattern http://ftp.software.ibm.com/ 0 80% 20160 reload-into-ims[/CODE]
امیدوارم دوستان عزیز هم تجربیات خودشون رو در این زمینه بیان بفرمایند....
کنترل دانلود فایل های خاص
یکی از مواردی که معمولا به صورت مشکل در شبکه ها نمایان می شود؛دانلود آهنگ و بعضی فایل های خاص (مخصوصا در LAN) است که باعث مصرف شدید پهنای باند می شود...برای داشتن کنترل بیشتر به دانلودها و مدیریت پهنای باند دو روش پیشنهاد می گردد:
1-استفاده از delay_pools و تعیین حداکثر پهنای باند برای هر حجم از فایل های خاص بدین شکل:
[CODE]
acl Media urlpath_regex -i \.arj?$ \.au?$ \.asf?$ \.aif?$ \.avi?$ \.bz2?$ \.bin?$ \.cab?$ \.exe?$ \.iso?$ \.msi?$ \.mp3?$ \.mpg?$ \.mpe?$ \.mpeg?$ \.mp2?$ \.mov?$ \.qt?$ \.raw?$ \.ram?$ \.ra?$ \.rar?$ \.rm?$ \.rpm?$ \.tar?$ \.tar.gz?$ \.tgz?$ \.wmv?$ \.wma?$ \.wav?$ \.zip?$ \.mmf?$ \.sis?$ \.midi?$ \.3gp?$ \.psf?$
delay_pools 1
delay_class 1 2
delay_parameters 1 -1/-1 10000/3000000 5000/50000000 3000/10000000 1000/15000000
delay_access 1 allow Media
10 kb 3 mb
5 kb 5 mb
3 kb 10 mb
1 kb 15 mb[/CODE]
(به این صورت که به ازای دانلود فایل هایی با سایز کمتر از 3mb دانلود با سرعت 10kbشروع می شود و 5mb از 5kb و به همین نحوه 10mb از 3kb و 15mbاز 1kb)
و برای یک سرعت ثابت برای همه می توان از این الگو استفاده کرد:
[CODE]delay_pools 1
delay_class 1 2
delay_parameters 1 -1/-1 3000/3000
delay_access 1 allow Media [/CODE]
2-استفاده از محدودیت روی IP با reply_body_max_size:
[CODE]
acl SAMI src 159.159.9.162
reply_body_max_size 0 allow SAMI
reply_body_max_size 1000000 allow all
[/CODE]
در این مثال به جز یوزر samiکه می تواند به صورت نامحدود استفاده نمایدسایر یوزرها محدودیت دار ند...این روش برای مجزا کردن یوزرهای خاص بسیار عالی جواب می دهد....
....
کنترلSquid و مدیریت آن با Webmin
دوستان عزیز همانطوری که مستحضرید ابزرا قدرتمند [URL=http://www.webmin.com]Webmin[/URL] امکان مدیریت و کنترل گرافیکی بسیار گیرایی را برای تمامی سرور ها ی لینوکسی و خانواده یونیکس پدید می اورد... مطلب جذابی در کار با Squid توسط webmin یافتم که سعی می کنم به صورت فارسی ساده بیان کنم ؛به این امید که مورد توجه دوستان قرار بگیرد:
[B]جلوگیری از دانلود فایل های خاص:[/B]
1- به قسمت کنترل پانل squid در بخش Server های webmin می رویم.
2-صفحه Access Control را باز می کنیم.
3- ACL جدید دلخواهمان را با استفاده ازعبارت URL Path Regexp از منوی کشویی می سازیم.
[CENTER][IMG]http://photo-origin.tickle.com/image/21/9/7/O/21979779O668731538.jpg[/IMG] [/CENTER]
4- نام دلخواهی را به ACL ی که ساخته ایم نسبت می دهیم مثلا در اینجااز DenyFileTypesاستفاده کرده ایم:
[CENTER][IMG]http://photo-origin.tickle.com/image/21/9/7/O/21979788O316235360.jpg[/IMG]
[/CENTER]
5- در کنار گزینه Ignore case چک مارک می گذاریم و از منوی Regular Expressions فرمت فایل هایی که می خواهیم از دانلود شان جلوگیری کنیم انتخاب می نماییم برای مثال در اینجا از فایل های mp3 و exe جلوگیری می کنیم و extension های .mp3$ .exe$ را انتخاب می کنیم.
6- برای ویرایش فایل های خطا(Error files) که در صورت تلاش یوزری برای دانلود این فایلها نمایش داده می شوند؛ به صورت مد نظرتان توسط ویرایشگر vi یا هر ویرایشگر دیگری؛ در مسیر /etc/squid/errors/ فایل DENYFILE را ویرایش کنید.
[CENTER][IMG]http://photo.tickle.com/image/21/9/7/O/21979865O178562768.jpg[/IMG] [/CENTER]
این فایل به صورت یک فایل وب ویرایش شود مثلا:
[CENTER][IMG]http://photo.tickle.com/image/21/9/7/O/21979810O814288792.jpg[/IMG][/CENTER]
حال اسم فایل ویرایش شده DENYFILE را در Failure URL نوشته و access control را Save می کنیم.
7- حال کار تمام است و لازم است که با استفاده از Add proxy restriction link و انتخاب گزینه Deny این سیاست را هم توسط فلش های بالابر به سطر قبل از سیاست هایی که مجاز دانسته ایم قرار می دهیم.
[IMG]http://photo-origin.tickle.com/image/21/9/7/O/21979754O859496939.jpg[/IMG]
دیدید که چه ساده یکی از جالب ترین سیاست های مدیریتی را اعمال کردیم ؛امیدوارم که شما هم لذت برده باشید... :)