资讯专栏INFORMATION COLUMN

利用RouterOS配合squid实现透明缓存

Andrman / 2766人阅读

摘要:系统版本安装并开机启动方式安装在级别上自动运行服务修改配置文件并修改为如下配置其他相关命令检查命中率查看访问情况查看命中情况

系统版本:Cent OS 6.9

1.安装 squid 并开机启动

yum -y install squid    //yum 方式安装
chkconfig --level 35 squid on             //在3、5级别上自动运行squid服务
  

2.修改squid 配置文件并修改为如下配置 vi /etc/squid/squid.conf

# Recommended minimum Access Permission configuration:

# Only allow cachemgr access from localhost
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

# 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 3128  transparent

# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /mnt/squid 1000000 16 256
cache_mem 300000 MB
maximum_object_size_in_memory 1 MB
maximum_object_size 2 GB
max_filedesc 45000
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %h" "%{User-Agent}>h" %Ss:%Sh
access_log /var/log/squid/access.log combined
cache_log /var/log/squid/cache.log
hierarchy_stoplist cgi-bin ?

#acl QUERY urlpath_regex cgi-bin ?
acl BIGMEDIA urlpath_regex -i .rmvb$ .avi$ .mpg$ .mkv$ .rm$

#acl WEBMAIL dstdom_regex -i "/usr/local/squid/etc/webmails"
#cache deny WEBMAIL
#cache deny QUERY
cache deny BIGMEDIA
cache allow all
cache_swap_high 95
cache_swap_low 90
cache_mgr  123@123.com
cache_effective_user squid
quick_abort_min -1 KB
range_offset_limit -1
refresh_pattern -i .htm$ 5 20% 1440
refresh_pattern -i .html$ 5 20% 1440
refresh_pattern -i .jpeg$ 60 50% 4320 reload-into-ims
refresh_pattern -i .jpg$ 60 50% 4320 reload-into-ims
refresh_pattern -i .png$ 60 50% 4320 reload-into-ims
refresh_pattern .flv?start 0 0% 0
refresh_pattern .flv?ref  0 0% 0
refresh_pattern .f4v?ref  0 0% 0
refresh_pattern .mp4?ref  0 0% 0
refresh_pattern .flv? 1440 99% 10080 reload-into-ims ignore-reload ignore-no-cache
refresh_pattern .flv$ 1440 99% 10080 reload-into-ims ignore-reload ignore-no-cache
refresh_pattern .f4v? 1440 99% 10080 reload-into-ims ignore-reload ignore-no-cache
refresh_pattern .mp4? 1440 99% 10080 reload-into-ims ignore-reload ignore-no-cache
refresh_pattern .m4v? 1440 99% 10080 reload-into-ims ignore-reload ignore-no-cache
refresh_pattern .swf$ 1440 99% 10080 reload-into-ims ignore-reload ignore-no-cache

# Leave coredumps in the first cache dir
coredump_dir /home/squid01

# Add any of your own refresh_pattern entries above these.
#refresh_pattern .(jpg|png|gif|mp3|xml)1440    50%     2880   ignore-reload
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    

其他相关命令

检查命中率    squidclient  -h 127.0.0.1 -p 3128 mgr:info
查看访问情况    tail -f /var/log/squid/access.log
查看命中情况    tail -f /var/log/squid/access.log | grep HIT

3.检查 squid 配置是否正常,如没问题则启动服务

squid -z  //根据反馈提示修改配置文件
service squid start
chkconfig squid on

4.编辑iptables 文件,插入如下配置并重启

vi /etc/sysconfig/iptables
service iptables restart 

*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A PREROUTING -s 192.168.1.0/24 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128  //重定向ros路由过来的内网80端口的流量到 squid的3128端口   根据实际情况调整来源网段
COMMIT

注意:如果缓存使用外部挂载存储,请关闭 SElinux

setenforce 0 && sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

RouterOS 上面的配置 根据实际情况修改为IP地址

ip route add dst-address=0.0.0.0/0 gateway=192.168.1.2 routing-mark=squid check-gateway=ping  //添加一条默认路由到代理服务器并添加标签为squid,用ping的方式检查网关  根据实际情况调整来源网段
ip firewall address-list add address=192.168.1.1 list=noproxy    //加入网关地址和代理服务器地址
ip firewall address-list add address=192.168.1.2 list=noproxy    
ip firewall mangle add src-address=192.168.1.0/24 dst-address-list=noproxy protocol=tcp dst-port=80 action=mark-routing new-routing-mark=squid    //把来源内网段80端口的流量转发到代理服务器上面并排除之前加入的网关和服务器IP

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/62017.html

相关文章

  • 运维-服务器缓存之varnish

    摘要:如带有如下头信息,,默认不会缓存。是一款高性能开源的反向代理服务器和缓存服务器,其开发者是核心的开发人员之一。备份复制备份用默认端口开启你会得到如下信息我们看到,已经开启,但是除了知道这个东西已经监听端口外,我们不知道发生了什么。 varnish 小草新blog: http://homeway.me/ 一、介绍下吧: 首先了解几个概念, 1. Varnish不缓存带有...

    leanote 评论0 收藏0
  • 后端好书阅读与推荐(续)

    摘要:续前文后端好书阅读与推荐,几十天过去了,又看了两本好书还有以前看过的书,这里依然把它们总结归纳一下,加入一些自己的看法有用的链接和可能的延伸阅读,并推荐给需要的同学。 续前文 后端好书阅读与推荐 - Mageek`s Wonderland ,几十天过去了,又看了两本好书(还有以前看过的书),这里依然把它们总结归纳一下,加入一些自己的看法、有用的链接和可能的延伸阅读,并推荐给需要的同学。...

    刘福 评论0 收藏0

发表评论

0条评论

Andrman

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<