Linux下配置Squid代理服务器


Linux下配置Squid代理服务器

1、什么是squid

Squid cache(简称为Squid)是一个流行的自由软件(GNU通用公共许可证)的代理服务器和Web缓存服务器。Squid有广泛的用途,从作为网页服务器的前置cache服务器缓存相关请求来提高Web服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。Squid主要设计用于在Unix一类系统运行。
Squid的发展历史相当悠久,功能也相当完善。除了HTTP外,对于FTP与HTTPS的支援也相当好,在3.0 测试版中也支援了IPv6。
squid可以做代理也可以做缓存;

squid缓存不仅可以节省宝贵的带宽资源,也可以大大降低服务器的I/O
squid不仅可以做正向代理,又可以做反向代理。
正向代理,squid后面是客户端,客户端上网要通过Squid去上;反向代理,squid后面是服务器,服务器返回给用户数据需要走squid。
正向代理用在企业的办公环境中,员工上网需要通过squid代理来上网,这样可以节省网络带宽资源。而反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,它用于网站架构中。

2、搭建squid正向代理
官方网站为 http://www.squid-cache.org/ 
安装命令:yum install -y squid
squid -v  查看版本以及编译参数(Squid Cache: Version 3.1.10)
> /etc/squid/squid.conf    清空配置文件;
vim /etc/squid/squid.conf
加入如下配置:
http_port 3128
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
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.0.0/16 # RFC1918 possible internal network
acl SSL_ports port 443
acl Safe_ports port 80 8080
acl Safe_ports port 21
acl Safe_ports port 443
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localnet
http_access allow localhost
http_access allow all
cache_dir aufs /data/cache 1024 16 256
cache_mem 128 MB
hierarchy_stoplist cgi-bin ?
coredump_dir /var/spool/squid
refresh_pattern ^ftp:          1440    20%    10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0    0%      0
refresh_pattern \.(jpg|png|gif|mp3|xml) 1440    50%    2880    ignore-reload
refresh_pattern .              0      20%    4320
######################################################### 到此结束

配置解释:
acl Safe_ports port 80 8080        # http的端口
acl Safe_ports port 21          # ftp的端口
acl Safe_ports port 443        # https的端口
cache_dir aufs /data/cache 1024 16 256    #缓存空间1024M大小 16个一级目录,256个子目录
cache_mem 128 MB    #缓存可以使用的内存大小;放在内存中访问数据速度快;


mkdir  /data/cache    #创建缓存目录
chown -R squid:squid /data/cache    #更改缓存目录权限
squid -z    #初始化缓存目录,squid新版本3.1可以省略
/etc/init.d/squid start    #启动squid服务
squid  -k check    #可以检测配置文件是否有错;可以简写为-kche
squid -k rec    #可以重新加载配置,reconfig的简写;
service squid restart    #重启squid服务;重启经常性的很慢,可以先killall squid,在启动服务;

检测配置文件,报错:Could not determine this machines public hostname. Please configure one or set 'visible_hostname'.没有定义公共主机名,需要配置visible_hostname 可视化主机名;(squid出问题,会在浏览器显示squid的主机名)
在配置文件中加入:visible_hostname yonglinux 就不会报错;
1234 [root@yonglinux ~]# squid -k check
2015/05/25 03:09:18| WARNING: Could not determine this machines public hostname. Please configure one or set 'visible_hostname'.
2015/05/25 03:09:18| WARNING: Could not determine this machines public hostname. Please configure one or set 'visible_hostname'.
squid: ERROR: No running copy

在另一台linux进行测试:curl -x192.168.22.30:3128 www.qq.com   
指定代理服务器192.168.22.30的3128端口访问网站,前提保证代理服务器能访问网站;
设定代理服务器的作用是让局域网的用户访问网站速度快,另一方面可以控制用户访问哪些网站;上班期间禁止员工看视频,购物;

访问图片,测试缓存,缓存的时间,X-Cache为HIT击中,说明squid缓存起作用;第一次为MISS;
[root@localhost ~]# curl -x192.168.22.30:3128 '

Squid:实现高速的Web访问

CentOS 6.2 编译安装Squid 配置反向代理服务器

简单配置Squid代理和反向代理

CentOS 6.4下DNS+Squid+Nginx+MySQL搭建高可用Web服务器

Squid 的详细介绍:请点这里
Squid 的下载地址:请点这里

本文永久更新链接地址

相关内容