设为首页 收藏本站
查看: 1679|回复: 0

[经验分享] 基于iptables+squid web缓存服务

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-3-30 14:04:46 | 显示全部楼层 |阅读模式
小型企业的一般互联网宽带接入比较差的。公共上网带宽也不很充足的,再加上有一些下载或都视频流,对于web浏览将有严重的影响的。可能听过各种网络慢的声音。
在以上的基本目标下,准备使用squid做透明代理,这样在用户不用改变任何的配置情况下,将有一定的网络浏览速度的提升的。
基本系统:
    Centos 6.7 内核:2.6.32.547
    双网卡
    iptables 1.4.7
    squid 3.5.15
    开始安装配置
1
yum install -y gcc make



    软件存放路径:/usr/local/src/
    安装路径:/usr/local/squid/  

1
2
3
4
5
6
7
8
9
10
11
12
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 下,目录结构如下:
[iyunv@nginx-master squid]# ll

1
2
3
4
5
6
7
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



   编辑配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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目录下,因为是测试了所以没有详细的规划硬盘的使用空间。如果实施时,需要进一步的规划磁盘空间。
配置文件完成,保存退出
1
2
3
4
5
cd /usr/local/squid/sbin/
./squid -z #初始化cache目录
./squid #启动程序
ps -axu | grep squid #查看服务是否正常启动
netstat -nat | grep 3128 #查看端口是否被监听



此配置为透明代理,需要开启路由转发与iptables NAT和端口转发
  • 开启转发(永久的方法)


1
2
3
4
5
6
7
8
9
10
11
vim /etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 1 # 0 修改为1 保存退出
#需重启系统

[iyunv@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配置
1
2
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
有写的错误的地方,喜欢大家批评指正的。
努力写好每一篇文章,拒绝当转发党。严格测试每篇文章。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-197681-1-1.html 上篇帖子: squid 启动报错 下篇帖子: squid实例配置之静态cdn
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表