关于云计算: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/Ve…
如果你下载的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                    :::*                   users:(("squid",pid=6304,fd=10))

查看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服务器就只有一块,就不做演示。

阿里云K8s实战手册 K8s

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理