squid 2.7 配置与安装

时间:2023-12-15 12:20:50

1、准备安装包

2、准备编译环境

yum -y install gcc

3、编译安装squid

./configure \
--prefix=/data/squid \
--disable-internal-dns \
--enable-forward-log   \
--enable-follow-x-forwarded-for \
--enable-snmp \
--enable-linux-netfilter  \
--enable-http-violations \
--enable-delay-pools \
--enable-storeio=diskd,aufs,ufs,coss \
--with-coss-membuf-size=8388608 \
--with-large-files \
--enable-large-cache-files \
--with-maxfd=8192 \
--enable-removal-policies=lru,heap \
--enable-useragent-log \
--enable-referer-log  \
--enable-err-languages=Simplify_Chinese \
--enable-default-err-language=Simplify_Chinese

make && make install

4、创建缓存目录

mkdir /data/squid/var/cache
chown -R nobody.nobody /data/squid/var/cache
chown -R nobody.nobody /data/squid/var/logs

5、配置squid.conf

如果WEB服务器和反向代理服务器是两台单独的机器(一般的反向代理应该有两块网卡分别连接了内外部网络)。那么,应该修改下面的内容来设置反向代理服务。

http_port 80 # squid监听的端口
httpd_accel_host 172.16.250.250 # 内部WEB服务器的IP地址
httpd_accel_port 80 # WEB服务器的IP地址
httpd_accel_single_host on # 转发为缓冲的请求到一台单独的机器
httpd_accel_with_proxy on #
httpd_accel_uses_host_header off

如果WEB服务器和反向代理服务器是同一台机器。
那么,应该设置WEB服务器的监听端口为非80端口(比如:81端口)。要修改的内容如下:
http_port 80 # squid监听的端口
httpd_accel_host localhost # 内部WEB服务器的IP地址
httpd_accel_port 81 # WEB服务器的IP地址
httpd_accel_single_host on # 转发为缓冲的请求到一台单独的机器
httpd_accel_with_proxy on #
httpd_accel_uses_host_header off

下面解释一下配置指令。

http_port 80

选项 http_port 指定squid监听HTTP请求的端口,一般都设置成80端口,这样使用户感觉不到反向代理的存在,就像访问真正的WEB服务器一样。

httpd_accel_host 172.16.250.250 和 httpd_accel_port 80

选项httpd_accel_host 和 httpd_accel_port 指定WEB服务器的IP地址和端口号,可以根据自己的WEB服务器的实际情况而定。

httpd_accel_single_host on

选项httpd_accel_single_host 为on 时,squid被设置成仅对单一的web服务器作反向代理。不考虑HTTP头信息,Squid转发所有的未被缓冲的页面请求到这个web服务器。如果squid需要做多个web服务器反向代理,必须将此选项设置为off,并且使用转向器或者DNS去映射请求到合适的后台WEB服务器。

httpd_accel_with_proxy on

如果希望squid既作反向代理服务器又作本地机器的上网代理,需要将httpd_accel_with_proxy 改为 on,默认情况下是off

httpd_accel_uses_host_header off

在HTTP协议1.1中,HTTP请求包括一个主机头信息,指定URL的主机名或者主机的IP地址。这个选项可以用来完成多个后台WEB服务器的反向代理功能。

6、启动squid

初始化你在 squid.conf 里配置的 cache 目录
/usr/local/squid/sbin/squid -zX
验证 squid.conf 的 语法和配置
/usr/local/squid/sbin/squid -k parse
前台启动squid,并输出启动过程
/usr/local/squid/sbin/squid -N -d1
出现2010/06/21 15:36:44| Ready to serve requests. 说明启动成功
然后 ctrl + c,停止squid,并以后台运行的方式启动它
启动squid在后台运行
/usr/local/squid/sbin/squid -s
停止 squid
/usr/local/squid/sbin/squid -k shutdown
要执行两次才能正常关闭squid
重引导修改过的 squid.conf
/usr/local/squid/sbin/squid -k reconfigure
可能通过squidclient这个工具来查看squid的运行情况,缓存命中率等
/usr/local/squid/bin/squidclient -h 10.80.11.203 -p 80 mgr:info
可查看各选项的使用说明
/usr/local/squid/bin/squidclient -h 10.80.11.203 -p 80 mgr:
内存使用情况可通过如下选项查看
/usr/local/squid/bin/squidclient -h 10.80.11.203 -p 80 mgr:mem
查看你的日志文档
more /usr/local/squid/var/logs/access.log | grep TCP_MEM_HIT
该指令可以看到在squid运行过程中,有那些文件被squid缓存到内存中,并返回给访问用户
more /usr/local/squid/var/logs/access.log | grep TCP_HIT
该指令可以看到在squid运行过程中,有那些文件被squid缓存到cache目录中,并返回给访问用户
more /usr/local/squid/var/logs/access.log | grep TCP_MISS
该指令可以看到在squid运行过程中,有那些文件没有被squid缓存,而是现重原始服务器获取并返回给访问用户