Squid+iptables代理配置
Squid+iptables代理配置
Squid代理服務器簡介
Squid是一種在Linux系統下使用的優秀的代理服務器軟件。
squid不僅可用在Linux系統上,還可以用在AIX、Digital Unix、FreeBSD、HP-UX、Irix、NetBSD、Nextstep、SCO和Solaris等系統上。
Squid是一個緩存internet數據的一個軟件,它接收用戶的下載申請,并自動處理所下載的數據。也就是說,當一個用戶象要下載一個主頁時,它向Squid發出一個申請,要Squid替它下載,然后Squid 連接所申請網站并請求該主頁,接著把該主頁傳給用戶同時保留一個備份,當別的用戶申請同樣的頁面時,Squid把保存的備份立即傳給用戶,使用戶覺得速度相當快。
對于Web用戶來說,Squid是一個高性能的代理緩存服務器,可以加快內部網瀏覽Internet的速度,提高客戶機的訪問命中率。Squid不僅支持HTTP協議,還支持FTP、gopher、SSL和WAIS等協議。
實驗環境:
系統 redhat linux 5.4
外網 eth0:192.168.0.1
內網 eth1:192.168.1.254
Squid Cache: Version 3.1.7
RPM安裝,這里就不說明了。
修改配置文件
- [root@a ~]# cat /etc/squid/squid.conf
#p#
配置文件說明
定義acl訪問控制元素:ip地址、端口
- acl manager proto cache_object
- acl localhost src 127.0.0.1/32 ::1
- acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
- 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 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
- acl worktime time 8:00-23:59 //定義工作時間
- acl worktime time 00:00-5:59
- http_access allow mynetwork !worktime //只允許非工作時間上網
#p#
訪問控制設置
- http_access allow manager localhost
- http_access deny manager
- # Deny requests to certain unsafe ports
- http_access deny !Safe_ports 禁止非安全端口的訪問
- # Deny CONNECT to other than secure SSL ports
- http_access deny CONNECT !SSL_ports
- http_access allow localnet
- http_access allow localhost
- # And finally deny all other access to this proxy
- http_access allow all ***一條http_access設置默認訪問規則
- # Squid normally listens to port 3128
- http_port 192.168.1.254:3128 transparent 代理服務器監聽的地址及端口,transparent 為透明代理模式
- # We recommend you to use at least the following line.
- hierarchy_stoplist cgi-bin ?
- # Uncomment and adjust the following to add a disk cache directory.
- cache_dir ufs /var/spool/squid 1000 8 128 配置緩存文件的文件格式,緩存大小,緩存目錄數
- # Leave coredumps in the first cache dir
- coredump_dir /var/spool/squid
- # Add any of your own refresh_pattern entries above these.
- 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
- visible_hostname 192.168.1.254
- cache_mem 128 MB //squid服務器占用內存大小
- forwarded_for off //不傳遞被代理地址
- via off //不傳遞代理服務器信息
初始化squid代理服務器
- squid -z
開啟路由功能,并將下面的命令寫入/etc/rc.d/rc.local 文件,使其開機自動開啟路由功能
- echo '1' >/proc/sys/net/ipv4/ip_forward
#p#
配置iptables防火墻
自動將http請求轉發到代理服務器上
- iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128
設置源地址映射
- iptables -t nat -A POSTROUTING -j SNAT --to-source 192.168.1.254 (外網地址)
- iptables -P INPUT ACCEPT
啟動squid代理服務
- [root@a ~]# service squid restart
查看squid代理服務器緩存日志
- [root@a ~]# tail -f /var/log/squid/access.log
個人感覺使用Squid+iptables緩存服務器之后網頁訪問速度有大幅度的加快。
【編輯推薦】