Monday, March 22, 2010

Squid Configuration - How to build simple Proxy Server

Proxy server เป็น Server ชนิดหนึ่ง ซึ่งมีคุณสมบัติดังนี้

1. Bandwidth management
ทำหน้าที่เก็บรวบรวมข้อมูลของหน้าเวบเพจต่าง ๆ ที่ผู้ใช้งานเรียกใช้ และนำมาแสดงผลให้ผู้ใช้คนอื่น ๆ ถ้าเรียกไปยังเวบเดียวกันในช่วงเวลาไม่นานนักหลังจากที่มีการเรียกใช้ไปแล้วครั้งแรก โดยที่ไม่ต้องออกไปที่เวบนั้นบ่อย ๆ ทั้งนี้ เพื่อเป็นการประหยัด Bandwidth ที่ใช้งานนั่นเอง
2. Authentication
เราสามารถที่จะเก็บข้อมูลการเข้าไปใช้งาน Internet ของผู้ใช้ได้ ว่า ผู้ใช้งานเข้าใช้งานเวบไซท์ไหนมากที่สุด และในช่วงเวลาใดบ้าง รวมถึงการทำ Authentication ก่อนเข้าใช้งานเวบไซท์ ต่าง ๆ ได้ด้วย เพื่อที่จะสามารถระบุตัวตนของคนที่เข้าไปใช้งานเวบไซท์ต่าง ๆ ได้
3. Restriction
สามารถที่จะ Block เวบไซท์ที่ไม่ต้องการให้ผู้ใช้งานเข้าไปใช้งานได้

Package สำหรับการทำ Proxy server ที่เป็นที่นิยมมากตัวหนึ่งคือ Squid ปัจจุบันเป็น Version 3 แล้ว

สิ่งที่ต้องเตรียมการ :
1. Computer สำหรับทำเป็น Proxy server 1 เครื่อง
2. OS Installation สามารถเข้าไปดูได้ที่ หรือจะทำเองก็ได้ ตามใจชอบ
http://itprosolution.blogspot.com/2010/01/debian-5-minimal-installation-part-1.html
http://itprosolution.blogspot.com/2010/01/debian-5-minimal-installation-part-2.html
http://itprosolution.blogspot.com/2009/08/debian-5-post-installations.html

ขั้นตอนการติดตั้ง (Debian only ถ้าใช้ Distro อื่น ๆ ก็คล้าย ๆ กัน)
1. ติดตั้ง squid3 โดยใช้ Aptitude

aptitude install squid3

2. แก้ไขไฟล์/etc/squid3/squid.conf

Configuration file (/etc/squid3/squid.conf)

auth_param basic program /usr/lib/squid3/squid_ldap_auth -v 3 -b "ou=user,o=ldap.example.com" -f "uid=%s" 1.1.1.1 #ถ้าไม่ทำ Authen หรือถ้าไม่มี LDAP Server ก็ไม่ต้องใส่

auth_param basic children 5
auth_param basic realm Web-Proxy
auth_param basic credentialsttl 1 minute

acl ldap-auth proxy_auth REQUIRED #ถ้าไม่ทำ Authen หรือถ้าไม่มี LDAP Server ก็ไม่ต้องใส่


http_access allow ldap-auth #ถ้าไม่ทำ Authen หรือถ้าไม่มี LDAP Server ก็ไม่ต้องใส่ให้ใส่ว่า http_access allow all แทน
http_access deny all #ถ้าไม่ทำ Authen หรือถ้าไม่มี LDAP Server ก็ไม่ต้องใส่


http_port 7890 # อันนี้เป็น port ของ Proxy เปลี่ยนได้นะ จะเอา 8080 ก็ได้

hierarchy_stoplist cgi-bin ?


cache_mem 1024 MB
maximum_object_size_in_memory 8 MB
memory_replacement_policy heap LFUDA
ipcache_high 95
ipcache_low 90

cache_dir aufs /var/spool/squid3 2048 16 256
maximum_object_size 32 MB
cache_replacement_policy heap LFUDA
store_dir_select_algorithm round-robin
cache_swap_high 95
cache_swap_low 90

ie_refresh on
vary_ignore_expire on

cache_access_log /var/log/squid3/access.log squid
cache_log /var/log/squid3/cache.log
cache_store_log none
logfile_rotate 90
pid_filename /var/run/squid3.pid
buffered_logs off
strip_query_terms off

pipeline_prefetch on

ftp_passive on
ftp_sanitycheck on

#cache deny QUERY
quick_abort_min 0 KB
quick_abort_max 0 KB

refresh_pattern -i \.(asp\?|aspx\?|php\?) 0 20% 720
refresh_pattern -i \.(asp|aspx|php)$ 0 20% 720
refresh_pattern -i \.(jpg|gif|jpeg|png|css|js|bmp|tif|tiff|xbm|png|swf)$ 0 100% 14400
refresh_pattern -i \.(exe|zip|gz|arj|lha|lzh|rar|tgz|tar|Z)$ 4320 80% 43200 override-lastmod reload-into-ims ignore-reload
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern . 0 20% 4320

prefer_direct on

half_closed_clients off

cache_mgr taveesak@gmail.com
visible_hostname http://itprosolution.blogspot.com



เสร็จแล้ว อย่าลืม Restart Squid ล่ะ

/etc/init.d/squid3 restart

เวลาใช้งานทำได้ 2 แบบคือ

1. ถ้ามี Firewall อยู่ในองค์กร ก็ตั้งให้ใครที่จะออกไปยัง Port 80 ให้วิ่งผ่านมาที่ Proxy server นี้อย่างเดียว
2. ถ้าไม่มี Firewall ก็เข้าไปตั้งค่า Proxy server ใน Browser ให้ชี้้มาที่ Proxy server ตัวนี้ วิธีการก็ลองเข้าไปหาดูใน Google นะ



#Code Reference : http://www.linuxthai.org/forum/index.php?topic=472.0