侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

用DNSPod和Squid打造自己的CDN (七) 配置Squid

技术  /  管理员 发布于 7年前   269

第 7 章 配置Squid
1.设置hosts表
首先我们要明白一下squid在CDN中扮演的角色。squid在CDN中其实只是一个代理+缓存。跟一般的代理服务器类似,squid代替用户向真正有内容的服务器进行请求,并且缓存下来。所不同的是,squid是作为一个类似apache一样的web服务器存在,用户访问的时候并不感觉到是访问了一台代理服务器,而是一个真实的web服务器。一般情况下,我们把这种方式称之为“反向代理”。
所以,我们需要让squid知道内容要到哪里去取。因为我们已经把域名naizhao.com设置了智能DNS,而且这台squid服务器也是网通的,按照正常的dns解析,会把naizhao.com解析为2.2.2.2这个IP(也就是网通机器本身)。但这个IP上面并不存在任何所需要的内容,所有需要的内容其实是在1.1.1.1上(源服务器)。所以我们需要为naizhao.com固定一个IP,也就是1.1.1.1。
让系统得到这个IP的方法有两种,一种是设置hosts表,另外一种是系统里面搭建一个dns服务器。Windows下大家应该会经常去编辑到hosts文件,linux下同样也存在这个文件,我们在前面的vim教程中已经提到过。搭建dns系统这个方法工程太过庞大,还是编辑hosts文件相对来说比较简单,所以我们就采用编辑hosts表的方法。
linux下面的hosts表位于/etc/hosts,格式跟Windows大致相同,打开后我们把下面一行加到末尾

1.1.1.1         naizhao.com www.naizhao.com cnc.naizhao.com

如果已经按照前面的教程,添加过1.1.1.1这条记录的,修改为上面的样子。
2.设置squid
squid的配置文件位于/usr/local/squid/etc/squid.conf,打开后会有英文的详细说明。暂时我们不需要去理解这个文件里面各个配置的含义,相信大家看到一大堆英文都会头大的。大家只需要知道如何把squid按照自己的需求跑起来,并且不出错就好。
我们先用rm命令把squid.conf删掉。不用怕删掉后找不回来,还有一个默认的squid.conf.default在呢。一般的linux程序,包括apache等,都会有一个.default的默认文件,方便大家恢复默认设置,或者作为一个修改的参考。
squid.conf删掉后,我们再用vi squid.conf,新建这个文件,然后把下面的内容帖进去,保存
http_port 80 vhost vport=80
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
cache_mem 1024 MB
maximum_object_size 4096 KB
minimum_object_size 0 KB
maximum_object_size_in_memory 256 KB
cache_dir ufs /var/spool/squid 20480 16 256
cache_swap_low 80
cache_swap_high 97
strip_query_terms off
request_body_max_size 5 MB
memory_pools on
memory_pools_limit 50 MB
access_log none
cache_log /var/log/squid/cache.log
cache_store_log none
pid_filename /var/run/squid.pid
hosts_file /etc/hosts
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i naizhao\.com          240     100%    1440    ignore-reload
refresh_pattern -i www\.naizhao\.com           240     100%    1440    ignore-reload
refresh_pattern -i .gif 180 20% 10080 override-expire ignore-reload reload-into-ims
refresh_pattern -i .jpg 180 20% 10080 override-expire ignore-reload reload-into-ims
refresh_pattern .                           120  50%    1440
acl all src 0.0.0.0/0.0.0.0
acl p-manager proto cache_object
acl s-localhost src 127.0.0.1/255.255.255.255
acl d-localhost dst 127.0.0.0/8
acl d-domains dstdomain .naizhao.com
acl p-ssl  port 443 563
acl p-safe port 80 443 563
acl m-conn method CONNECT
acl m-purge method PURGE
acl n-maxconn maxconn 15
http_access allow p-manager s-localhost
http_access allow m-purge
http_access deny  !p-safe
http_access deny  m-conn !p-ssl
http_access deny  n-maxconn
http_access allow p-manager
http_access allow d-domains
http_access deny  all
http_reply_access allow all
acl r-url urlpath_regex realtime
cache deny r-url
icp_access allow all
follow_x_forwarded_for allow all
acl_uses_indirect_client offrange_offset_limit -1
dns_timeout 2 seconds
forward_timeout 10 seconds
connect_timeout 10 seconds
peer_connect_timeout 6 seconds
read_timeout 10 seconds
request_timeout 6 seconds
persistent_request_timeout 16 seconds
cache_mgr [email protected]
cache_effective_user nobody
cache_effective_group nobody
visible_hostname cnc.naizhao.com
logfile_rotate 0
always_direct allow all
error_directory /usr/local/squid/share/errors/Simplify_Chinese
coredump_dir /var/spool/squid


需要说明的几个参数:
cache_mem 1024 MB (squid使用的内存大小,注意必须要留下500M左右的内存给系统使用,建议系统内存2G,然后分配1G给squid)

cache_dir ufs /var/spool/squid 20480 16 256 (缓存的目录,放在/var/spool/squid下面,大小是20G,第一级目录是16个,第二级目录是256个。因为一个目录下面放置的文件和目录数量有限制,所以这里必须要设置多个目录,以适合squid存放大量文件的需要。目录数千万别设置太大,否则会引起系统死机)

memory_pools_limit 50 MB (内存池,用来存放缓存文件的链表,方便高速查找文件存放的对应位置,不需要太大。不懂就别管了)

cache_log /var/log/squid/cache.log(log文件的存放位置,必须要有读写权限,稍后说到)

refresh_pattern -i naizhao\.com          240     100%    1440    ignore-reload
refresh_pattern -i www\.naizhao\.com           240     100%    1440    ignore-reload
refresh_pattern -i .gif 180 20% 10080 override-expire ignore-reload reload-into-ims
refresh_pattern -i .jpg 180 20% 10080 override-expire ignore-reload reload-into-ims
(上面的几行指定了文件缓存的时间,可以指定某个域名,也可以指定某个文件类型。有更加多的域名或者文件类型就按照需要添加吧。-i是忽略大小写,naizhao.com和.gif是文件类型,240是最小缓存的时间,1440是最大缓存时间,都是分钟。这些先不用管,有需要的时候调整就好了。我们的口号是:先跑起来)

acl d-domains dstdomain .naizhao.com(允许访问的域名列表。如果要跑多个网站就添加多几个,用空格分开。naizhao.com前面有个英文的.,表示所有*.naizhao.com都允许。如果没有这个点,那么就只允许naizhao.com,访问www.naizhao.com就会提示访问被拒绝。)

cache_mgr [email protected] (管理员的邮件地址,改为自己的)

cache_effective_user nobody
cache_effective_group nobody
(用来跑squid的用户和用户组。为了避免使用root帐号造成的不安全因素,大部分linux程序都会先用root运行,然后马上把运行的用户从root切换到其他用户,这样就算程序有漏洞被黑了,黑客也只能得到这个普通用户的权限,对系统的危害将大大降低。保持默认即可)

visible_hostname cnc.naizhao.com (服务器输出的机器名字,多台squid分布的时候这个很重要,通过设置多个不同的名字,很容易就可以找出哪台服务器有问题)

coredump_dir /var/spool/squid (squid挂掉后,临终遗言要放到哪里。不用管,一般人看不懂)

修改好配置文件后,我们还需要做一些最后的工作。
先建一个log目录,用来存放log文件。按照squid.conf的设置,我们输入下面的命令

mkdir /var/log/squid
chown nobody:nobody /var/log/squid

chown命令是设置所有者和组。刚才说了,squid将会使用用户nobody组nobody来运行。nobody:nobody冒号前面的是用户,后面的是用户组。
还有squid的缓存目录,同样需要设置权限为nobody:nobody

chown nobody:nobody /var/spool/squid

查看命令是否成功,可以使用ls -lh

ls -lh /var/spool/
……
drwxr-xr-x 3 nobody nobody 4.0K Jun 15 08:19 squid

剩下最后一步,初始化squid的缓存目录结构。(也就是刚才说的16个一级目录,256个二级目录。本身这些文件夹不存在,需要通过squid的命令建立)

squid的命令在/usr/local/squid/sbin下,我们进入这个目录,然后看看squid命令的帮助

./squid -h
Usage: squid [-hvzCDFNRYX] [-d level] [-s | -l facility] [-f config-file] [-u port] [-k signal]
       -d level  Write debugging to stderr also.
       -f file   Use given config-file instead of
                 /usr/local/squid/etc/squid.conf
       -h        Print help message.
       -k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse
                 Parse configuration file, then send signal to
                 running copy (except -k parse) and exit.
       -s | -l facility
                 Enable logging to syslog.
       -u port   Specify ICP port number (default: 3130), disable with 0.
       -v        Print version.
       -z        Create swap directories
       -C        Do not catch fatal signals.
       -D        Disable initial DNS tests.
       -F        Don't serve any requests until store is rebuilt.
       -N        No daemon mode.
       -R        Do not set REUSEADDR on port.
       -S        Double-check swap during rebuild.
       -X        Force full debugging.
       -Y        Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.

我们可以看到-z命令就是建立缓存目录的,所以我们输入

./squid -z
2007/07/07 07:52:03| Creating Swap Directories

没有任何错误提示,说明目录已经建立成功了,我们可以用 ls /var/spool/squid看看效果。

最后,我们让squid跑起来

 

 

./squid -Nd 10

2007/07/07 07:53:50| Starting Squid Cache version 2.6.STABLE13 for i686-pc-linux-gnu...
2007/07/07 07:53:50| Process ID 3135
2007/07/07 07:53:50| With 1024 file descriptors available
2007/07/07 07:53:50| Using poll for the IO loop
2007/07/07 07:53:50| Performing DNS Tests...
2007/07/07 07:53:50| Successful DNS name lookup tests...
2007/07/07 07:53:50| DNS Socket created at 0.0.0.0, port 1026, FD 4
2007/07/07 07:53:50| Adding nameserver 211.147.6.3 from /etc/resolv.conf
2007/07/07 07:53:50| Adding nameserver 61.233.9.9 from /etc/resolv.conf
2007/07/07 07:53:50| Unlinkd pipe opened on FD 8
2007/07/07 07:53:50| Swap maxSize 20971520 KB, estimated 1613193 objects
2007/07/07 07:53:50| Target number of buckets: 80659
2007/07/07 07:53:50| Using 131072 Store buckets
2007/07/07 07:53:50| Max Mem  size: 1048576 KB
2007/07/07 07:53:50| Max Swap size: 20971520 KB
2007/07/07 07:53:50| Store logging disabled
2007/07/07 07:53:50| Rebuilding storage in /var/spool/squid (DIRTY)
2007/07/07 07:53:50| Using Least Load store dir selection
2007/07/07 07:53:50| Set Current Directory to /var/spool/squid
2007/07/07 07:53:50| Loaded Icons.
2007/07/07 07:53:52| Accepting accelerated HTTP connections at 0.0.0.0, port 80, FD 7.
2007/07/07 07:53:52| Accepting ICP messages at 0.0.0.0, port 3130, FD 9.
2007/07/07 07:53:52| WCCP Disabled.
2007/07/07 07:53:52| Ready to serve requests.
2007/07/07 07:53:52| Done scanning /var/spool/squid (0 entries)
2007/07/07 07:53:52| Finished rebuilding storage from disk.
2007/07/07 07:53:52|         0 Entries scanned
2007/07/07 07:53:52|         0 Invalid entries.
2007/07/07 07:53:52|         0 With invalid flags.
2007/07/07 07:53:52|         0 Objects loaded.
2007/07/07 07:53:52|         0 Objects expired.
2007/07/07 07:53:52|         0 Objects cancelled.
2007/07/07 07:53:52|         0 Duplicate URLs purged.
2007/07/07 07:53:52|         0 Swapfile clashes avoided.
2007/07/07 07:53:52|   Took 2.0 seconds (   0.0 objects/sec).
2007/07/07 07:53:52| Beginning Validation Procedure
2007/07/07 07:53:52|   Completed Validation Procedure
2007/07/07 07:53:52|   Validated 0 Entries
2007/07/07 07:53:52|   store_swap_size = 0k
2007/07/07 07:53:53| storeLateRelease: released 0 objects

打开浏览器,输入http://2.2.2.2,会看到Access Denied的错误提示,说明squid已经正常跑起来了。

因为指定了-d参数,所以现在squid会把所有的调试信息都输出终端。 按下键盘的ctrl+c,然后输入killall squid杀死squid,可以终止squid的进程。


  • 上一条:
    用DNSPod和Squid打造自己的CDN (八) 测试并运行SQUID
    下一条:
    用DNSPod和Squid打造自己的CDN (六) 编译并安装Squid
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客