您的位置:首页 > 运维架构 > 反向代理

Squid概述及相关配置说明

2016-10-15 22:48 330 查看
一、简介
代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息。
Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据。当一个用户想要下载一个主页时,可以向Squid 发出一个申请,要Squid 代替其进行下载,然后Squid 连接所申请网站并请求该主页,接着把该主页传给用户同时保留一个备份,当别的用户申请同样的页面时,Squid 把保存的备份立即传给用户,使用户觉得速度相当快。Squid 可以代理HTTP、FTP、GOPHER、SSL和WAIS等协议并且Squid 可以自动地进行处理,可以根据自己的需要设置Squid,使之过滤掉不想要的东西。
1.1 工作流程
当代理服务器中有客户端需要的数据时:
a. 客户端向代理服务器发送数据请求;
b. 代理服务器检查自己的数据缓存;
c. 代理服务器在缓存中找到了用户想要的数据,取出数据;
d. 代理服务器将从缓存中取得的数据返回给客户端。
当代理服务器中没有客户端需要的数据时:
1. 客户端向代理服务器发送数据请求;
2. 代理服务器检查自己的数据缓存;
3. 代理服务器在缓存中没有找到用户想要的数据;
4. 代理服务器向Internet 上的远端服务器发送数据请求;
5. 远端服务器响应,返回相应的数据;
6. 代理服务器取得远端服务器的数据,返回给客户端,并保留一份到自己的数据缓存中。



Squid代理服务器工作在TCP/IP的应用层。



1.2 Squid 分类
按照代理类型的不同,可以将Squid 代理分为正向代理和反向代理,正向代理中,根据实现方式的不同,又可以分为普通代理和透明代理。
普通代理:需要客户机在浏览器中指定代理服务器的地址、端口;

透明代理:适用于企业的网关主机(共享接入Internet)中,客户机不需要指定代理服务器地址、端口等信息,代理服务器需要设置防火墙策略将客户机的Web访问数据转交给代理服务程序处理;

反向代理:是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

官方地址:http://www.squid-cache.org/
参考文档:http://www.squid-cache.org/Doc/config/
二、编译安装参数说明
配置参数示例:./configure --prefix=/usr/local/squid \
--enable-async-io=100 \
--with-pthreads \
--enable-storeio="aufs,diskd,ufs" \
--enable-removal-policies="heap,lru" \
--enable-icmp \
--enable-delay-pools \
--enable-useragent-log \
--enable-referer-log \
--enable-kill-parent-hack \
--enable-cachemgr-hostname=localhost \
--enable-arp-acl \
--enable-default-err-language=English \
--enable-err-languages="Simplify_Chinese English" \
--disable-poll \
--disable-wccp \
--disable-wccpv2 \
--disable-ident-lookups \
--disable-internal-dns \
--enable-basic-auth-helpers="NCSA" \
--enable-stacktrace \
--with-large-files \
--disable-mempools \
--with-filedescriptors=64000 \
--enable-ssl \
--enable-x-accelerator-vary \
--disable-snmp \
--with-aio \
--enable-linux-netfilter \
--enable-linux-tproxy

Squid编译安装参数说明:
--prefix=/usr/local/squid :指定软件的安装路径

--prefix=PREFIX 默认的安装位置是/usr/local/squid

--enable-icmp 启用ICMP ping。squid能利用ICMP消息来确定回环时间尺寸,非常像ping程序,可以通过使用该选项来激活这些功能。

--disable-htcp 禁用HTCP协议支持。HTCP是超文本缓存协议--类似于ICP的内部缓存协议

--localstatedir 数据及日志的目录(未来会很大)

–enable-gnuregex :支持GNU正则表达式。

–disable-carp: Cache数组路由协议(CARP)用来转发丢失的cache到父cache的数组或cluste

–enable-async-io=100等同于同时打开./configure如下三个选项

--with-aufs-threads=N_THREADS

--with-pthreads

--enable-storeio=ufs,aufs

这个主要是设置async模式来运行squid,我的理解是设置用线程来运行squid,如果服务器配置很不错,有1G以上内存,cpu使用SMP的方式的话可以考虑设成160或者更高。如果服务器比较糟糕就根据实际情况设了。另外此项还另cache文件支持aufs

--disable-wccp用于阻止或分发HTTP请求到一个或多个caches

–enable-icmp :加入icmp支持

–enable-kill-parent-hack :关掉suqid的时候,要不要连同父进程一起关掉

--enable-cachemgr-hostname=localhost:指定cachemgr-hostname值为localhost

-- enable-snmp :此选项可以让MRTG使用SNMP协议对服务器的流量状态进行监测,因此必须选择此项,使Squid支持SNMP接口。

–disable-ident-lookups :防止系统使用RFC931规定的身份识别方法。

--enable-delay-pools 开启squid延时池功能

–enable-cahce-digests :加快请求时,检索缓存内容的速度。

–enable-err-language=”Simplify_Chinese” 和

–enable-default-err-languages=”Simplify_Chinese” :指定出错是显示的错误页面为简体中文

--with-maxfd=65535指定最大文件描述

–enable-poll :指定使用Poll()函数,提升性能就是啦。

--disable-ident-lookups允许服务器利用客户端的特殊TCP连接来发现用户名

–enable-linux-netfilter :可以支持透明代理

--enable-large-cache-files开启大文件支持,支持2GB以上的文件

--disable-internal-dns使用自己的内部DNS查询

–enable-underscore :允许解析的URL中出现下划线,因为默认squid会认为带下划线的URL地址是非法的,并拒绝访问该地址

–enable-arp-acl :可以在规则设置中直接通过客户端的MAC地址进行管理,防止客户使用IP欺骗。

三、squid配置文件讲解
http_port 3128       //设置监听的IP与端口号

cache_mem 64 MB      //额外提供给squid使用的内存,squid的内存总占用为 X * 10+15+“cache_mem”,其中X为squid的cache占用的容量(以GB为单位),
                //比如下面的cache大小是100M,即0.1GB,则内存总占用为0.1*10+15+64=80M,推荐大小为物理内存的1/3-1/2或更多。
maximum_object_size 4 MB   //设置squid磁盘缓存最大文件,超过4M的文件不保存到硬盘

minimum_object_size 0 KB   //设置squid磁盘缓存最小文件

maximum_object_size_in_memory 4096 KB   //设置squid内存缓存最大文件,超过4M的文件不保存到内存

cache_dir ufs /var/spool/squid 100 16 256   //定义squid的cache存放路径 、cache目录容量(单位M)、一级缓存目录数量、二级缓存目录数量

logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh //log文件日志格式

access_log /var/log/squid/access.log combined  //log文件存放路径和日志格式

cache_log /var/log/squid/cache.log   //设置缓存日志

logfile_rotate 60   //log轮循 60天

cache_swap_high 95  //cache目录使用量大于95%时,开始清理旧的cache

cache_swap_low 90   //cache目录清理到90%时停止。

acl localnet src 192.168.1.0/24  //定义本地网段

http_access allow localnet  //允许本地网段使用

http_access deny all  //拒绝所有

visible_hostname squid.david.dev  //主机名

cache_mgr mchina_tang@qq.com  //管理员邮箱
四、配置文件示例
squid常用命令:
/usr/local/squid/sbin/squid -z 初始化缓存空间
/usr/local/squid/sbin/squid 启动
/usr/local/squid/sbin/squid -k shutdown 停止
/usr/local/squid/sbin/squid -k reconfigure 重新载入配置文件
/usr/local/squid/sbin/squid -k rotate 轮循日志

#acl all src 0.0.0.0/0.0.0.0 and http_access allow all选项定义了一个访问控制列表。详细情况参见和Squid软件
#携带的文档。这里的访问控制列表允许所有对代理服务的访问,因为这里该代理是加速web服务器。
acl all src 0.0.0.0/0.0.0.0 #允许所有IP访问
acl manager proto http #manager url协议为http
acl localhost src 127.0.0.1/255.255.255.255 #允午本机IP
acl to_localhost dst 127.0.0.1 #允午目的地址为本机IP
acl Safe_ports port 80 # 允许安全更新的端口为80
acl CONNECT method CONNECT #请求方法以CONNECT
http_access allow all #允许所有人使用该代理.因为这里是代理加速web服务器
http_reply_access allow all #允许所有客户端使用该代理

acl OverConnLimit maxconn 16 #限制每个IP最大允许16个连接,防止攻击
http_access deny OverConnLimit

icp_access deny all #禁止从邻居服务器缓冲内发送和接收ICP请求.
miss_access allow all #允许直接更新请求
ident_lookup_access deny all #禁止lookup检查DNS
http_port 8080 transparent #指定Squid监听浏览器客户请求的端口号。

hierarchy_stoplist cgi-bin ? #用来强制某些特定的对象不被缓存,主要是处于安全的目的。
acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY

cache_mem 1 GB #这是一个优化选项,增加该内存值有利于缓存。应该注意的是:
#一般来说如果系统有内存,设置该值为(n/)3M。现在是3G 所以这里1G
fqdncache_size 1024 #FQDN 高速缓存大小
maximum_object_size_in_memory 2 MB #允许最大的文件载入内存

memory_replacement_policy heap LFUDA #动态使用最小的,移出内存cache
cache_replacement_policy heap LFUDA #动态使用最小的,移出硬盘cache

cache_dir ufs /home/cache 5000 32 512 #高速缓存目录 ufs 类型使用的缓冲值最大允午1000MB空间,
#32个一级目录,512个二级目录

max_open_disk_fds 0 #允许最大打开文件数量,0 无限制
minimum_object_size 1 KB #允午最小文件请求体大小
maximum_object_size 20 MB #允午最大文件请求体大小

cache_swap_low 90 #最小允许使用swap 90%
cache_swap_high 95 #最多允许使用swap 95%

ipcache_size 2048 # IP 地址高速缓存大小 2M
ipcache_low 90 #最小允许ipcache使用swap 90%
ipcache_high 95 #最大允许ipcache使用swap 90%

access_log /var/log/squid/access.log squid #定义日志存放记录
cache_log /var/log/squid/cache.log squid
cache_store_log none #禁止store日志

emulate_httpd_log on #将使Squid仿照Web服务器的格式创建访问记录。如果希望使用
#Web访问记录分析程序,就需要设置这个参数。

refresh_pattern . 0 20% 4320 override-expire override-lastmod reload-into-ims ignore-reload #更新cache规则

acl buggy_server url_regex ^http://.... http:// #只允许http的请求
broken_posts allow buggy_server

acl apache rep_header Server ^Apache #允许apache的编码
broken_vary_encoding allow apache

request_entities off #禁止非http的标分准请求,防止攻击
header_access header allow all #允许所有的http报头
relaxed_header_parser on #不严格分析http报头.
client_lifetime 120 minute #最大客户连接时间 120分钟

cache_mgr sky@test.com #指定当缓冲出现问题时向缓冲管理者发送告警信息的地址信息。

cache_effective_user squid #这里以用户squid的身份Squid服务器
cache_effective_group squid

icp_port 0 #指定Squid从邻居服务器缓冲内发送和接收ICP请求的端口号。
#这里设置为0是因为这里配置Squid为内部Web服务器的加速器,
#所以不需要使用邻居服务器的缓冲。0是禁用

# cache_peer 设置允许更新缓存的主机,因是本机所以127.0.0.1
cache_peer 127.0.0.1 parent 80 0 no-query default multicast-responder no-netdb-exchange
cache_peer_domain 127.0.0.1
hostname_aliases 127.0.0.1

error_directory /usr/share/squid/errors/Simplify_Chinese #定义错误路径

always_direct allow all # cache丢失或不存在是允许所有请求直接转发到原始服务器
ignore_unknown_nameservers on #开反DNS查询,当域名地址不相同时候,禁止访问
coredump_dir /var/log/squid #定义dump的目录

max_filedesc 2048 #最大打开的文件描述

half_closed_clients off #使Squid在当read不再返回数据时立即关闭客户端的连接。
#有时read不再返回数据是由于某些客户关闭TCP的发送数据
#而仍然保持接收数据。而Squid分辨不出TCP半关闭和完全关闭。

buffered_logs on #若打开选项“buffered_logs”可以稍稍提高加速某些对日志文件的写入,该选项主要是实现优化特性。

#防止天涯盗链,转嫁给百度
acl tianya referer_regex -i tianya
http_access deny tianya
deny_info tianya
#阻止baidu蜘蛛
acl baidu req_header User-Agent Baiduspider
http_access deny baidu
#限制同一IP客户端的最大连接数
acl OverConnLimit maxconn 128
http_access deny OverConnLimit

#防止被人利用为HTTP代理,设置允许访问的IP地址
acl myip dst 222.18.63.37
http_access deny !myip

#允许本地管理
acl Manager proto cache_object
acl Localhost src 127.0.0.1 222.18.63.37
http_access allow Manager Localhost
cachemgr_passwd 53034338 all
http_access deny Manager

#仅仅允许80端口的代理
acl all src 0.0.0.0/0.0.0.0
acl Safe_ports port 80 # http
http_access deny !Safe_ports
http_access allow all

#Squid信息设置
visible_hostname happy.swjtu.edu.cn
cache_mgr ooopic2008@qq.com

#基本设置
cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65535 bytes

#2.6的反向代理加速配置
cache_peer 127.0.0.1 parent 80 0 no-query originserver

#错误文档
error_directory /usr/local/squid/share/errors/Simplify_Chinese

#单台使用,不使用该功能
icp_port 0

hierarchy_stoplist cgi-bin ?

acl QUERY urlpath_regex cgi-bin \? .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe
cache deny QUERY

acl apache rep_header Server ^Apache
broken_vary_encoding allow apache

refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern . 0 20% 4320

cache_store_log none
pid_filename /usr/local/squid/var/logs/squid.pid
emulate_httpd_log on
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
cache_log /usr/local/squid/var/logs/cache.log
access_log /usr/local/squid/var/logs/access.log combined
coredump_dir /usr/local/squid/var/cache
cache_dir ufs /usr/local/squid/var/cache 10000 16 256

dns_children 32
hosts_file /etc/hosts

cache_mem 400 MB
cache_swap_low 90
cache_swap_high 95
maximum_object_size 32768 KB
maximum_object_size_in_memory 4096 KB
emulate_httpd_log on

#防止盗链
acl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$
acl mystie1 referer_regex -i happy.swjtu.edu.cn
http_access allow mystie1 picurl
acl nullref referer_regex -i ^$
http_access allow nullref
acl hasref referer_regex -i .+
http_access deny hasref picurl

本文相关内容参考自:http://jafy00.blog.51cto.com/2594646/682590
http://www.cnblogs.com/mchina/p/3812190.html
http://www.cnblogs.com/mchina/p/3812190.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  squid 概念 编译参数