8.1  Squid安装

$ sudo apt-get install squid

 

FATAL: Could not determine fully qualified hostname.  Please set 'visible_ hostname'

8.2           Squid配置主机名

先备份配置文件

$ 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

8.3  访问控制列表

 

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

8.4  正向代理

 

8.4.1  设置端口号

http_port 8888

8.4.2          禁止某些IP地址上网

方法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

8.4.3          禁止在某时间段上网

使用time类型的acl,可以定义上网时间。只允许上午910点之间上网

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

8.4.4          个别网站的控制

首先创建文件

$ 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

8.4.5  NCSA做密码认证squid自带的ncsa_auth功能可以读取兼容NCSA的密码文件,通过该功能,用户在通过正确的用户名、密码才能使用代理服务器上网)

创建文件,权限其他只读

$ 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

8.4.6  透明代理的设置

1.服务器网卡配置(一个内网一个外网)

$ 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

3Squid的透明代理配置 

修改/etc/squid/squid.conf文件,添加在http_port后添加transparent就可以

http_port 192.168.1.10:3128 transparent

4iptables防火墙的配置(通过acl来应用代理)

$ 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

 

5.保存iptables规则

将防火墙规则保存到/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

8.5  反向代理

 

8.5.1  Squid反向代理单个后台Web服务器

1WebSquid在同一台机器上(squid的主要配置)

Web服务器监听81端口。

http_port 80 vhost vport

cache_peer 127.0.0.1 parent 81 0 no-query originserver

2WebSquid在不同的机器上(squid的主要配置)

此时web服务器不需要些端口

http_port 80 vhost vport

cache_peer 221.214.14.185 parent 80 0 no-query originserver
保存完后直接重启就可以

8.5.2  Squid反向代理多个后台Web服务器

下面是需要缓存的服务器地址

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

8.6  Squid排错

 

8.6.1  Squid运行状态检查

查看squid的运行状态

$ sudo squid -NCd1

2009/06/22 09:56:26| Squid is already running!  Process ID 4832

文章整理来自于《Ubuntu Server最佳方案》书籍

由于字数限制,笔记可能无法全部上传,请下载笔记