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 就不会报错;
 
[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 'http://www.51cto.com/images/home/images/logo.jpg' -I
HTTP/1.0 200 OK
Server: Tengine
Date: Sun, 24 May 2015 13:42:43 GMT
Content-Type: image/jpeg
Content-Length: 5309
Last-Modified: Wed, 22 Jan 2014 07:55:12 GMT
Expires: Sun, 31 May 2015 13:42:43 GMT
Cache-Control: max-age=604800
Load-Balancing: web39
Accept-Ranges: bytes
Age: 29661
X-Cache: HIT from yonglinux
X-Cache-Lookup: HIT from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

设置squid代理服务器只代理某几个域名

设置域名白名单,允许baidu sohu可以访问,其他都拒绝;

vim /etc/squid/squid.conf    下面的内容加入到squid配置文件acl下面;

acl http proto HTTP

acl good_domain dstdomain .baidu.com .sohu.com

http_access allow http good_domain

http_access deny http !good_domain

使用curl测试白名单,baidu、sohu返回状态码为200 OK,qq不在白名单则返回403;
 
[root@localhost ~]# curl -x192.168.22.30:3128 www.sohu.com -I
HTTP/1.0 200 OK
Content-Type: text/html
Date: Sun, 24 May 2015 13:57:32 GMT
Server: SWS
Vary: Accept-Encoding
Cache-Control: no-transform, max-age=120
Expires: Sun, 24 May 2015 13:59:32 GMT
Last-Modified: Sun, 24 May 2015 13:57:21 GMT
X-RS: 11172604.20347654.12509576
FSS-Cache: HIT from 9861864.17726194.11198816
X-Cache: MISS from yonglinux
X-Cache-Lookup: MISS from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive
 
[root@localhost ~]# curl -x192.168.22.30:3128 www.qq.com -I
HTTP/1.0 403 Forbidden
Server: squid/3.1.10
Mime-Version: 1.0
Date: Sun, 24 May 2015 22:04:30 GMT
Content-Type: text/html
Content-Length: 3254
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from yonglinux
X-Cache-Lookup: NONE from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

限制某些域名不能通过代理访问

设置域名黑名单,不允许访问taobao.com jd.com;其他的都允许;

vim /etc/squid/squid.conf  下面的内容加入到squid配置文件acl下面

acl http proto HTTP

acl bad_domain dstdomain .taobao.com .jd.com

http_access deny http bad_domain

使用curl测试黑名单,taobao、jd返回状态码为403,51cto不在黑名单返回200 OK;
 
[root@localhost ~]# curl -x192.168.22.30:3128 www.taobao.com -I
HTTP/1.0 403 Forbidden
Server: squid/3.1.10
Mime-Version: 1.0
Date: Sun, 24 May 2015 21:35:22 GMT
Content-Type: text/html
Content-Length: 3266
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from yonglinux
X-Cache-Lookup: NONE from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive
 
[root@localhost ~]# curl -x192.168.22.30:3128 www.jd.com -I
HTTP/1.0 403 Forbidden
Server: squid/3.1.10
Mime-Version: 1.0
Date: Sun, 24 May 2015 21:35:32 GMT
Content-Type: text/html
Content-Length: 3254
X-Squid-Error: ERR_ACCESS_DENIED 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from yonglinux
X-Cache-Lookup: NONE from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

[root@localhost ~]# curl -x192.168.22.30:3128 www.51cto.com -I
HTTP/1.0 200 OK
Server: Tengine
Date: Sun, 24 May 2015 13:31:21 GMT
Content-Type: text/html
Vary: Accept-Encoding
Load-Balancing: web39
X-Cache: MISS from yonglinux
X-Cache-Lookup: MISS from yonglinux:3128
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

使用IE浏览器测试,需要设置代理服务器,菜单栏——工具——Internet选项——连接——局域网设置,勾选代理服务器——高级,填写squid代理服务器地址和端口号;

访问jd.com taobao.com,提示错误,访问被拒绝,由之前定义的可视化主机名发出的;访问其他网站正常;

3、搭建squid反向代理

vim /etc/squid/squid.conf  #如下变更

之前增加的域名白/黑名单相关配置去掉;

http_port 3128 改为 http_port 80 accel vhost vport

增加如下内容:

cache_peer 14.17.42.40 parent 80 0 originserver name=a

cache_peer 180.97.33.107 parent 80 0 originserver name=b

cache_peer_domain a www.qq.com

cache_peer_domain b www.baidu.com

监听的3128端口改为80端口;IE浏览器代理服务器的端口也要更改为80;

14.17.42.40为ping www.qq.com的ip地址;

如果是squid要代理一台web上的所有域名,那么就写成这样: cache_peer 192.168.10.111 80 0 originserver  #只需要这一行,cache_peer_domain  可以省略;

/etc/init.d/squid restart

IE浏览器测试反向代理,访问baidu.com、qq.com可以访问,其他网站提示:目前无法将您的请求进行转送操作

使用curl测试

[root@localhost ~]# curl -x192.168.22.30:80 www.qq.com -I
HTTP/1.0 200 OK
Server: squid/3.4.1
Date: Sun, 24 May 2015 14:22:47 GMT
Content-Type: text/html; charset=GB2312
Vary: Accept-Encoding
Vary: Accept-Encoding
Expires: Sun, 24 May 2015 14:23:47 GMT
Cache-Control: max-age=60
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Cache:
HIT from shenzhen.qq.com
X-Cache:
MISS from yonglinux
X-Cache-Lookup: MISS from yonglinux:80
Via: 1.0 yonglinux (squid/3.1.10)
Connection: keep-alive

访问qq.com返回HIT from shenzhen.qq.com,说明qq.com本身也做了反向代理;

squid还有很多配置,后续会陆续更新;

Linux入门教程:Squid代理服务器:http://www.linuxdiyf.com/linux/10528.html

RHEL6.4搭建Squid代理服务器:http://www.linuxdiyf.com/linux/10655.html

10个关于Linux中Squid代理服务器的实用面试问答:http://www.linuxdiyf.com/linux/10443.html

CentOS 6.4下Squid代理服务器的安装与配置:http://www.linuxdiyf.com/linux/7048.html

linux下配置squid 服务器,最简单使用方式相关推荐

  1. Linux下配置Smba服务器

    文章目录 1 Linux下配置Smba服务器 1 Linux下配置Smba服务器 Ubuntu安装Samba 服务器: 确认安装: dpkg -l | grep samba 安装: sudo apt- ...

  2. Linux下配置DNS服务器之一--Master服务器

    Linux下配置DNS服务器之一--Master服务器 系统环境: RedHat EL55 Oracle 11g RAC 集群中引入了SCAN(Single Client Access Name)的概 ...

  3. linux下pppoe服务器,Linux下配置pppoe服务器

    Linux下配置pppoe服务器 发布时间:2007-04-28 00:51:28来源:红联作者:readywin 系统环境: fedora 4 2.6.11 i386 rp-pppoe-3.5-27 ...

  4. 虚拟机linux ftp慢,虚拟机Linux下配置FTP服务器的方法

    虚拟机Linux下配置FTP服务器的方法 1.确保虚拟机系统与宿主系统是桥接设置,以方便连接. 2.在虚拟机系统中安装ftp服务器,我安装的是vsftpd服务器.由于安装的虚拟机系统CentOS 中已 ...

  5. linux下配置samba服务器(以CentOS6.7为例)

    一.简介(百度百科) Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共 ...

  6. linux 关闭rsync服务器,linux下配置rsync服务器和实时同步

    安装:rpm –ivh rsync-XXXXXX.RPM也可以用YUM 启动rsync必须要装xinetd,它是靠它启动的,端口是873 设定: /etc/xinetd.d/rsync:即#vi /e ...

  7. Linux下配置Samba服务器使用SMBv1或SMBv2协议

    修改配置文件 使用命令:man smb.conf,查看Samba所支持的协议级别 编辑Samba配置文件:vi /etc/samba/smb.conf 在smb.conf文件中找到[global],在 ...

  8. Linux下配置NTP时间服务器

    2019独角兽企业重金招聘Python工程师标准>>> Linux下配置NTP服务器 一.前言: Network Time Protocol(NTP)是用来使计算机时间同步化的一种协 ...

  9. linux服务器的功能需求,Linux下的各种服务器技术及配置 (毕业论文).doc

    Linux下的各种服务器技术及配置 (毕业论文) PAGE 26 PAGE 27 Linux下的各种服务器技术及配置 姓 名: 学 号: 指导老师: 系 名: 专 业: 班 级: . 二00一二年 十 ...

最新文章

  1. CSS+HTML大白
  2. 基于Docker Compose搭建的Mysql8.0主从复制(1主3从,多主机)
  3. 小白的入门之——汇编语言程序设计教程
  4. 石化行业应急指挥系统
  5. win7 计算机定时关机脚本,win7怎么定时关机?win7定时关机设置两种方法
  6. pytorch参数dim为-1的解释
  7. android 文件管理器打开方式,android怎么用浏览器打开浏览器文件?
  8. 【ML on Kubernetes】第 3 章:探索 Kubernetes
  9. 路由器的类型及衡量路由器性能的主要参数指标
  10. 中国企业软件公司的转型之路
  11. python如何爬有道翻译_如何利用Python网络爬虫来获取有道翻译翻译接口--手机版的哦!...
  12. 后凯恩斯学派给出的不同答案-中国视角下的宏观经济
  13. php读取excel效率,PhpSpreadsheet VS Box\Spout读取excel性能对比
  14. 推断性统计部分(三)---假设检验
  15. 京东2017实习生招聘试题 静态方法
  16. Java-AOP(Hook)实现机制(JDK/cglib动态代理/ASM/Javassist/AspectJ)
  17. 行测:判断推理(图形推理)
  18. thhinkphp5前后端分离微信公众号支付
  19. X64位游戏逆向入门之魔兽
  20. 【Trex】Trex初始化配置和server/client的启动

热门文章

  1. matlab找距离最近的元素,如何用MATLAB找到给定坐标的最近点?
  2. 如何禁止chrome浏览器http自动转成https 【转】
  3. IP地址转换函数——inet_pton inet_ntop inet_aton inet_addr inet_ntoa
  4. Java串口通信具体解释
  5. 旧文重发:程序员的七种武器
  6. python中lambda()的用法_python中lambda()的用法
  7. 转换文档参数_1分钟教会你将Excel转换成Word,简单高效,办公人士必备神技
  8. kvm架构详解--理解CPU、内存、IO虚拟化技术、处理器硬件支持
  9. GroupCoordinator分析
  10. 基于xilinx FPGA实现LZW压缩算法