参考xiaosuo的博客,网址:http://blog.chinaunix.net/uid-317451-id-92316.html
零度科技,网址:http://www.ldisp.com/a/primary/2013/1727.shtml
启光博客:http://www.iewb.net/qg/4267.html
参考:ITeye,网址:http://www.iteye.com/problems/38663
参考博客:xuplus,网址:http://blog.csdn.net/xuplus/article/details/51613883
参考Darren Fang的生活点滴,网址:http://www.darrenfang.com/2015/05/install-squid-on-ubuntu-and-enable-ssl/
参考:https://blog.csdn.net/unixtech/article/details/53185297
squid反代:

1、概念
Squid cache(简称为Squid)是代理服务器和Web缓存服务器。从作为网页服务器的前置cache服务器缓存相关请求来提高Web服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。
除了HTTP外,对于FTP与HTTPS的支援也相当好,在3.0 测试版中也支援了IPv6。
squid可以做代理也可以做缓存;
squid缓存不仅可以节省宝贵的带宽资源,也可以大大降低服务器的I/O
squid不仅可以做正向代理,又可以做反向代理。
正向代理,squid后面是客户端,客户端上网要通过Squid去上;反向代理,squid后面是服务器,服务器返回给用户数据需要走squid。
正向代理用在企业的办公环境中,员工上网需要通过squid代理来上网,这样可以节省网络带宽资源。而反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,它用于网站架构中。
2、搭建squid正向代理
官方网站为 http://www.squid-cache.org/
安装命令:yum install -y squid
squid -v 查看版本以及编译参数(Squid Cache: Version 3.1.10)
> /etc/squid/squid.conf 清空配置文件;
vim /etc/squid/squid.conf
http_port 3128
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
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
acl SSL_ports port 443
acl Safe_ports port 80 8080
acl Safe_ports port 21
acl Safe_ports port 443
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localnet
http_access allow localhost
http_access allow all
cache_dir aufs /data/cache 1024 16 256
cache_mem 128 MB
hierarchy_stoplist cgi-bin ?
coredump_dir /var/spool/squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern \.(jpg|png|gif|mp3|xml) 1440 50% 2880 ignore-reload
refresh_pattern . 0 20% 4320
############################## 到此结束
配置解释:
acl Safe_ports port 80 8080 # http的端口
acl Safe_ports port 21 # ftp的端口
acl Safe_ports port 443 # https的端口
cache_dir aufs /data/cache 1024 16 256 #缓存空间1024M大小 16个一级目录,256个子目录
cache_mem 128 MB #缓存可以使用的内存大小;放在内存中访问数据速度快
mkdir /data/cache #创建缓存目录
chown -R squid:squid /data/cache #更改缓存目录权限
squid -z #初始化缓存目录,squid新版本3.1可以省略
/etc/init.d/squid start #启动squid服务
squid -k check #可以检测配置文件是否有错;可以简写为-kche
squid -k rec #可以重新加载配置,reconfig的简写;
service squid restart #重启squid服务;重启经常性的很慢,可以先killall squid,在启动服务;
设置squid代理服务器只代理某几个域名
设置域名白名单,允许baidu sohu可以访问,其他都拒绝;
vim /etc/squid/squid.conf 下面的内容加入到squid配置文件acl下面;
acl http proto HTTP
acl good_domain dstdomain .baidu.com .sohu.com
http_access allow http good_domain
http_access deny http !good_domain
使用curl测试白名单,baidu、sohu返回状态码为200 OK,qq不在白名单则返回403;
一、ACL
1 acl:此为定义条件或者请求
(1)定义IP
acl 名字 类型 值1 值2
acl Http_ports port 80 8080 #
其中,类型可以是src、dst、srcdomain、dstdomain、url_regex、urlpath_regex、time、port、proto、method中的一任意一种,这也是今天文章的重点。
src:指明源地址;
dst:即客户请求的服务器的IP;
acl allowwebsite dst www.baidu.com
myip:指Squid的IP地址,用的比较少,只有当squid服务器多个IP的时候才需要
acl hostip myip 192.168.6.5
(2)定义域名
域名的定义主要使用的对象是srcdomain,dstdomain
srcdomain:客户所属的域,Squid将根据客户IP反向查询DNS
acl allowdomain srcdomain iewb.net //允许iewb.net域访问
dstdomain:服务器所属域,由客户请求的URL决定
acl allowdomain dstdomain iewb.net
http_access allow !allowdomain //拒绝用户访问iewb.net域,squid原则是使用allow语句,然后加deny all。
http_access deny all
http_access定义一条规则,满足时执行
acl localhost src 127.0.0.1/32
acl all src 0.0.0.0./0.0.0.0
http_access allow localhost
http_access deny all
配置允许localhost访问,不允许其他访问。
acl normal src 10.20.12.0/24
acl admin src 10.20.12.66/32
acl manager proto cache_object
acl all src 0.0.0.0/0.0.0.0
http_access allow manager admin
http_access allow normal
http_access deny all
这个配置添加了10.20.12.66用户对cache的管理权限。
acl localhost src 127.0.0.1/32
acl all src 0.0.0.0./0.0.0.0
acl admin src 10.20.12.66/32
acl user proxy_auth REQUIRED
acl normal src 10.20.12.0/24
http_access allow localhost
http_access allow user
http_access allow manager admin
http_access allow normal
http_access deny all
因为admin, normal都从user那行开始继承了需要认证的条件,所以两个都需要认证。
http_access deny !SafePort !localhost
上式表明只有当非本地主机访问不安全端口时才拒绝访问。
二、cache_peer
cache_peer 192.168.1.50 parent 81 0 no-query originserver weight=1 name=a
cache_peer 192.168.1.50 parent 82 0 no-query originserver weight=1 name=b
cache_peer_domain b www.serverB.com
cache_peer_access a allow all
cache_peer_access b allow all
#设置访问权限,允许所有外部客户端访问 a b c
#如果是cache_peer_access a allow allowuser
#表示只允许allowuser访问 www.serverA.com
(1)命令cache_peer的定义格式是:
cache_peer hostname type 3128 3130
hostname 是用来指定获取缓冲的PROXY主机的名字
type 是PROXY主机的类型,有以上PARENT SIBLING两种
3128:HTTP_PORT
3130:ICP_PORT
(2)附加配置选项(注意应为小写,配置时)
NO-QUERY:不做查询操作,直接获取数据
NO-NETDB-EXCHANGE:代理服务器之间不交互信息
NO-DIGEST:代理服务器之间不做摘要表查询,直接用ICP协议沟通(同级代理)
PROXY-ONLY:直接获取对方缓冲,转交给代理服务器的客户端,而不存入本地
ROUND-ROBIN:如果设置数部上层服务器,那么轮流查询父级服务器
LOGIN=USER:PASSWD:要求对方做帐号及密码验证,例如
三、port端口
仅仅允许比较安全的端口
这是个较明智的配置。它允许用户连接到任何非特权端口(1025-65535),但仅仅指定的特权端口可以被连接。假如你的用户试图访问某个URL如下:http://www.lrrr.org:123/,
四、port
http_port中squid将监听客户端的http请求
https_port中squid将监听客户端的https请求
对于:http_port 80 accel vhost vport
1 如果你不加http_port 80 accel vhost vport 这个配置的话,squid默认就作为一个缓存服务器(cache server, 参考第一个链接),这个时候如果客户端有请求发到了squid,squid起到的是路由功能,把请求转发出去,被真正的web server接收,web server返回响应,当squid接收到响应后,根据响应头决定是否缓存,此时的squid,只是一个cache server。
2 如果你加http_port 80 accel vhost vport 这个配置的话,squid就从一个缓存变成了一个web server(参考第一个链接), 这个时候squid在80端口监听请求,同时和web server的请求端口(vhost vport)绑定,这个时候请求到了squid,squid是不用转发请求的,而是直接要么从缓存中拿数据要么向绑定的端口直接请求数据。另外绑定端口还有一个好处,可以充分利用http 响应头中的到期时间头和etag头。
五:初始化配置
/usr/local/squid/sbin/squid -z //初始化缓存空间
#/usr/local/squid/sbin/squid -k parse //对你的squid.conf 排错,即验证 squid.conf 的 语法和配置。
其他说明:
- http_port 80 accel vhost vport allow-direct -----指定squid监听HTTP请求的端口,一般都设置成80端口,
- 这样使用户感觉不到反向代理的存在,就像访问真正的WEB服务器一样。设定squid为accel加速模式,vhost必须要加.
- 否则将无法将主机头转发至后端服务器,访问时就会出现无法找到主机头的错误此处就为加入到CDN服务的网站起加速作用
六 生成证书实例

方法一、
key为私用密钥openssl格式,csr是证书请求文件,用于证书申请,crt是ca认证后的证书文。
注意:如果生成过程中,需要填写证书的资料,起对应的Common Name(e.g. server FQDN or YOUR name)[]: 申请的域名 ************注意一定为域名
(1)key生成
openssl genrsa -des3 -out server.key 2048
采用下面方式去掉密码
openssl rsa -in server.key -out server.key
(2)生成ca的crt
openssl req -new -x509 -key server.key -out ca.crt -days 3650
(3)生成csr
openssl req -new -key server.key -out server.csr
(4)生成crt
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey server.key -CAcreateserial -out server.crt
最后生成了私用密钥:server.key和自己认证的SSL证书:server.crt
证书合并:
cat server.key server.crt > server.pem
方法二、
或者直接生成key.pem和cert.pem的证书,均行。
七、squid.conf配置
acl remote_con src ip #只允许这个ip的客户端访问
acl SSL_ports port 443
acl Safe_ports port 80 # http
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
http_access deny !Safe_ports
http_access allow remote_con
http_access deny all
http_port 8087 vhost #监听squid的8087端口,即访问http://www.baidu.com:8087即可
#add ----start --访问https://www.baidu.com:443即可,或者https://www.baidu.com
https_port 443 accel cert=/etc/squid/ca/server.pem key=/etc/squid/ca/server.key defaultsite=域名(www.baidu.com) vhost
https_port 443 accel cert=/etc/squid/ca/server.crt key=/etc/squid/ca/server.key defaultsite=域名(www.baidu.com) vhost
#https_port 443 accel cert=/etc/squid/caTest/cert.pem key=/etc/squid/caTest/key.pem defaultsite=域名(www.baidu.com) vhost
#add-----end
cache_peer svn服务器的IP parent 8087 0 no-query login=PASS originserver name=tools1server
cache_peer svn服务器的IP parent 18080 0 no-query login=PASS originserver name=tools2server
acl tools_site dstdomain www.baidu.com
acl path_svn urlpath_regex ^/svnTest
cache_peer_access tools_svn_server allow tools_site path_csvn
coredump_dir /xxx
...........
refresh_pattern . 0 20% 4320
八、squid的apt-get安装
(1)安装openssl
apt-get install openssl
apt-get install libssl-dev
apt-get install ssl-cert
(2)安装squid,默认没有启用ssl
apt-get source squid
apt-get build-dep squid
apt-get install devscripts build-essential fakeroot
cd squid3-3.3.8
vim debian/rules
# 在 DEB_CONFIGURE_EXTRA_FLAGS 配置中添加
# 注意 with-open-ssl 的值为实际 openssl.cnf 所在路径
--enable-ssl \
--with-open-ssl=/usr/lib/ssl/openssl.cnf \
注意:tar.gz安装为
--with-openssl=/usr/lib/ssl
# 编译 squid./configuredebuild -us -uc -b# 安装cd ..apt-get install squid-langpackdpkg -i squid3-common_3.3.8-1ubuntu6.2_all.debdpkg -i squid3_3.3.8-1ubuntu6.2_amd64.deb
(3)禁用squid升级
sudo apt-mark hold squid3
注意:如果是采用解压缩包的形式安装,则系统默认用户为nobody,如果修改了目录权限为squid:nobody
sudo chown -Rf squid:nobody /usr/local/squid
建立cache需要对目录赋予nobody用户权限
有时需要sudo /usr/local/squid/sbin/squid -z 初始化
squid安装方式二
下载tar.gz文件,
之后解压缩
tar zxvf xx.tar.gz
之后 ./configure --enable-ssl --with-openssl=/etc/ssl
************************* path does not point to a directory--
则是因为注意/usr/local/ssl /openssl.cnf 应该为/usr/local/ssl,是目录,不是文档
之后make
make install
最后需要添加用户组
# mkdir -p /data/squid/{cache,coredump,logs}
# /usr/sbin/groupadd squid
# /usr/sbin/useradd squid -g squid -s /sbin/nologin
# chmod -R 777 /data/squid/{cache,coredump,logs}
# chown -R squid:squid /data/squid/{cache,coredump,logs}
chmod -R 777 /usr/local/squid/var/cache
chmod -R 777 /usr/local/squid/var/logs
chmod -R 777 /usr/local/squid/var/run
先初始化
./squid -z
启动./squid start
关闭 ./squid -k shutdown
配置,参考:
代理设置:
浏览器中设置代理IP(squid主机的IP),端口(http_port参数)
九 常用命令
1、初始化squid 缓存目录
/usr/sbin/squid -zX
/usr/sbin/squid -NCd1
2、启动代理服务
/etc/init.d/squid start
3、停止代理服务
/etc/init.d/squid stop
/usr/sbin/squid -k shutdown
4、重新启动代理服务
/etc/init.d/squid restart
5、重新载入配置文件
/etc/init.d/squid reload
/usr/sbin/squid -k reconfig
6、自动启动代理服务
执行”ntsysv”命令启动服务配置程序
7、通过crontab每小时截断/轮循日志 59 * * * * /usr/sbin/squid -k rotate
service squid start/stop/restart
336

被折叠的 条评论
为什么被折叠?



