$ sudo apt-get install squid
FATAL: Could not determine fully qualified hostname. Please set 'visible_ hostname'
先备份配置文件
$ sudo cp /etc/squid/squid.conf /etc/squid/squid.conf.backup
$ sudo chmod a-w /etc/squid/squid.conf.backup
查看默认配置
$ cd /etc/squid/
$ sudo cat squid.conf.backup | grep -v ^$ | grep -v ^# | sudo tee squid.conf
修改主机名(不是必须的)
$ sudo nano /etc/squid/squid.conf
visible_hostname ubproxy
$ sudo /etc/init.d/squid restart
acl name type value1 value2 ...
例如:
acl NormalUsers src 192.168.1.0/24, 192.168.2.0/24
acl NormalUsers src 192.168.1.0/24
acl NormalUsers src 192.168.2.0/24
http_access deny NormalUser
http_port 8888
方法1
acl WorkShop src 192.168.1.0-192.168.2.0/24
方法2
acl WorkShop src 192.168.1.0/24
acl WorkShop src 192.168.2.0/24
方法3
acl WorkShop src 192.168.1.0/24, 192.168.2.0/24
方法1效率最高
http_access deny WorkShop
使用time类型的acl,可以定义上网时间。只允许上午9到10点之间上网
acl NormalUsers src 192.168.1.0/24
acl WorkingHours time D 09:00-10:00
http_access deny !WorkingHours NormalUsers
上诉配置中,最后一行也可以这样写
http_access allow NormalUsers WorkingHours
首先创建文件
$ sudo nano /etc/squid/allowedSites.list
写入允许访问的网站
hiweed.com
aixingzou.cn
写入禁止访问的网站
$ sudo nano /etc/squid/deniedSites.list
www.illegalsite.com
abcdef.com
列表创建好后,来创建acl和访问规则
acl office_network src 192.168.1.0/24
acl GoodSites dstdomain "/etc/squid/allowedSites.list"
acl BadSites dstdomain "/etc/squid/denySites.list"
http_access deny BadSites
http_access allow office_network GoodSites
创建文件,权限其他只读
$ sudo touch /etc/squid/auth-password
$ sudo chmod o+r /etc/squid/auth-password
使用apache自带的htpasswd命令向文件添加用户和密码
$ sudo htpasswd /etc/squid/auth-password username
New password:
Re-type new password:
Adding password for user username
找到配置文件在哪个地方
$ dpkg -L squid | grep ncsa_auth
/usr/lib/squid/ncsa_auth
修改/etc/squid/squid.conf,将密码验证的配置加进去,REQUIRED来强制squid使用NCSA进行用户验证
# 定义认证程序和密码文件的位置
auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/auth-password
# 定义派生认证进程的数量
auth_param basic children 5
# 要求输入用户名和密码时显示的信息
auth_param basic realm Please Login First
# 每隔 2 小时就重新认证一次
auth_param basic credentialsttl 2 hours
# 大小写敏感:关闭(对用户名不区分大小写)
auth_param basic casesensitive off
acl ncsa_users proxy_auth REQUIRED
http_access allow ncsa_users
$ cat /etc/network/interfaces
auto eth1
iface eth1 inet static
address 192.168.1.10
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
修改/etc/squid/squid.conf文件,添加在http_port后添加transparent就可以
http_port 192.168.1.10:3128 transparent
$ iptables --list
默认没有配置
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
将所有的HTTP(80)的访问请求都转发到squid服务的3128端口上
$ sudo iptables -t nat -A PREROUTING -i eth1 -p tcp \
--dport 80 -j REDIRECT --to-port 3128
$ sudo iptables -A INPUT -j ACCEPT -m state \
--state NEW,ESTABLISHED,RELATED -i eth1 -p tcp \
--dport 3128
$ sudo iptables -A OUTPUT -j ACCEPT -m state \
--state NEW,ESTABLISHED,RELATED -o eth0 -p tcp \
--dport 80
$ sudo iptables -A INPUT -j ACCEPT -m state \
--state ESTABLISHED,RELATED -i eth0 -p tcp \
--sport 80
$ sudo iptables -A OUTPUT -j ACCEPT -m state \
--state ESTABLISHED,RELATED -o eth1 -p tcp \
--sport 80
$ iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere state NEW,RELATED,ESTABLIS HED tcp dpt:3128
ACCEPT tcp -- anywhere anywhere state RELATED,ESTABLISHED tcp spt:www
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere state NEW,RELATED,ESTABLIS HED tcp dpt:www
ACCEPT tcp -- anywhere anywhere stateRELATED,ESTABLISHED tcp spt:www
将防火墙规则保存到/etc/iptables.rules文件
$ sudo sh -c "iptables-save > /etc/iptables.rules"
开机自动应用防火墙规则
pre-up iptables-restore < /etc/iptables.rules
开机自动保存防火墙规则
post-down iptables-save -c > /etc/iptables.rules
auto eth1
iface eth1 inet static
address 192.168.1.10
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
pre-up iptables-restore < /etc/iptables.rules
post-down iptables-save -c > /etc/iptables.rules
Web服务器监听81端口。
http_port 80 vhost vport
cache_peer 127.0.0.1 parent 81 0 no-query originserver
此时web服务器不需要些端口
http_port 80 vhost vport
cache_peer 221.214.14.185 parent 80 0 no-query originserver
保存完后直接重启就可以
下面是需要缓存的服务器地址
202.108.9.79 news.163.com
61.135.163.87 news.baidu.com
209.85.175.99 news.google.com
修改dns服务器的配置,将域名全部指向缓存服务器
192.168.1.10 news.163.com
192.168.1.10 news.baidu.com
192.168.1.10 news.google.com
在缓存服务器的hosts文件,放缓存服务器知道真实服务器的IP地址
202.108.9.79 news.163.com
61.135.163.87 news.baidu.com
209.85.175.99 news.google.com
将下面配置添加到squid.conf文件,使其同时反向代理上面的网站
acl ServerIPs dst 202.108.9.79 61.135.163.87 209.85.175.99
acl ServerDomains dstdomain news.163.com news.baidu.com news.google.com
always_direct allow ServerDomains
never_direct allow !ServerDomains
http_access allow ServerIPs
http_access allow ServerDomains
查看squid的运行状态
$ sudo squid -NCd1
2009/06/22 09:56:26| Squid is already running! Process ID 4832