Squid是什么

Squid是一种用来缓冲Internet数据的软件。它接受来自人们需要下载的目标(object)的请求并适当地处理这些请求。也就是说,如果一个人想下载一web页面,他请求Squid为他取得这个页面。Squid随之连接到远程服务器(比如:http://squid.nlanr.net) 并向这个页面发出请求。然后,Squid显式地聚集数据到客户端机器,而且同时复制一份。当下一次有人需要同一页面时,Squid可以简单地从磁盘中读到它,那样数据迅即就会传输到客户机上。当前的Squid可以处理HTTP,FTP,GOPHER,SSL和WAIS等协议。但它不能处理如POP,NNTP,RealAudio以及其它类型的东西。

我这里的常用于做服务器的统一出口,把squid作为能够出公网的设备,然后为所有需要出公网的服务器进行代理设置,从而带动内网服务器能够上网,但是我们上网也是仅仅使用公网的yum源以及公网的一些技术资源。

Squid的基本类型

传统代理 也就是普通的代理服务,,必须在客户端的浏览器、QQ聊天工具、下载软件等程序中手动设置代理服务器的地址和端口,然后才能使用代理服务来访问网络。对于网页浏览器,访问网站时的域名解析请求也会发送给指定的代理服务器。

透明代理 提供与传统代理相同的功能和服务,其区别在于客户机不需要指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将web访问重定向,实际上仍然交给代理服务器来处理。重定向的过程对于客户机来说是“透明”的,用户甚至并不知道自己在使用代理服务,所以称为“透明代理”。

Squid部署

下载地址:http://www.squid-cache.org/Versions/v4/squid-4.8.tar.gz 如果你下载的Squid v3 版本,则任何 C++ 编译器都可以,如果你下载的是Squid v4或者更高版本,那么就需要 C++11 的编译器。

yum install libtool-ltdl-devel libxml2-devel libcap-devel perl gcc autoconf automake make sudo wgettar xf squid-4.8.tar.gzcd squid-4.8./configure --prefix=/usr/local/squid --enable-arp-acl --enable-linux-netfilter --enable-linux-tproxy --enable-async-io=100 --enable-err-language="Simplify_Chinese" --enable-underscore --enable-poll --enable-gnuregex
参数解释:./configure:检查你的系统编译器是否可用--preifx:指定安装路径--enable-arp-acl:可以在规则中设置为直接通过客户端MAC进行管理,防止客户端使用IP欺骗--enable-linux-netfilter:使用内核过滤--enable-linux-tproxy:支持透明模式--enable-async-io=100:异步I/O,提升存储性能,相当于 --enable-pthreads   --enable-storeio=ufs,aufs--enable-err-language="Simplify_Chinese":报错时显示的语音,这里指定为Chinese--enable-underscore:允许URL中有下划线--enable-poll:使用Poll()模式,提升性能--enable-gnuregex:使用GUN正则表达式
make && make install
useradd -M -s /sbin/nologin squidchown -R squid.squid /usr/local/squid/varln -s /usr/local/squid/sbin/squid  /usr/local/sbin/

初始化并启动Squid

添加squid运行的用户及组

echo 'cache_effective_user squid' >> /usr/local/squid/etc/squid.confecho 'cache_effective_group squid' >> /usr/local/squid/etc/squid.conf

初始化缓存目录

[root@host-10-200-86-163 /]# squid -z2019/08/08 17:04:40| Created PID file (/usr/local/squid/var/run/squid.pid)[root@host-10-200-86-163 /]# 2019/08/08 17:04:40 kid1| Set Current Directory to /usr/local/squid/var/cache/squid2019/08/08 17:04:40 kid1| Creating missing swap directories2019/08/08 17:04:40 kid1| No cache_dir stores are configured.2019/08/08 17:04:40| Removing PID file (/usr/local/squid/var/run/squid.pid)

启动Squid

[root@host-10-200-86-163 /]# squid[root@host-10-200-86-163 /]# ss -anplt | grep 3128LISTEN     0 128 :::3128 :::*                   users:(("squid",pid=6304,fd=10))

查看Squid的运行用户

[root@host-10-200-86-163 /]# ps -ef|grep squidroot      6302 1 0 17:05 ? 00:00:00 squidsquid     6304 6302 0 17:05 ? 00:00:00 (squid-1) --kid squid-1squid     6305 6304 0 17:05 ? 00:00:00 (logfile-daemon) /usr/local/squid/var/logs/access.logroot      6322 30305 0 17:06 pts/7 00:00:00 grep --color=auto squid

创建服务启动脚本

vim /etc/init.d/squid#!/bin/bash#chkconfig: 2345 90 25PID="/usr/local/squid/var/run/squid.pid"CONF="/usr/local/squid/etc/squid.conf"CMD="/usr/local/squid/sbin/squid"
case "$1" in   start)     netstat -natp | grep squid &> /dev/null if [ $? -eq 0 ] then       echo "squid is running" else       echo "正在启动 squid..."       $CMD fi ;;   stop)     $CMD -k shutdown &> /dev/null #这里可以仔细看下     rm -rf $PID &> /dev/null ;;   status) [ -f $PID ] &> /dev/null if [ $? -eq 0 ] then            netstat -natp | grep squid else            echo "squid is not running" fi ;;   restart)      $0 stop &> /dev/null      echo "正在关闭 squid..."         $0 start &> /dev/null      echo "正在启动 squid..."      $CMD fi ;;   stop)     $CMD -k shutdown &> /dev/null #这里可以仔细看下     rm -rf $PID &> /dev/null ;;   status) [ -f $PID ] &> /dev/null if [ $? -eq 0 ] then            netstat -natp | grep squid else            echo "squid is not running" fi ;;   restart)      $0 stop &> /dev/null      echo "正在关闭 squid..."         $0 start &> /dev/null      echo "正在启动 squid..." ;;   reload)      $CMD -k reconfigure ;;   check)      $CMD -k parse ;; *)      echo "用法:$0{start|stop|status|reload|check|restart}" ;;esac

加入开机启动

chmod +x /etc/init.d/squidchkconfig --add squidchkconfig --level 35 squid on

脚本测试

[root@host-10-200-86-163 init.d]# netstat -anplt | grep squid[root@host-10-200-86-163 init.d]# service squid start正在启动 squid...[root@host-10-200-86-163 init.d]# netstat -anplt | grep squidtcp6       0 0 :::3128 :::*                    LISTEN      8260/(squid-1) [root@host-10-200-86-163 init.d]# service squid stop[root@host-10-200-86-163 init.d]# netstat -anplt | grep squid

创建传统代理

主要修改下图中所圈出的内容

-w703

# And finally deny all other access to this proxyhttp_access allow all       #在deny all前添加allow allhttp_access deny all
# Squid normally listens to port 3128http_port 3128 #squid对外端口cache_mem 128 MB            #指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4reply_body_max_size 10 MB   #允许用户下载的最大文件大小,以字节为单位。默认设置0表示不进行限制maximum_object_size 4096 KB #允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户# Uncomment and adjust the following to add a disk cache directory.#cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256
# Leave coredumps in the first cache dircoredump_dir /usr/local/squid/var/cache/squid
## Add any of your own refresh_pattern entries above these.#refresh_pattern ^ftp: 1440 20% 10080refresh_pattern ^gopher: 1440 0% 1440refresh_pattern -i (/cgi-bin/|\?) 0 0% 0refresh_pattern . 0 20% 4320cache_effective_user squid  #squid运行用户cache_effective_group squid #squid运行组

重启Squid

#进行配置检查[root@host-10-200-86-163 init.d]# /usr/local/squid/sbin/squid -k reconfigure[root@host-10-200-86-163 init.d]# /usr/local/squid/sbin/squid -k check
#重启[root@host-10-200-86-163 init.d]# service squid restart正在关闭 squid...正在启动 squid...
[root@host-10-200-86-163 init.d]# netstat -anplt | grep 3128tcp6       0 0 :::3128 :::*                    LISTEN      8774/(squid-1)

设置Linux服务器内网上网 重新找一台内网的linux服务器 没有设置代理上网前,去curl百度是失败的

[root@sx-sj-mcn-redis-1 ~]# curl www.baidu.com -Icurl: (6) Could not resolve host: www.baidu.com; Unknown error

临时设置代理

[root@sx-sj-mcn-redis-1 ~]# export proxy=http://10.200.86.163:3128;             #proxy=http代理http协议的请求[root@sx-sj-mcn-redis-1 ~]# export http_proxy="http://10.200.86.163:3128";[root@sx-sj-mcn-redis-1 ~]# export https_proxy="http://10.200.86.163:3128"; #https=proxy代理https协议的请求

临时设置代理后再次curl百度

[root@sx-sj-mcn-redis-1 ~]# curl www.baidu.com -I                          HTTP/1.1 200 OKAccept-Ranges: bytesCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformContent-Length: 277Content-Type: text/htmlDate: Thu, 08 Aug 2019 12:40:01 GMTETag: "575e1f60-115"Last-Modified: Mon, 13 Jun 2016 02:50:08 GMTPragma: no-cacheServer: bfe/1.0.8.18X-Cache: MISS from host-10-200-86-163Via: 1.1 host-10-200-86-163 (squid/4.8)Connection: keep-alive

永久设置代理

#在/etc/profile中全局设置的最后添加以下配置[root@sx-sj-mcn-vgateway-2 ~]# vim /etc/profileexport proxy=http://10.200.86.163:3128export http_proxy="http://10.200.86.163:3128"export https_proxy="http://10.200.86.163:3128"export ftp_proxy="http://10.200.86.163:3128"
[root@sx-sj-mcn-vgateway-2 ~]# source /etc/profile[root@sx-sj-mcn-vgateway-2 ~]# curl www.baidu.com -IHTTP/1.1 200 OKAccept-Ranges: bytesCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformContent-Length: 277Content-Type: text/htmlDate: Thu, 08 Aug 2019 12:50:07 GMTETag: "575e1f60-115"Last-Modified: Mon, 13 Jun 2016 02:50:08 GMTPragma: no-cacheServer: bfe/1.0.8.18X-Cache: MISS from host-10-200-86-163Via: 1.1 host-10-200-86-163 (squid/4.8)Connection: keep-alive

注意:yum使用的话,我们把我们squid服务器的yum源拷贝到Linux内网设备中然后指定yum makecache生成缓存就可以执行

透明代理

透明代理需要squid服务器拥有两块网卡,我这里的squid服务器就只有一块,就不做演示