ferragamo shoes timberland outlet tory burch north face coats Timberland Sale paul smith bags Paul Smith handbag dsquared sale tods UK
Linux « So Tired!_!

Category: Linux

2012年02月8日

GoAccess,Apache/Nginx 日志统计分析软件

by Nick Xu — Categories: LinuxLeave a comment

什么是GoAccess?

GoAccess是运行在Unix系统终端,开源、实时分析Apache日志(其实也能够分析Nginx日志)并且能够动态呈现的软件。它为系统管理员提供了一个快速、有价值的HTTP统计,动态的可视化的服务器报告。

它是做什么以及如何工作?

从本质上讲,GoAccess的工作方式是,它将解析众所周知的Apache访问日志文件 access log,GoAccess通过解析日志收集的数据,将会显示它的控制台或某个终端。收集到的信息或生成的报告将显示给在视觉、交互窗口的系统管理员用户。报告包括:

  • 综合统计
  • 访客排行榜
  • 请求文件排行榜
  • 请求的静态文件排行榜,如图片、JS文件、CSS文件等
  • 访问来源
  • 404页面
  • 访问者的操作系统
  • 访问者的浏览器
  • 浏览器和蜘蛛(搜索爬虫)
  • 主机,反向DNS,IP位置
  • HTTP状态码
  • 推介网站
  • 搜索关键词
  • 显示时不同的配色方案
  • 无限制日志文件的大小(决定了GoAccess的打开速度)

日志文件格式

GoAccess可以解析Apache的日志格式,通用日志格式(CLF)和组合日志格式(XLF/ ELF),包括虚拟主机. 它也能够解析Nginx日志. (如果你按照Apache日志的格式配置了Nginx日志)

GoAccess主页 http://goaccess.prosoftcorp.com/

安装方法

1、安装GoAccess需要一些系统支持库

Ncurses 提供字符终端处理库,包括面板和菜单
GLib C语言的函数库(大于2.0的版本)
GeoIP 通过IP,定位他的经纬度,国家/地区,省市,甚至街道等位置信息。(如果不需要这个不是必须的,配置时候去掉这个选项 –enable-geoip)

CentOS下yum安装

yum install ncurses-devel
yum install glib2-devel

Ubuntu下apt安装

apt-get install libglib2.0-dev
apt-get install libncursesw5-dev

2、安装GoAccess

CentOS下安装

wget http://sourceforge.net/projects/goaccess/files/0.4.2/goaccess-0.4.2.tar.gz
tar zxvf goaccess-0.4.2.tar.gz
cd goaccess-0.4.2
./configure --enable-utf8
make
make install

Ubuntu下安装

apt-get install goaccess

GoAccess使用介绍

启动参数介绍:

  • -b 流量消耗统计,如果想要提高解析速度,不要开启这个选项。缺省值为不开启此选项
  • -f 日志文件路径。
  • -s HTTP 状态码报告,为了能够更好的解析日志,选项被默认关闭。
  • -e 指定IP地址的访问统计。
  • -a 通过代理的请求统计。

菜单介绍

  • F1 帮助菜单
  • F5 刷新
  • q 退出当前口窗、菜单、或是当前查看的选项
  • o 打开当前的选项、菜单
  • c 改变窗口配色(目前只有两种 默认和绿色)
  • SHIFT + TAB 从当前选定模块向后切换
  • RIGHT 打开当前选中模块,查看详细信息
  • s 通过日期排序,只会在访问请求模块起作用
  • S 通过点击次数排序,只会在访问请求模块起作用
  • / 查看详细信息的窗口进行搜索
  • n 通过/进行查找后,查找下个匹配的内容的位置,如果没有则在窗口底部显示“search hit BOTTOM”
  • t 在查看详细信息窗口,移动指针到最顶部
  • b 在查看详细信息窗口,移动指针到最底部

使用实例

  • 最简单、快速的使用方式
goaccess -f /data1/logs/blog.wuwangwo.net/access.log
  • 产生完整、全面统计数据的使用方式
goaccess -f /data1/logs/blog.wuwangwo.net/access.log -a -s -b
  • 通过管道的方式将更多access log 通过GoAccess解析
zcat access.log.*.gz | goaccess

或者

zcat -f access.log* | goaccess
  • 另外一种通过管道使用GoAccess解析日志的方式
sed -n '/05/Dec/2010/,$ p' access.log | goaccess -s -b
  • 低优先级运行GoAccess的方式
nice -n 19 goaccess -f access.log -s -a -b
  • 如果你只把GoAccess安装到了一台服务器上
ssh user@server 'cat /var/log/apache2/access.log' | goaccess -s -a -b

注意事项

  1. 每一个详细信息窗口,只显示300条数据
  2. GoAccess 通过管道解析日志时将禁用实时解析的功能

总结

GoAceess 不是很大的一款软件却有着强大的功能即使软件可能还有些BUG,软件的更多介绍信息请查看GoAccess的官方网站«./GoAccess»,当前版本的官方手册页面 Man Page

2012年02月8日

Nginx Block And Deny IP Address OR Network Subnets

by Nick Xu — Categories: LinuxLeave a comment

How do I block or deny access based on the host name or IP address of the client visiting website under nginx web server?

Nginx comes with a simple module called ngx_http_access_module to allow or deny access to IP address. The syntax is as follows:

deny IP;
deny subnet;
allow IP;
allow subnet;
# block all ips
deny    all;
# allow all ips
allow    all;
Note rules are checked in the order of their record to the first match.

How Do I Configure Nginx To Block IPs?

Edit nginx.conf file, enter (note my nginx path is set to /usr/local/nginx/, replace this according to your setup):
# cd /usr/local/nginx/conf/
# vi nginx.conf

Add the following line in http section:

## Block spammers and other unwanted visitors  ##
include blockips.conf;
Save and close the file. Finally, create blockips.conf in /usr/local/nginx/conf/, enter:
# vi blockips.conf

Append / add entries as follows:

deny 1.2.3.4;
deny 91.212.45.0/24;
deny 91.212.65.0/24;

Save and close the file. Test the config file, enter:
# /usr/local/nginx/sbin/nginx -t

Sample outputs:

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
Reload the new config, enter:
# /usr/local/nginx/sbin/nginx -s reload

How Do I Deny All and Allow Only Intranet/LAN IPs?

Edit config file as follows:

location / {
# block one workstation
deny    192.168.1.1;
# allow anyone in 192.168.1.0/24
allow   192.168.1.0/24;
# drop rest of the world
deny    all;
}
Granted access to network 192.168.1.0/24 with the exception of the address 192.168.1.1.

How Do I Customize HTTP 403 Forbidden Error Messages?

Create a file called error403.html in default document root, enter:
# cd /usr/local/nginx/html
# vi error403.html

<html>
<head><title>Error 403 – IP Address Blocked</title></head>
<body>
Your IP Address is blocked. If you this an error, please contact webmaster with your IP at webmaster@example.com
</body>
</html>

If SSI enabled, you can display the client IP easily from the html page itself:

Your IP Address is <!–#echo var=”REMOTE_ADDR” –> blocked.
Save and close the file. Edit your nginx.conf file, enter:
# vi nginx.conf

# redirect server error pages to the static page
error_page   403  /error403.html;
location = /error403.html {
root   html;
}
Save and close the file. Reload nginx, enter:
# /usr/local/nginx/sbin/nginx -s reload

See also:
Nginx Shell Script To Block Spamhaus Lasso Drop Spam IP Address

2012年02月7日

[转]禁止爬虫访问技巧

by Nick Xu — Categories: LinuxLeave a comment

有时候,一些秘密的入口,一些被爬虫爬过便有暴露风险的目录,我们必须采用一切手段来保卫。禁止一切爬虫进入,尽管称不上完全保密,但基本上可以把秘密的传播变得可控。

1.robots.txt

在想要保密的那个目录或者入口的根目录创建robots.txt,然后在里面加入以下内容,则听话的爬虫如Google爬虫在抓到你的网站的时候就会悄然离去。

User-Agent: *
Disallow: /

2.htaccess

由于并不是所有搜索引擎都像Google那样Do No Evil,所以并不是所有爬虫都像Google爬虫那样听话的遵循Robots.txt里的描述,这个时候我们需要用到htaccess来强制屏蔽那些恶 心的坏爬虫们的访问,这种屏蔽是直接的屏蔽。我们可以在要保密的那个目录或者入口的根目录的.htaccess文件中加入以下内容:

RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:craftbot@yahoo.com [OR]
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR]
RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR]
RewriteCond %{HTTP_USER_AGENT} ^Download\ Demon [OR]
RewriteCond %{HTTP_USER_AGENT} ^eCatch [OR]
RewriteCond %{HTTP_USER_AGENT} ^EirGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ^Express\ WebPictures [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro [OR]
RewriteCond %{HTTP_USER_AGENT} ^EyeNetIE [OR]
RewriteCond %{HTTP_USER_AGENT} ^FlashGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetRight [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetWeb! [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go!Zilla [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go-Ahead-Got-It [OR]
RewriteCond %{HTTP_USER_AGENT} ^GrabNet [OR]
RewriteCond %{HTTP_USER_AGENT} ^Grafula [OR]
RewriteCond %{HTTP_USER_AGENT} ^HMView [OR]
RewriteCond %{HTTP_USER_AGENT} HTTrack [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Stripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} Indy\ Library [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^InterGET [OR]
RewriteCond %{HTTP_USER_AGENT} ^Internet\ Ninja [OR]
RewriteCond %{HTTP_USER_AGENT} ^JetCar [OR]
RewriteCond %{HTTP_USER_AGENT} ^JOC\ Web\ Spider [OR]
RewriteCond %{HTTP_USER_AGENT} ^larbin [OR]
RewriteCond %{HTTP_USER_AGENT} ^LeechFTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mass\ Downloader [OR]
RewriteCond %{HTTP_USER_AGENT} ^MIDown\ tool [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mister\ PiX [OR]
RewriteCond %{HTTP_USER_AGENT} ^Navroad [OR]
RewriteCond %{HTTP_USER_AGENT} ^NearSite [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetAnts [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Net\ Vampire [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Octopus [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Explorer [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Navigator [OR]
RewriteCond %{HTTP_USER_AGENT} ^PageGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^Papa\ Foto [OR]
RewriteCond %{HTTP_USER_AGENT} ^pavuk [OR]
RewriteCond %{HTTP_USER_AGENT} ^pcBrowser [OR]
RewriteCond %{HTTP_USER_AGENT} ^RealDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^ReGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^SiteSnagger [OR]
RewriteCond %{HTTP_USER_AGENT} ^SmartDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperBot [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperHTTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Surfbot [OR]
RewriteCond %{HTTP_USER_AGENT} ^tAkeOut [OR]
RewriteCond %{HTTP_USER_AGENT} ^Teleport\ Pro [OR]
RewriteCond %{HTTP_USER_AGENT} ^VoidEYE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Image\ Collector [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebAuto [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebCopier [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebFetch [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebGo\ IS [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebLeacher [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebReaper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebSauger [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ eXtractor [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ Quester [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebStripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebWhacker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Wget [OR]
RewriteCond %{HTTP_USER_AGENT} ^Widow [OR]
RewriteCond %{HTTP_USER_AGENT} ^WWWOFFLE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Xaldon\ WebSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus [or]
RewriteCond %{HTTP_USER_AGENT} ^BaiduSpider
RewriteRule ^.* – [F,L]

 

nginx 用 if 和 $http_user_agent。

如:

  1. location / {
  2.     root   /home/www/;
  3.     if ($http_user_agent ~* ”qihoobot”) {
  4.         return 403;
  5.     }
  6.     #…
  7. }
location / {
	root   /home/www/;

	if ($http_user_agent ~* "qihoobot") {
		return 403;
	}
	#...
}

然后重启 nginx

2012年02月7日

转Linux流量监控工具 – iftop (最全面的iftop教程)

by Nick Xu — Categories: LinuxLeave a comment

在类Unix系统中可以使用top查看系统资源、进程、内存占用等信息。查看网络状态可以使用netstat、nmap等工具。若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop

一、iftop是什么?

iftop是类似于top的实时流量监控工具。

官方网站:http://www.ex-parrot.com/~pdw/iftop/

二、iftop有什么用?

iftop可以用来监控网卡的实时流量(可以指定网段)、反向解析IP、显示端口信息等,详细的将会在后面的使用参数中说明。

三、安装iftop

安装方法1、编译安装

如果采用编译安装可以到iftop官网下载最新的源码包。

安装前需要已经安装好基本的编译所需的环境,比如make、gcc、autoconf等。安装iftop还需要安装libpcap和libcurses。

CentOS上安装所需依赖包:

yum install flex byacc  libpcap ncurses ncurses-devel libpcap-devel

Debian上安装所需依赖包:

apt-get install flex byacc  libpcap0.8 libncurses5

下载iftop

wget http://www.ex-parrot.com/pdw/iftop/download/iftop-0.17.tar.gz

tar zxvf iftop-0.17.tar.gz

cd iftop-0.17

./configure

make && make install

安装方法2:(懒人办法,最简单)

直接省略上面的步骤

CentOS系统:

yum install flex byacc  libpcap ncurses ncurses-devel

wget ftp://fr2.rpmfind.net/linux/dag/redhat/el5/en/i386/dag/RPMS/iftop-0.17-1.el5.rf.i386.rpm

rpm -ivh iftop-0.17-1.el5.rf.i386.rpm

Debian系统 运行:apt-get install iftop

四、运行iftop

直接运行: iftop

效果如下图:

五、相关参数及说明

1、iftop界面相关说明

界面上面显示的是类似刻度尺的刻度范围,为显示流量图形的长条作标尺用的。

中间的<= =>这两个左右箭头,表示的是流量的方向。

TX:发送流量
RX:接收流量
TOTAL:总流量
Cumm:运行iftop到目前时间的总流量
peak:流量峰值
rates:分别表示过去 2s 10s 40s 的平均流量

2、iftop相关参数

常用的参数

-i设定监测的网卡,如:# iftop -i eth1

-B 以bytes为单位显示流量(默认是bits),如:# iftop -B

-n使host信息默认直接都显示IP,如:# iftop -n

-N使端口信息默认直接都显示端口号,如: # iftop -N

-F显示特定网段的进出流量,如# iftop -F 10.10.1.0/24或# iftop -F 10.10.1.0/255.255.255.0

-h(display this message),帮助,显示参数信息

-p使用这个参数后,中间的列表显示的本地主机信息,出现了本机以外的IP信息;

-b使流量图形条默认就显示;

-f这个暂时还不太会用,过滤计算包用的;

-P使host信息及端口信息默认就都显示;

-m设置界面最上边的刻度的最大值,刻度分五个大段显示,例:# iftop -m 100M

进入iftop画面后的一些操作命令(注意大小写)

按h切换是否显示帮助;

按n切换显示本机的IP或主机名;

按s切换是否显示本机的host信息;

按d切换是否显示远端目标主机的host信息;

按t切换显示格式为2行/1行/只显示发送流量/只显示接收流量;

按N切换显示端口号或端口服务名称;

按S切换是否显示本机的端口信息;

按D切换是否显示远端目标主机的端口信息;

按p切换是否显示端口信息;

按P切换暂停/继续显示;

按b切换是否显示平均流量图形条;

按B切换计算2秒或10秒或40秒内的平均流量;

按T切换是否显示每个连接的总流量;

按l打开屏幕过滤功能,输入要过滤的字符,比如ip,按回车后,屏幕就只显示这个IP相关的流量信息;

按L切换显示画面上边的刻度;刻度不同,流量图形条会有变化;

按j或按k可以向上或向下滚动屏幕显示的连接记录;

按1或2或3可以根据右侧显示的三列流量数据进行排序;

按<根据左边的本机名或IP排序;

按>根据远端目标主机的主机名或IP排序;

按o切换是否固定只显示当前的连接;

按f可以编辑过滤代码,这是翻译过来的说法,我还没用过这个!

按!可以使用shell命令,这个没用过!没搞明白啥命令在这好用呢!

按q退出监控。

六、常见问题

1、make: yacc: Command not found
make: *** [grammar.c] Error 127

解决方法:apt-get install byacc   /   yum install byacc

2、configure: error: Curses! Foiled again!
(Can’t find a curses library supporting mvchgat.)
Consider installing ncurses.

解决方法:apt-get install libncurses5-dev  /    yum  install ncurses-devel

2012年02月5日

Nginx的防盗链和限速处理

by Nick Xu — Categories: LinuxLeave a comment

防盗链处理

1 简单的通过referer判断

location ~ .*\.(gif|jpg|jpeg|png|bmp|wma|mp3|swf)$ {
      valid_referers none blocked server_names *.163.com 163.com baidu.com;
      if ($invalid_referer) {return 403;}
    expires      30d;
}

2 NginxHttpAccessKeyModule 插件

这个需要安装额外的模块,下载模块源码,然后按照说明文档,将下载来的源码中”$HTTP_ACCESSKEY_MODULE” 替换成 “ngx_http_accesskey_module“,再

  1. ./configure –add-module=path/to/nginx-accesskey

完了以后再重新编译一次nginx,然后就可以在配置文件中使了。

location /download {
  accesskey             on;
  accesskey_hashmethod  md5;
  accesskey_arg         "key";
  accesskey_signature   "mypass$remote_addr";
}

这样一个下载的文件就需要 http://example.com/download/file.zip?key=09093abeac094 这样的形式进行下载
其中 key的值为 mypass和下载客户端ip 进行md5运算获得。比如在 php 中可以向如下方式输出针对客户ip的下载链接地址

  1. <?
  2. $ipkeyvalue = md5(“mypass”.$_SERVER['REMOTE_ADDR']);
  3. $out_keyfile_link=”<a href=http://example.com/download/testfile.rar?key=”.$ipkeyvalue.”>防盗链文件下载</a><br />”;
  4. echo $out_keyfile_link;
  5. ?>

这样当一个用户将访问地址拷贝给别人时,因为访问ip不同,就造成 md5(“mypass”.ipaddr) 值不同,达到防盗链目的。

限速处理

限速使用 limit_zone, limit_conn 以及 limit_rate 进行配置
首先在 http 段配置一个 limit_zone,然后在需要的地方使用 limit_conn 和 limit_rate 进行限速设置,如下一个简单的例子

http {
  limit_zone   one  $binary_remote_addr  10m;
  server {
    location /files/ {
      limit_conn   one  1;
      limit_rate 20k;
    }
  }
}

说明:
limit_zone,是针对每个IP定义一个存储session状态的容器。这个示例中定义了一个名叫one的10m大小的容器,这个名字会在后面的limit_conn中使用。

limit_conn one 1;
限制在one中记录状态的每个IP只能发起一个并发连接。

limit_rate 20k;
对每个连接限速20k. 注意这里是对连接限速,而不是对IP限速。如果一个IP允许三个并发连接,那么这个IP就是限速为limit_rate×3,在设置的时候要根据自己的需要做设置调整,要不然会达不到自己希望的目的。

2012年01月17日

三种web性能压力测试工具http_load webbench ab Siege

by Nick Xu — Categories: LinuxLeave a comment

http_load

下载地址:http://www.acme.com/software/http_load/http_load-12mar2006.tar.gz

http_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载。但是它不同于大多数压力测试工具,它可以以一个单一的进程运行,一般不会把客户机搞死。可以可以测试HTTPS类的网站请求。

命令格式:http_load  -p 并发访问进程数  -s 访问时间  需要访问的URL文件
参数介绍

        -p 并发访问进程数

        -f 总的访问次数

        -r 每秒的访问频率

        -s 总的访问时间

通常参数组合:-p –f;-r -s

引用
http_load -p 30 -s 60  urllist.txt准备URL文件:tst.list,文件格式是每行一个URL,URL最好超过50-100个测试效果比较好,另外,测试结果中主要的指标是 fetches/sec 这个选项,即服务器每秒能够响应的查询次数,用这个指标来衡量性能

 

引用
% ./http_load -rate 10 -seconds 60 urllist.txt
49 fetches, 4 max parallel, 289884 bytes, in 10.0148 seconds
5916 mean bytes/connection
4.89274 fetches/sec, 28945.5 bytes/sec
msecs/connect: 28.8932 mean, 44.243 max, 24.488 min
msecs/first-response: 63.5362 mean, 81.624 max, 57.803 min

4.89274 fetches/sec 这个值得就是说服务器每秒能够响应的查询次数为4.8左右
这个值得是根据 49 fetches / 10.0148 seconds 秒计算出来的


webbench
webbench是Linux下的一个网站压力测试工具,最多可以模拟3万个并发连接去测试网站的负载能力。
下载地址:

http://cid-9601b7b7f2063d42.skydrive.live.com/self.aspx/Public/webbench-1.5.tar.gz

安装
#tar zxvf webbench-1.5.tar.gz
#cd webbench-1.5
#make && make install
会在当前目录生成webbench可执行文件,直接可以使用了

用法:
webbench -c 并发数 -t 运行测试时间 URL
如:webbench -c 5000 -t 120 http://www.askwan.com


ab 

ab是Apache服务器自己带的性能测试工具,用它可以对指定的URL进行模拟访问,然后生成一份有关访问效率的报表,从中可以大概看出网站在高压力情况下的表现。

Windows下的ab.exe一般都在Apache安装目录的bin目录下。这里举几个我实际中用过的例子:

对http://localhost/dz7/index.php产生400次请求
ab -n 400 http://localhost/dz7/index.php
测试完成后,ab会输出测试的结果:

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Finished 400 requests

Server Software:        Apache/2.2.11
Server Hostname:        localhost
Server Port:            80

Document Path:          /dz7/index.php
Document Length:        7015 bytes

Concurrency Level:      1
Time taken for tests:   21.623 seconds
Complete requests:      400
Failed requests:        399
   (Connect: 0, Receive: 0, Length: 399, Exceptions: 0)
Write errors:           0
Total transferred:      2968811 bytes
HTML transferred:       2805205 bytes
Requests per second:    18.50 [#/sec] (mean)
Time per request:       54.057 [ms] (mean)
Time per request:       54.057 [ms] (mean, across all concurrent requests)
Transfer rate:          134.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.3      0      16
Processing:    31   54  64.5     47    1265
Waiting:       16   51  64.3     47    1250
Total:         31   54  64.5     47    1265

Percentage of the requests served within a certain time (ms)
  50%     47
  66%     47
  75%     47
  80%     47
  90%     47
  95%     62
  98%    141
  99%    187
 100%   1265 (longest request)

参数-n设定请求的次数;另外,还可以用-c来设定并发访问数,例如要模拟50个并发访问,可以用
ab -n 400 -c 50 http://localhost/dz7/index.php

此外,还有一个实际的问题,就是如何用ab来模拟已登录的用户的访问。绝大部分程序的登录机制都是用Cookie+Session来实现的,浏览器在每次请求时都发送一个Cookie,里面记录了Session的SID。你可以先在利用Firefox的Firebug插件来确定Cookie里的内容是什么。
firebug_cookie
从中可以看到表示登录用户的内容是“QfI_sid=55ySsk”。ab可以用-C(大写的C)来设定每次请求用发送的Cookie。所以
ab -n 400 -c 50 -C "QfI_sid=55ySsk" http://localhost/dz7/index.php
就可以模拟已登录用户的400次访问,并发数为50的情况。

最后再透露一个绝技:
ab --help

 

四、Siege
一款开源的压力测试工具,可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进行。
官方:http://www.joedog.org/
Siege下载:http://soft.vpser.net/test/siege/siege-2.67.tar.gz
解压:
# tar -zxf siege-2.67.tar.gz
进入解压目录:
# cd siege-2.67/
安装:
#./configure ; make
#make install

使用
siege -c 200 -r 10 -f example.url
-c是并发量,-r是重复次数。 url文件就是一个文本,每行都是一个url,它会从里面随机访问的。

example.url内容:

http://www.licess.cn
http://www.vpser.net
http://soft.vpser.net

结果说明
Lifting the server siege… done.
Transactions: 3419263 hits //完成419263次处理
Availability: 100.00 % //100.00 % 成功率
Elapsed time: 5999.69 secs //总共用时
Data transferred: 84273.91 MB //共数据传输84273.91 MB
Response time: 0.37 secs //相应用时1.65秒:显示网络连接的速度
Transaction rate: 569.91 trans/sec //均每秒完成 569.91 次处理:表示服务器后
Throughput: 14.05 MB/sec //平均每秒传送数据
Concurrency: 213.42 //实际最高并发数
Successful transactions: 2564081 //成功处理次数
Failed transactions: 11 //失败处理次数
Longest transaction: 29.04 //每次传输所花最长时间
Shortest transaction: 0.00 //每次传输所花最短时间

2012年01月11日

nginx做非80端口转发

by Nick Xu — Categories: LinuxLeave a comment

nginx可以很方便的配置成反向代理服务器
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://147.16.24.175:9500;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via “nginx”;
}
}
但是如果nginx的监听端口不是默认的80端口,改为其他端口如81端口。
后端服务器中request.getServerPort()无法获得正确的端口,返回的仍然是80;
在response.sendRedirect()时,客户端可能无法获得正确的重定向url。
正确的配置方法为
在 $host之后加上端口号,如$host:81
server {
listen       83;
server_name  localhost;
location / {
proxy_pass  http://147.16.24.175:9500;
proxy_set_header   Host             $host:83;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_set_header Via    “nginx”;
}
}

2011年12月28日

用Windows的远程桌面连接访问Debian和Ubuntu图形界面

by Nick Xu — Categories: LinuxLeave a comment
Linux的服务器我一般是不安装图形界面的,但是有的客户需要使用VPS的图形界面,又觉得Windows的VPS性价比不高,所以我就尝试了在Windows上远程访问Debian和Ubuntu。

要实现这一功能非常简单,在Windows客户端无需任何设置,直接使用系统附件里的“远程桌面连接”即可。而在服务端,也只需要安装几个软件并做小小的设置即可。

一、服务端的安装与设置:

1.安装Tightvncserver和xrdp这两个软件包

  1. # apt-get install tightvncserver xrdp

复制代码

2.关闭Gnome Terminal的plugin以解决键盘映射错误的问题:
Application-system tools-editor
选择apps–>gnome_settings_daemon–>plugins–>keyboard–>取消勾选active

3.在终端运行vpcserver命令,输入密码后会生成相关配置文件:

  1. # vncserver

复制代码

二、Windows客户端的使用:

点击“开始”菜单-“所有程序”-“附件”-“远程桌面连接”,在“计算机”一栏里输入你VPS的IP地址,然后点击“连接”,在接下来的对话框里输入用户名和密码,点击“OK”之后即可进入Linux VPS的桌面。

想要退出的话只要选择注销(log out)即可。

未解决的问题:
Debian服务器的locale是中文,但是远程访问的界面却变成了英文,无法调出中文输入法,不过除终端外的中文显示正常;

2011年12月13日

在终端上,python打印出不同颜色的文字

by Nick Xu — Categories: Linux, PythonLeave a comment

终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无 关。

转义序列是以 ESC 开头,可以用 \033 完成相同的工作(ESC 的 ASCII 码用十进制表 示就是 27, = 用八进制表示的 33)。

\033[显示方式;前景色;背景色m

显示方式:0(默认值)、1(高亮)、22(非粗体)、4(下划线)、24(非下划线)、 5(闪烁)、25(非闪烁)、7(反显)、27(非反显)

前景色:30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋 红)、36(青色)、37(白色)

背景色:40(黑色)、41(红色)、42(绿色)、 43(黄色)、44(蓝色)、45(洋 红)、46(青色)、47(白色)

\033[0m 默认

\033[1;32;40m 绿色

033[1;31;40m 红色

print “\033[1;31;40m%s\033[0m” % ” 输出红色字符”

2011年12月2日

SSH远程会话管理工具screen使用教程

by Nick Xu — Categories: LinuxLeave a comment

一、screen命令是什么?
Screen是一个可以在多个进程之间多路复用一个物理终端的全屏窗口管理器。Screen中有会话的概念,用户可以在一个screen会话中创建多个screen窗口,在每一个screen窗口中就像操作一个真实的telnet/SSH连接窗口那样。
二、如何安装screen命令?
除部分精简的系统或者定制的系统大部分都安装了screen命令,如果没有安装,
CentOS系统可以执行:
yum install screen
Debian/Ubuntu系统执行:
apt-get install screen
三、screen命令使用方法?
1、常用的使用方法
用来解决文章开始我们遇到的问题,比如在安装lnmp时。
1.1 创建screen会话
可以先执行:screen -S lnmp ,screen就会创建一个名字为lnmp的会话。
1.2 暂时离开,保留screen会话中的任务或程序
当需要临时离开时(会话中的程序不会关闭,仍在运行)可以用快捷键Ctrl+a d(即按住Ctrl,依次再按a,d)
1.3 恢复screen会话
当回来时可以再执行执行:screen -r lnmp 即可恢复到离开前创建的lnmp会话的工作界面。如果忘记了,或者当时没有指定会话名,可以执行:screen -ls screen会列出当前存在的会话列表。
11791.lnmp即为刚才的screen创建的lnmp会话,目前已经暂时退出了lnmp会话,所以状态为Detached,当使用 screen -r lnmp后状态就会变为Attached,11791是这个screen的会话的进程ID,恢复会话时也可以使用:screen -r 11791
1.4 关闭screen的会话
执行:exit ,会提示:[screen is terminating],表示已经成功退出screen会话。
2、远程演示
首先演示者先在服务器上执行 screen -S test 创建一个screen会话,观众可以链接到远程服务器上执行screen -x test 观众屏幕上就会出现和演示者同步。
3、常用快捷键
Ctrl+a c :在当前screen会话中创建窗口
Ctrl+a w :窗口列表
Ctrl+a n :下一个窗口
Ctrl+a p :上一个窗口
Ctrl+a 0-9 :在第0个窗口和第9个窗口之间切换

 

目标:终端使用 less/more/grep 等命令正确显示 GBK 编码文件内容,vim 正确显示 GBK 编码文件汉字

症状:

1. 系统自带 gnome-terminal 在设置终端编码为 GBK 后,能达到目标。

2. 使用 xshell 在 windows 平台上设置终端编码为 default 时,ssh 登录到 CentOS,能达到目标。

3. 在 screen 命令窗口内,无论终端还是 vim, 中文均显示为乱码,无法达到目标。

解决办法:在 ~/.screenrc 中,添加下面两句:

defencoding GBK
encoding UTF-8 GBK

我的猜测是 xshell、gnome-terminal 等终端能够将自身编码传给系统,因此系统能够对输出自动进行转码。而 screen 属于终端中的终端,它自身的编码不是 GBK,导致传给系统以后没有对输出进行转码。设置 screen 的编码和转换规则后,就 OK 了。

© 2012 So Tired!_! All rights reserved - Wallow theme v0.46.4 by ([][]) TwoBeers - Powered by WordPress - Have fun!
$1.99 Domains* at GoDaddy.com
֩` ֩` `ȥ Moncler Jackets Moncler Jackets Sale ֩` Moncler Monclair Moncler On Sale Moncler Online Outlet Moncler For Sale Monclers For Cheap Moncler Stores Moncler Cheap Authentic Moncler Jackets Moncler Outlets "Genuine Ugg Boots" Moncler Store moncler buy Moncler Jacket Down Down Jacket Moncler monclerforkids Moncler It Shop Moncler Uk Moncler Moncler Ski Mens Down Coat Jackets And Coats LeatherJackets For Men Real Ugg Boots "Classic Short Ugg Boots"
Abercrombie Sale Abercrombie Outlet Abercrombie Sale Abercrombie Jackets North Face Outlet ferragamo north face coat north face clothes ferragamo shoes timberland outlet spyder jackets paul smith spyder jacket salvatore ferragamo outlet spyder ski jacket tory burch sale Timberland Boots sale