3月 16

squid 配置 和常用命令

【收集整理:张子萌 2009-11-2】

设置为缓荐服务器
cache_mem 30 M #内存使用大小。一般来说如果系统有内存,设置该值为(n/)3M。现在是3G 所以这里1G
cache_swap_low 90
cache_swap_high 95
max_open_disk_fds 0 #允许最大打开文件数量,0 无限制
minimum_object_size 0 KB #磁盘cache中最小的object的大小(低于这个值则不缓存)
maximum_object_size 20000 KB #磁盘cache中最大的object的大小(超过这个值则不缓存)
maximum_object_size_in_memory 4096 KB #装入内存缓存的文件大小,默认值是8K,超过8K的文件都不装入内存,可以在这里设成4M

cache_swap_low 90 #最小允许使用swap 90%
cache_swap_high 95 #最多允许使用swap 95%
cache_dir ufs /tmp1 10000 16 256 #磁盘缓存的类型和目录,大小,一二级目录的设置,这里磁盘缓存大小是10G
#ufs是一种文件存储方式 因为os一般都是从内存获取数据 那么内存的东西必须最后写的硬盘上
#sync 同步的时候用的ufs 那么squid也是一样
#ufs一般是同时写入内存和硬盘
#注意:size是按照M为单位的也就这个目录中最大存储容量的上限

acl QUERY urlpath_regex -i cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe #-i为不区分大小写
cache deny QUERY #设置不想缓存的目录或者文件类型

cache_mgr webmaster@test.com #管理员邮箱

###相关日志记录,可以设为none成不记录日志####
cache_store_log /var/log/squid/store.log
cache_log /var/log/squid/cache.log
access_log /var/log/squid/access.log combined

http_port 80 # squid监听的端口,客户访问网站的端口
httpd_accel_host ip_address # WEB服务器的IP地址
httpd_accel_single_host on # 设置转发为缓冲的请求到一台单独的机器
httpd_accel_port 81 # WEB服务器的端口
httpd_accel_uses_host_header off # 完成单台WEB服务器的反向代理功能
httpd_accel_with_proxy off # 停用代理
cache_peer www.XXXXX.com parent 80 0 no-query originserver name=www #定义不同的父节点,将节点设为no-query以及originserver说明这些节点是实际服务器
cache_peer_domain www.XXXXX.com #设定不同域名转发到不同的cache_peer上,如果没有这项.不同域名的域名可能被分发到同一台服务器上.
hierarchy_stoplist cgi-bin ? #用来强制某些特定的对象不被缓存,主要是处于安全的目的。

refresh_pattern . 0 20% 4320 override-expire override-lastmod reload-into-ims ignore-reload #更新cache规则
client_lifetime 120 minute #最大客户连接时间 120分钟
cache_mgr sky@test.com #指定当缓冲出现问题时向缓冲管理者发送告警信息的地址信息
request_entities off #禁止非http的标准请求,防止攻击
acl buggy_server url_regex ^http://…. http:// #只允许http的请求
# 设定可以访问的域名别名
acl HostA dstdomain www.XXXX.com
acl HostB dstdomain bbs.XXXX.com
# 加速器采用http协议在8000端口监听
acl acceleratedProtocol protocol HTTP
acl acceleratedPort port 8000
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

half_closed_clients off #半关闭状态的TCP连接,设为off,则一旦从客户端返回“no more data to read”的信息,squid就立即关闭该连接
refresh_pattern .htm 0 25% 8 #设置htm文件过期时间为10分钟

一些关于squid调试的命令:
1,初始化squid.conf 里配置的 cache 目录
#squid/sbin/squid -z
如果有错误提示,请检查 cache目录的权限。

2. squid.conf 排错,即验证 squid.conf 的语法和配置。
#squid/sbin/squid -k parse
如果squid.conf 有语法或配置错误,这里会返回提示你,如果没有返回,恭喜,可以尝试启动squid。

3.在前台启动squid,并输出启动过程。
#squid/sbin/squid -N -d1
如果有到 ready to server reques,启动成功。
然后 ctrl + c,停止squid,并以后台运行的方式启动。

4.启动squid在后台运行。
#squid/sbin/squid -s
可以通过ps -ef|grep squid 来查看系统进程。

5.停止 squid
#squid/sbin/squid -k shutdown

6.重引导修改过的 squid.conf
#squid/sbin/squid -k reconfigure
修改squid.conf配置后,先检查配置文件是否正确,然后再执行此指令,即可让squid按新quid.conf 来运行。

7.把squid添加到系统启动项 编辑
/etc/rc.d/rc.local 添加如下行:
/usr/local/squid/sbin/squid -s

8.查看你的日志文档。
#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目录中

3月 15

日志 压缩 备份 定时删除脚本

[编写整理:simonzhang 2010-03-15 2012-04-08修改]

  在linux下有大量日志需要压缩备份,并定期清理长期保存的备份日志。对于多处日志存放使用数组进行循环处理。脚本修改好后,设置定时任务即可。

#!/bin/sh
##############################################
# AUTHOR: simonzhang
# back log
# Ver : 1.1 For Production
# description: 
# 2010-03-12  
##############################################
####### set log patch
log_path=("/usr/local/nginx/logs/" "/usr/local/tomcat6/logs/" )
####### set backup log patch
bak_log_path=("/usr/local/nginx/logs/back" "/usr/local/tomcat6/logs/back")

####### set backup  3 day ago log
backupdays=3

#######clear 180 day ago compress log
deletedays=180

#######  start
for (( i=0 ; i<${#log_path[@]} ; i++ ))
do
        cd ${log_path[i]}
    if [ ! -f ${bak_log_path[i]} ] ; then
            /bin/mkdir -p ${bak_log_path[i]}
    fi
        /usr/bin/find  * -ctime +$backupdays -maxdepth 0 -not -name *.pid -not -name error* -exec zip -m {}.zip  {} \;
        /bin/mv *.zip ${bak_log_path[$i]}
        cd ${bak_log_path[$i]}
        echo /usr/bin/find  * -ctime +$deletedays -maxdepth 0 -exec rm {} \;
done
############  end
3月 15

网站压力测试微型工具技术备忘

[记录整理:张子萌 2010-03-15]

经常对服务器单个模块做非专业性压力测试,所以学习了两个小巧大的工具,做技术备忘。
如果需要做比较详细数据请使用LoadRunner、QALoad、JMeter等专业工具。

一款是apahe自带的ab工具。一款是webbench。

1.apache自带ab工具

此工具为apache自带可以显示较多测试信息,
http://apache.freelamp.com/httpd/httpd-2.2.15.tar.gz
以下编译只为获得ab工具,不作为apache安装使用,编译时添加安装路径,是为方便
不小心安装后删除方便。
# cd httpd-2.2.15
# ./configure –prefix=/usr/local/apache_ab
# make
# cd support/.libs/
在目录下已经可以看到ab程序了,ab程序主要参数解释如下

格式: ./ab [options] [http://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
# 在默认会话中发送请求的个数,默认时为50000
-c concurrency Number of multiple requests to make
# 一次请求所产生的次数
-t timelimit Seconds to max. wait for responses
# 测试所进行的最大秒数。默认时,没有时间c和n参数限制请求完毕为止。
-b windowsize Size of TCP send/receive buffer, in bytes
# 发送和接受包大小
-w Print out results in HTML tables
# 以HTML表的格式输出结果
-v verbosity How much troubleshooting info to print
# 打印头部信息,参数为输出级别1-4
-i Use HEAD instead of GET
# 使用head请求不是get请求
-H attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
# web使用庄户密码
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
# 对代理使用账户密码
-X proxy:port Proxyserver and port number to use
# 使用代理服务器

开始测试主要使用参数也就两个,以下对部分结果做解释
# ./ab -c 1000 -n 1000 http://172.192.1.100/
# url结尾如不是文件(如:index.html),一定要增加“/”
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.192.1.100 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: nginx
Server Hostname: 172.192.1.100
Server Port: 80

Document Path: /
Document Length: 2731 bytes
# 传输单个文档大小

Concurrency Level: 1000
# 并发数
Time taken for tests: 3.320 seconds
# 本次测试持续的时间
Complete requests: 1000
# 本次测试完成请求数量
Failed requests: 0
# 本此测试失败数量
Write errors: 0
Total transferred: 3039360 bytes
HTML transferred: 2749800 bytes
Requests per second: 301.18 [#/sec] (mean)
# 每秒钟处理数量的平均值
Time per request: 3320.263 [ms] (mean)
# 处理请求的平均响应时间
Time per request: 3.320 [ms] (mean, across all concurrent requests)
# 每个请求实际运行时间的平均值
Transfer rate: 893.94 [Kbytes/sec] received
# 平均每秒网络流量

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 200 701.6 26 3000
Processing: 1 275 373.3 254 3280
Waiting: 1 275 373.2 254 3280
Total: 1 475 735.5 302 3316
# 网络响应时间,主要为connect时间。
# 以下为请求处理时间的分布

Percentage of the requests served within a certain time (ms)
50% 302
66% 348
75% 362
80% 369
90% 410
95% 3008
98% 3016
99% 3311
100% 3316 (longest request)

2.webbench工具
获取地址:
http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz
获取版本进行编译
# tar zxvf webbench-1.5.tar.gz
# cd webbench-1.5
# make
在本目录下可以看到webbench软件,webbench程序主要参数如下
格式:webbench [option]… URL
-f|–force Don’t wait for reply from server.
-r|–reload &
nbsp; Send reload request – Pragma: no-cache.
-t|–time Run benchmark for seconds. Default 30.
-p|–proxy Use proxy server for request.
-c|–clients Run HTTP clients at once. Default one.
-9|–http09 Use HTTP/0.9 style requests.
-1|–http10 Use HTTP/1.0 protocol.
-2|–http11 Use HTTP/1.1 protocol.
–get Use GET request method.
–head Use HEAD request method.
–options Use OPTIONS request method.
–trace Use TRACE request method.
-?|-h|–help This information.
-V|–version Display program version.

使用100个并发持续5秒钟
#./webbench -c 100 -t 5 http://www.google.com/
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.google.com/
100 clients, running 5 sec.

Speed=36 pages/min, 4289 bytes/sec.
Requests: 3 susceed, 0 failed.

3.两款工具对比

ab工具显示测试数据比webbench详细。但是ab可测试并发量小于webbench,ab并发使用
不要超过1024(主要受到apache配置和系统资源限制),webbench并发使用数量不要超过10000,
如果超出此方为可能会引起测试机短暂假死现象,且不能获得返回数据。此两宽小型软件已经
够用,对于网上常见的Siege,http_load等就不看了。