• squid


    squid简单使用

    简介

    squid是一款高性能的代理缓存服务器,支持FTP、gopher、HTTP协议。
    squid一个缓存Internet数据的软件,其接收用户的下载申请(作为代理服务器),并自动处理所下载的数据,并返回给客户。

    个人最喜欢的还是squid的正向代理,上网的行为控制很强大。

    安装

    ~]# yum install suqid -y           #这里使用yum安装
    ~]# systemctl enable squid.service #设置开机启动
    

    访问控制

    使用访问控制特性,可以控制在访问时根据特定的时间间隔进行缓存、访问特定站点或一组站点等等。squid访问控制有两个要素:ACL元素访问列表。访问列表可以允许或拒绝某些用户对此服务的访问。

    ACL格式

    acl acl_element_name type_of_acl_element values_to_acl
    注:

    • acl_element_name可以是任一个在ACL中定义的名称
    • 任何两个ACL元素不能重复
    • 每个ACL由列表值组成。当进行匹配检测的时候,多个值由逻辑或运算连接;换言之,即任一ACL元素的值被匹配,则这个ACL元素即被匹配
    • 并不是所有ACL元素都能使用访问列表中的全部类型
    • 不同的ACL元素写在不同行中,squid将把他们组合在一个列表中

    ACL元素类型

    • src: 源地址(即客户IP地址)
    • dst: 目标地址(即服务器IP地址)
    • srcdomain: 源名称(即客户机名称)
    • dstdomain: 目标名称(即服务器名称)
    • time: 一天中的时刻和一周内的一天
    • url_regex: URL规则表达式匹配
    • urlpath_regex: URL-path规则表达式匹配,略去协议和主机名
    • proxy_auth: 通过外部程序进行用户验证
    • maxconn: 单一IP的最在连接数

    访问条目

    常用如下:

    • http_access: 允许HTTP访问
    • no_cache: 定义对缓存请求的响应

    访问列表的规则由一些类似allowdeny的关键字构成,用以允许或拒绝向特定或一组ACL元素提供服务。

    1. 一个访问列表可以由多条规则组成
    2. 如果没有任何规则与访问请求匹配,默认动作将与列表中最后一条规则对应。
    3. 列表中的规则总是遵循由上而下的顺序
    4. 这些规则按照它们的排列顺序进行匹配检测,一旦检测到匹配的规则,匹配就立即结束。
    5. 一个访问条目中所有元素将用逻辑与运算连接
      http_access Action 声明1 AND 声明2 OR
      http_access Action 声明3
      多个http_access 声明间用或运算连接,但每个访问条目的元素间用与运算连接

    squid配置文件解释

    ~]# cp /etc/squid/squid.conf{,_bak}
    ~]# vim /etc/squid/squid.conf
    #
    # Recommended minimum configuration:
    #
    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        #允许目的地址为本机IP
    acl allow_domain dstdomain "/etc/squid/allow_domain"   #自定义acl(允许目标域名存放在/etc/squid/allow_domain文件中):
    
    # Example rule allowing access from your local networks.
    # Adapt to list your (internal) IP networks from where browsing
    # should be allowed
    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    #允许C类网络访问
    acl localnet src fc00::/7       # RFC 4193 local private network range
    acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
    
    acl SSL_ports port 443      #允许ssl端口443
    acl Safe_ports port 80		# http   允许安全更新的端口为80
    acl Safe_ports port 21		# ftp
    acl Safe_ports port 443		# https
    acl Safe_ports port 70		# gopher
    acl Safe_ports port 210		# wais
    acl Safe_ports port 1025-65535	# unregistered ports
    acl Safe_ports port 280		# http-mgmt
    acl Safe_ports port 488		# gss-http
    acl Safe_ports port 591		# filemaker
    acl Safe_ports port 777		# multiling http
    acl CONNECT method CONNECT 	#请求方法以CONNECT
    
    #
    # Recommended minimum Access Permission configuration:
    #
    # Only allow cachemgr access from localhost
    http_access allow allow_domain   #只允许allow_domain名称中自定义的域名可访问,在拒绝所有访问
    http_access deny all             #拒绝所有,当上面没有匹配上,即拒绝所有访问
    
    # Deny requests to certain unsafe ports
    http_access deny !Safe_ports     #拒绝不安全的端口
    
    # Deny CONNECT to other than secure SSL ports
    http_access deny CONNECT !SSL_ports #拒绝连接非443端口
    
    # We strongly recommend the following be uncommented to protect innocent
    # web applications running on the proxy server who think the only
    # one who can access services on "localhost" is a local user
    #http_access deny to_localhost
    
    #
    # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
    #
    
    # Example rule allowing access from your local networks.
    # Adapt localnet in the ACL section to list your (internal) IP networks
    # from where browsing should be allowed
    http_access allow localnet
    http_access allow localhost
    
    # And finally deny all other access to this proxy
    http_access deny all
    
    # Squid normally listens to port 3128
    http_port 172.23.100.163:3128     #监听在某个IP上的某个端口
    
    # Uncomment and adjust the following to add a disk cache directory.
    cache_dir ufs /var/spool/squid 100 16 256  #高速缓存目录 ufs 类型 缓存最在允许100m,16个一级目录,256个二级目录
    
    # Leave coredumps in the first cache dir
    coredump_dir /var/spool/squid  #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
    
    
    
    
    
    
    
    
    ~]# cat /etc/squid/allow_domain
    .npmjs.org
    .taobao.org
    只允许访问这两具一级域名及所有二级以上域名可访问
    

    squid命令使用

    ~]# squid -k parse //检查配置文件
    ~]# squid –k rec //重新加载配置文件
    ~]# squid -zX //初始化缓存目录
    ~]# vim /etc/sysctl.conf
    # Controls IP packet forwarding
    net.ipv4.ip_forward = 1    #打开本地转发
    ~]# sysctl -p    //重新加载配置文件,使其生效
    ~]# systemct start squid.service
    ~]# ss -tnl
    State      Recv-Q Send-Q                                               Local Address:Port                                                 Peer Address:Port 
    LISTEN     0      128                                                             :::22                                                             :::*     
    LISTEN     0      128                                                              *:22                                                              *:*     
    LISTEN     0      128                                                 172.23.100.163:3128                                                            *:*     
    LISTEN     0      100                                                            ::1:25                                                             :::*     
    LISTEN     0      100                                                      127.0.0.1:25                                                              *:*
    

    总结

    squid做正向代理实在是非常好用。推荐使用。

    此笔记中只做了域名访问控制,没有使用其它例如,用户认证,时间控制、限制URL、禁止IP等功能。如后期用上,则在做补充。
    如有错误,欢迎指出。

  • 相关阅读:
    1、如何使用Azure Rest API创建虚拟机
    Ansible---2的Roles使用
    linux下的shell脚本
    生成器 yield和协程
    xshell
    markdown的使用
    加密
    Hbuilder打包app
    尾递归
    jupyter
  • 原文地址:https://www.cnblogs.com/dance-walter/p/9432260.html
Copyright © 2020-2023  润新知