Linux系统通过Squid配置实现正向代理上网

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 wget
tar xf squid-4.8.tar.gz
cd 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 squid
chown -R squid.squid /usr/local/squid/var
ln -s /usr/local/squid/sbin/squid  /usr/local/sbin/

初始化并启动Squid

添加squid运行的用户及组

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

初始化缓存目录

[root@host-10-200-86-163 /]# squid -z
2019/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/squid
2019/08/08 17:04:40 kid1| Creating missing swap directories
2019/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 3128
LISTEN     0      128         :::3128                    :::* 

查看Squid的运行用户

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

创建服务启动脚本

vim /etc/init.d/squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/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/squid
chkconfig --add squid
chkconfig --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 squid
tcp6       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

创建传统代理

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

# And finally deny all other access to this proxy
http_access allow all       #在deny all前添加allow all
http_access deny all

# Squid normally listens to port 3128
http_port 3128              #squid对外端口
cache_mem 128 MB            #指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_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 dir
coredump_dir /usr/local/squid/var/cache/squid

#
# 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
cache_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 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      8774/(squid-1)  

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

[root@sx-sj-mcn-redis-1 ~]# curl www.baidu.com -I
curl: (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 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Thu, 08 Aug 2019 12:40:01 GMT
ETag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from host-10-200-86-163
Via: 1.1 host-10-200-86-163 (squid/4.8)
Connection: keep-alive

永久设置代理

#在/etc/profile中全局设置的最后添加以下配置
[root@sx-sj-mcn-vgateway-2 ~]# vim /etc/profile
export proxy=http://10.200.86.163:3128
export 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 -I
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Thu, 08 Aug 2019 12:50:07 GMT
ETag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from host-10-200-86-163
Via: 1.1 host-10-200-86-163 (squid/4.8)
Connection: keep-alive

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

透明代理

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值