正在浏览标签为 Squid 的文章

今天测试了一下在squid上建一个ssl代理转发.其实也蛮容易的.

  1. https_port 443 cert=/etc/squid/SSL.CRT key=/etc/squid/SSL.KEY defaultsite=ssl.php-oa.com
  2. cache_peer ssl.php-oa.com parent 80 o no-query no-digest originserver name=www
  3. cache_peer_homain www .php-oa.com

其实真的很容易.是吧.有个https_port和cache_peer加上那个ssl的地址基本就好了.记的https_port要加defaultsite.不加会下面错

The following error was encountered:

Invalid Request

Some aspect of the HTTP Request is invalid. Possible problems:

Missing or unknown request method

Missing URL

Missing HTTP Identifier (HTTP/1.0)

Request is too large

Content-Length missing for POST or PUT requests

Illegal character in hostname; underscores are not allowed

可以使用下面命令可以生成测试用的证书:

openssl req -new -keyout key.pem -nodes -x509 -days 365 -out cert.pem

如果要生成一个证书请求用于申请正式证书请用下面两个命令:

1.生成私钥 openssl genrsa -out key.pem 1024
2.生成待签名证书 openssl req -new -out req.csr -key key.pem

然后将req.csr文件中的内容提交给证书颁发机构。

Creating Certificates

$ openssl req -new -x509 -days 365 -out server.crt -keyout server.key

Creating a Certificate Signing Request (CSR)

$ openssl req -new -out server.csr -keyout server.key

Creating a Decrypted certificate

Create the certificate as above, then:

$ mv server.key server.key.encrypted
$ openssl rsa -in server.key.encrypted -out server.key

Viewing Certificates

$ openssl x509 -noout -text -in <name>.crt

Viewing Keys

$ openssl rsa -noout -text -in <name>.key

转载自:扶凯[http://www.php-oa.com]

squid.conf

抢沙发
?View Code SQUID_CONF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#squid.conf
#服务器IP 192.168.108.109
 
#Squid信息设置
visible_hostname squid2.jobkoo.com
cache_mgr root@localhost.localdomain
 
 
#监听服务器的80端口,透明代理,支持域名和IP的虚拟主机
http_port 80 accel vhost vport defaultsite=www.myhome2.com
 
 
#基本ACL设置
acl all src 0.0.0.0/0.0.0.0
acl Localhost src 127.0.0.1
acl myhost src 192.168.108.108 192.168.108.109 192.168.108.161 192.168.108.162
acl localnet  src 127.0.0.1 192.168.108.108 192.168.108.109
 
#仅允许80端口的代理
acl Safe_ports port 80 3130
 
# 定义可以清理缓存的访问权限
acl PURGE method PURGE
 
#允许本地管理
acl Manager proto cache_object # cache_object机制是squid的特性。它用于访问squid的缓存管理接口
 
# 防盗链
acl allow_ref referer_regex -i ^http://.+\.myhome2\.com/.*$ ^http://.+\.jobkoo\.com/.*$
acl deny_ref referer_regex -i .+
http_access allow allow_ref
http_access deny deny_ref
 
#deny_info http://www.baidu.com/img/baidu_logo.gif fdlacl
 
# icp 配置
icp_port 3130
log_icp_queries on
icp_hit_stale on
icp_query_timeout 200
# 只允许定义的localhost 主机执行icp查询
icp_access allow localnet
icp_access deny all
 
# 访问控制
http_access allow myhost
http_access allow Manager Localhost
http_access deny Manager
http_access deny !Safe_ports
http_access allow all
http_access allow PURGE localhost
http_access deny PURGE
 
 
 
#基本设置
cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65535 bytes
 
 
# 配置 squid2 为其邻居,当 squid1 在其缓存中没有找到请求的资源
# 时,通过 ICP 查询去其邻居中取得缓存
cache_peer		squid1.jobkoo.com sibling 80  3130 proxy-only
cache_peer_access	squid1.jobkoo.com allow  all
 
# squid1 的2个父节点,originserver 参数指明是源服务器,round-robin
# 参数指明 squid 通过轮询方式将请求分发到其中一台父节点;squid 同时会对这些
# 父节点的健康状态进行检查,如果父节点 down 了,那么 squid 会从剩余的 origin 
# 服务器中抓取数据
cache_peer 192.168.108.161 parent 80 0 no-query originserver round-robin name=webServer1
cache_peer 192.168.108.162 parent 80 0 no-query originserver round-robin name=webServer2
 
# www.myhome.com 域的请求通过 RR 轮询方式转发到2个父节点中的一个
cache_peer_domain webServer1 webServer2 www.myhome2.com
 
# hierarchy_stoplist cgi-bin ?
 
# squid 用于缓存的内存量
cache_mem 256 MB
 
# Cooper:内存中缓存的最大对象 512K 
maximum_object_size_in_memory 512 KB
 
# 缓存目录
cache_dir ufs /var/spool/squid 1000 16 256
 
# 能缓存的最大对象为 10M
maximum_object_size 10240 KB
 
# 访问日志
logformat squid  %ts.%03tu %6tr %>a %Ss/%03Hs %<st %rm %ru %un %Sh/%<A %mt
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h"
access_log /var/log/squid/access.log squid
access_log /var/log/squid/access_apache.log combined
 
# Cache 日志
cache_log /var/log/squid/cache.log
 
refresh_pattern -i .html 60 90% 10080 ignore-reload
refresh_pattern -i .png 60 90% 10080 reload-into-ims
refresh_pattern -i .jpg 1440 90% 129600 ignore-reload 
refresh_pattern -i .gif 1440 90% 129600 ignore-reload 
refresh_pattern -i .js 1440 90% 10080 ignore-reload 
refresh_pattern -i .css 1440 90% 10080 reload-into-ims
refresh_pattern -i .swf 1440 90% 129600 reload-into-ims 
refresh_pattern -i .bmp 1440 90% 129600 reload-into-ims 
 
# 定义不缓存的对象
acl QUERY urlpath_regex cgi-bin \? \.php
cache deny QUERY
 
coredump_dir /var/spool/squid

转自:www.hiadmin.com
原有的配置文件如下

?View Code SQUID_CONF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cache_peer 10.11.12.51 parent 80 0 no-query originserver round-robin name=web1
cache_peer 10.11.12.52 parent 80 0 no-query originserver round-robin name=web2
cache_peer 10.11.12.53 parent 80 0 no-query originserver round-robin name=web3
cache_peer 10.11.12.54 parent 80 0 no-query originserver round-robin name=web4
cache_peer 10.11.12.160 parent 80 0 no-query originserver name=content
cache_peer 10.11.12.150 parent 80 0 no-query originserver name=bbs
cache_peer 172.16.10.140 parent 80 0 no-query originserver round-robin name=game1
cache_peer 172.16.10.141 parent 80 0 no-query originserver round-robin name=game2
 
cache_peer_domain contentchina content.web.com
cache_peer_domain bbs  bbs.web.com
cache_peer_domain game1 game2 game.web.com
cache_peer_domain web1 web2 web3 web4  .web.com
cache_peer_domain web1 web2 web3 web4   web.com

设定不同的二级域名分发到不同的服务器上.
www.web.com能够正确访问.
查看后台链接.每次访问时squid也会正常去连parent服务器,每次都轮询访问
测试game.web.com
返回有很多内容都是404
但是单独访问140以及141都是没有问题
然后发现很多链接分发到了其他web服务器上
看了说明.里面提到round-robin参数会设置一组随机的访问
感觉是写了round-robin的都是一个组
所以将game的去掉round-robin参数

?View Code SQUID_CONF
1
2
cache_peer 172.16.10.140 parent 80 0 no-query originserver name=game1
cache_peer 172.16.10.141 parent 80 0 no-query originserver name=game2

访问后仍旧发现还是有分发错误的情况
再次查看squid.conf.default
里面的cache_peer_domain的语法如下

?View Code SQUID_CONF
1
2
#       cache_peer_domain cache-host domain [domain ...]
#       cache_peer_domain cache-host !domain

感觉是否是因为cache-host这里只能写一台服务器而非一组的关系
随将配置文件修改

?View Code SQUID_CONF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cache_peer 10.11.12.51 parent 80 0 no-query originserver round-robin name=web1
cache_peer 10.11.12.52 parent 80 0 no-query originserver round-robin name=web2
cache_peer 10.11.12.53 parent 80 0 no-query originserver round-robin name=web3
cache_peer 10.11.12.54 parent 80 0 no-query originserver round-robin name=web4
cache_peer 10.11.12.160 parent 80 0 no-query originserver name=content
cache_peer 10.11.12.150 parent 80 0 no-query originserver name=bbs
cache_peer 172.16.10.140 parent 80 0 no-query originserver round-robin name=game1
cache_peer 172.16.10.141 parent 80 0 no-query originserver round-robin name=game2
 
cache_peer_domain contentchina content.web.com
cache_peer_domain bbs  bbs.web.com
cache_peer_domain game1 game.web.com
cache_peer_domain game2 game.web.com
cache_peer_domain web1 .web.com
cache_peer_domain web2 .web.com
cache_peer_domain web3 .web.com
cache_peer_domain web4 .web.com
cache_peer_domain web1 web.com
cache_peer_domain web2 web.com
cache_peer_domain web3 web.com
cache_peer_domain web4 web.com

修改后访问game.web.com
问题解决.没有出现404
后台的分发很正常
可能由于cache-host这里写了一组
导致squid并无法辨识进行针对性的分发
随即将所有的分发都分发到所有round-robin服务器上.
因为www.web.com后端的服务器较多.命中的概率较大.
而命中后第二次取出文件即是squid中的缓存文件,所以访问www.web.com时没有发现问题
而game.web.com因为真实服务器比例较小
分发时很多链接分发到其他web服务器.
导致反馈回很多404

为了让awstats分析squid的日志,我们要修改一下它的日志格式.我们先看看squid的默认格式和解释吧.

logformat squid  %ts.%03tu %6tr %>a %Ss/%03Hs %<st %rm %ru %un %Sh/%<A %mt

解释如下:
Seconds since epoch; subsecond time (milliseconds);  Response time (milliseconds); Client source IP address;  Squid request status (TCP_MISS etc); HTTP status code; Reply size including HTTP headers;  Request method (GET/POST etc) ; Request URL;  User name;  Squid hierarchy status (DEFAULT_PARENT etc);  Client FQDN;  MIME content type

下面看看要怎么样修改,awstats才能认识和分析squid的日志

squid: /etc/squid/squid.conf
代码:

logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh %{host}>h
access_log /var/log/squid/access.log combined

awstats: /etc/awstats/awstats.conf.local
代码:

LogFormat = "%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %other %virtualname"
LogFile="/var/log/squid/access.log"

关键点: squid 的日志需要加上虚拟主机名: %{host}>h awstats的读取参数指出 %virtualname 还有一种方法,我觉得也是比较理想的,直接修改apache的日志格式:

原:
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

修改为:
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""combined

这样也可以将apache的日志转为正常的日志。

转载自:扶凯[http://www.php-oa.com]