基于iptables+squid web缓存服务

小型企业的通常互联网宽带接入比较差的。公共上网带宽也不很充足的,再加上有一些下载或都视频流,对于web浏览将有严重的影响的。可能听过各类网络慢的声音。html

在以上的基本目标下,准备使用squid作透明代理,这样在用户不用改变任何的配置状况下,将有必定的网络浏览速度的提高的。nginx

基本系统:web

    Centos 6.7 内核:2.6.32.547vim

    双网卡bash

    iptables 1.4.7服务器

    squid 3.5.15网络

    开始安装配置app

yum install -y gcc make

    软件存放路径:/usr/local/src/dom

    安装路径:/usr/local/squid/ 
tcp

cd /usr/local/src/
wget 
tar -zxvf squid-3.5.15.tar.gz 
cd squid-3.5.15
./configure --prefix=/usr/local/squid /
 --enable-arp-acl /
 --enable-ssl /
 --with-aufs-threads=64 /
 --enable-ltdl-convenience 
 #参数说明请参考 ./configure --help
 make && make install 
 基本安装完成,未有错误。如遇到错误请自行找度妈与谷爸吧!

   以上安装完成之后的,软件安装位置:/usr/local/squid 下,目录结构以下:

 [root@nginx-master squid]# ll

total 24
drwxr-xr-x. 2 root  root  4096 Feb 26 16:49 bin 
drwxr-xr-x. 2 root  root  4096 Mar 29 15:25 etc  #配置文件
drwxr-xr-x. 2 root  root  4096 Feb 26 16:49 libexec
drwxr-xr-x. 2 root  root  4096 Feb 26 16:49 sbin   #squid 执行程序
drwxr-xr-x. 5 root  root  4096 Feb 26 16:49 share #文档相关
drwxrwxrwx. 5 squid squid 4096 Feb 26 16:49 var 目录cache与logs

   编辑配置文件

cd /etc
vim squid.conf
#
# Recommended minimum configuration:
#
#squid run user
cache_effective_user squid
# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
#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.8.0/24 # 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 game dstdomain games.sina.com.cn
acl sport url_regex -i ccc.x.jd.com
#------ 47jp
acl jp src 192.168.47.188
acl jp src 192.168.47.187
acl jp src 192.168.47.32
acl jp src 192.168.47.153
acl jp src 192.168.47.148
acl jp src 192.168.47.147
acl jp src 192.168.47.105
acl jp src 192.168.47.130
acl jp src 192.168.47.230
acl jp src 192.168.47.103
acl jp src 192.168.47.150
acl jp src 192.168.47.127
acl jp src 192.168.47.123
acl jp src 192.168.47.158
#------- 4-5man
acl man src 192.168.4.97
acl man src 192.168.4.99
acl man src 192.168.4.96
acl man src 192.168.4.101
acl man src 192.168.5.2
acl man src 192.168.5.3
acl man src 192.168.5.8
acl man src 192.168.5.20
acl man src 192.168.4.13


#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports

http_access deny game sport
# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow man
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
http_access deny all

# Squid normally listens to port 3128
http_port 192.168.10.2:3128 transparent  #默认配置为:http_port 3128 此时为代理模式 
http_port 3129 #系统日志有报错:增长此参数
# Hostname
visible_hostname ancc # 不加此参数时,系统启动时会有警告

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256
cache_dir ufs /home/data/cache 4096 16 256     #修改存储目录之后,要给目录增长相应权限
cache_dir ufs /home/data/cache0 4096 16 256
cache_dir ufs /home/data/cache1 4096 16 256
cache_dir ufs /home/data/cache2 4096 16 256
access_log /home/data/log/access.log
cache_store_log /home/data/log/store.log
cache_log /home/data/log/cache.log
cache_effective_user squid
cache_effective_group squid
# Leave coredumps in the first cache dir
coredump_dir /home/data/cache
#coredump_dir /usr/local/squid/var/cache/squid
#manager mail
cache_mgr xiexiangrong@ancc.com
cache_mem 800 MB
#20160322
#cache_swap_high 100%
#cache_swap_low 80%
maximum_object_size 1024 KB
#
# 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

以上是个人用的配置文件,详细的配置参数可参考以下网址:http://zyan.cc/book/squid/index.html

上面的配置中:我修改了cache目录与log的存储位置的到/home/data目录下,由于是测试了因此没有详细的规划硬盘的使用空间。若是实施时,须要进一步的规划磁盘空间。

配置文件完成,保存退出

cd /usr/local/squid/sbin/
./squid -z #初始化cache目录
./squid #启动程序
ps -axu | grep squid #查看服务是否正常启动
netstat -nat | grep 3128 #查看端口是否被监听

此配置为透明代理,须要开启路由转发与iptables NAT和端口转发

  1. 开启转发(永久的方法)

vim /etc/sysctl.conf 
# Controls IP packet forwarding
net.ipv4.ip_forward = 1 # 0 修改成1 保存退出
#需重启系统

[root@nginx-master sbin]# sysctl -p
net.ipv4.ip_forward = 1  #修改生效
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1

  iptables配置

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128
iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -j SNAT --to-source 192.168.8.10

iptables 根据你的实际IP来配置的。

实验遇到的QA:

Q:配置时,把好多功能都配置完了,启动时各类报错。

A:实际配置当中的,一个功能一个功能实际并测试,这样比较好查找故障的。

Q:squid启动时,提示cache 与 log 目录拒绝访问

A:查看配置文件内的cache_effective_user squid 而后对目录进行用户与权限的配置

Q:配置完代理服务器后,固然想测试一下服务器的性能的。

A:请参考:http://www.oschina.net/question/12_6110  我使用的siege,其它没有测试

测试结果以下:

squid
Transactions:                   2000 hits
Availability:                 100.00 %
Elapsed time:                   9.30 secs
Data transferred:               6.89 MB
Response time:                  0.04 secs
Transaction rate:             215.05 trans/sec
Throughput:                     0.74 MB/sec
Concurrency:                    7.70
Successful transactions:           0
Failed transactions:               0
Longest transaction:            0.54
Shortest transaction:           0.00

没有SQUID
Lifting the server siege...      done.
Transactions:                    956 hits
Availability:                  99.17 %
Elapsed time:                 396.96 secs
Data transferred:             109.31 MB
Response time:                 26.62 secs
Transaction rate:               2.41 trans/sec
Throughput:                     0.28 MB/sec
Concurrency:                   64.10
Successful transactions:         956
Failed transactions:               8
Longest transaction:           78.13
Shortest transaction:           2.62

有写的错误的地方,喜欢你们批评指正的。

努力写好每一篇文章,拒绝当转发党。严格测试每篇文章。

相关文章
相关标签/搜索