Selasa, 11 Oktober 2005

Belajar Squid Proxy

Squid mungkin merupakan HTTP Proxy yang paling tangguh di linux. Squid dapat membantu kita menghemat bandwidth dengan dua cara:

1.Cara pertama, adalah karakteristik utama proxy server, dengan cara menyimpan halaman, gambar, dan objek lain yang telah didownload sebelumnya ke dalam memory atau disk. Sehingga, jika seseorang melakukan request dengan halaman yang sama, proxy akan mengambil halaman yang telah disimpan tersebut.

2.Terpisah dari penyimpanan cache yang umum dilakukan, Squid mempunyai fasilitas spesial yang disebut dengan delay pools. Dengan delay pools dimungkinkan untuk membatasi internet traffic dengan cara yang masuk akal, tergantung 'kata kunci' yang ada di URL, Kata kunci tersebut dapat berupa '.mp3', '.exe', atau '.avi', dan lain-lain. semua kata-kata di URL (seperti .avi) dapat dijadikan sebagai kata kunci.

Dengan demikian, kita dapat menugaskan Squid untuk mendownload tiga macam file tersebut dengan kecepatan yang telah ditentukan (seperti pada contoh, sekitar 5 kbytes/s). Jika pada LAN kita, dua user atau lebih mendownload file pada saat yang sama, file tersebut akan didownload sekitar 5 kbytes/s secara bersamaan, sehingga tidak mempengaruhi bandwidth untuk WWW, e-mail, news, irc, dan lain-lain.

Tentu saja internet tidak hanya digunakan untuk mendownload file melalui web (http atau ftp), selanjutnya, kita akan melakukan membatasan bandwidth untuk napster, realaudio, dan kemungkinan-kemungkinan lain.

Installasi Squid dengan fasilitas delay pools

Seperti yang telah disebutkan di atas, Squid memiliki fasilitas delay pools, yang memungkinkan kita mengontrol download bandwidth. Sayangnya, di kebanyakan distribusi, Squid dikompile tanpa fasilitas ini.

Jadi, apabila anda memiliki Squid yang telah terinstall, anda harus menginstallnya kembali dengan cara sebagai berikut:

1.Untuk mendapatkan performa maksimal dari Squid, kita perlu membuat partisi terpisah yang digunakan sebagai cache, seperti /cache/. Ukuran partisi sekitar 200 MB, atau tergantung dari kebutuhan.
Jika anda tidak mengetahui bagaimana membuat partisi baru, anda dapat meletakkan cache di partisi utama, tetapi performa Squid akan menurun.

2.Kita tambahkan user squid:
# useradd -d /cache/ -r -s /dev/null squid > dev/null 2>&1
Dimaksudkan, user squid tidak bisa digunakan untuk login

3.Download source Squid di http://www.squid-cache.org/. Ketika dokumen ini ditulis, versi terakhir dari squid adalah Squid 1.4 stable 1:
http://www.squid-cache.org/versions/v2/2.4/squid-2.4.STABLE1-src.tar.gz

4.Unpack source:
# tar xvzf squid-2.4.STABLE1-src.tar.gz

5.Kompilasi dan install Squid:
# ./configure --prefix=/opt/squid --exec-prefix=/opt/squid \
--enable-delay-pools --enable-cache-diggests --enable-poll \
--disable-ident-lookups --enable-truncate --enable-removal-policies

# make all
# make install

Konfigurasi Squid menggunakan delay pools

Konfigurasikan file squid.conf (/opt/squid/etc/squid.conf):

#squid.conf
#Every option in this file is very well documented in the original squid.conf file
#and on http://www.visolve.com/squidman/Configuration%20Guide.html
#
#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
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
#all our LAN users will be seen by external servers
#as if they all use Mozilla on Linux :)
anonymize_headers deny User-Agent
fake_user_agent Mozilla/5.0 (X11; U; Linux 2.4.4 i686)
#To make our connection even faster, we put a line similar
#to the one below. Don't forget to change the server to your closest!
#Measure pings, traceroutes and so on.
#Make sure that http and icp ports are correct
#cache_peer w3cache.icm.edu.pl parent 8080 3130 no-digest default
#This is useful when we want to use the Cache Manager
#copy cachemgr.cgi to cgi-bin of your www server
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 http://www.squid-cache.org
#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
#We don't block .html, .gif, .jpg and similar files, because they
#generally don't consume much bandwidth
#We have two different delay_pools
delay_pools 2
#First delay pool
#W don't want to delay our local traffic
#There are three pool classes; here we will deal only with the second
delay_class 1 2
#-1/-1 mean that there are no limits
delay_parameters 1 -1/-1 -1/-1
#magic_words1: 192.168
delay_access 1 allow magic_words1
#Second delay pool
#we want to delay downloading files mentioned in magic_words2
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
delay_access 2 allow magic_words2
#EOF

Setelah squid dikonfigurasikan, kita harus memastikan bahwa direktori /opt/squid dan /cache dimiliki oleh user 'squid':

# chown -R squid:squid /opt/squid/
# chown -R squid:squid /cache/
atau

# chown -R squid.squid /opt/squid/
# chown -R squid.squid /cache/

Sekarang kita siap untuk menjalankan squid, untuk menjalankan squid saat pertama kali, kita harus membuat cache direktori:

# /opt/squid/usr/bin/squid -z

Kemudian jalankan squid, dan periksa apakah semua berhasil dengan baik. Program bantu yang berguna untuk melihat bandwidth adalah IPTraf, anda dapat mendownloadnya di http://freshmeat.net/. Pastikan juga anda telah memasukkan alamat proxy di web browser anda (pada contoh: 192.168.1.1, port 8080).
Untuk menjalankan squid:

# /opt/squid/usr/bin/squid
Apabila semua berjalan, tambahkan /opt/squid/usr/bin/squid pada baris terakhir initial script, biasanya di /etc/rc.d/rc.local.

Option lain yang dapat membantu:

# /opt/squid/usr/bin/squid -k reconfigure
(reconfigures Squid jika kita melakukan perubahan pada squid.conf).

Anda dapat juga mengkopi file cachemgr.cgi ke direktori cgi-bin pada web server anda.

Tidak ada komentar: