[原]nginx折腾记(HTTP性能能测试,与Apache对比),nginxapache


话说nginx在大压力的环境中比apache的表现要好,于是下载了一个来折腾一下。

下载并编译安装,我的编译过程有点特别:

1。去除调试信息,修改$nginx_setup_path/auto/cc/gcc这个文件,将 CFLAGS="$CFLAGS -g"  这一行注释掉。

2。由于仅测试WEB服务器的性能,所以不安装FastCGI。

./configure \
  --prefix=/opt/nginx \
  --user=www \
  --group=www \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --without-http_fastcgi_module 

安装完成之后,将一堆生产环境中静态化了的HTML页面copy 到 nginx 的服务器上,我的 nginx.conf 的配置如下:

worker_processes  8;
worker_rlimit_nofile 102400;
events 
{
	use epoll;
	worker_connections  204800;
}
http 
{
	include       mime.types;
	default_type  application/octet-stream;
	sendfile        on;
	tcp_nopush     on;
	charset GBK ;
	keepalive_timeout  60;
	server_names_hash_bucket_size 128;
	client_header_buffer_size 2k;
	large_client_header_buffers 4 4k;
	client_max_body_size 8m;
	open_file_cache max=102400 inactive=20s;
	server 
	{
		listen       80;
				
		location / 
		{
		  root   /tmp/webapps/;
		  index  index.html index.htm;
		}
		
		location = /NginxStatus 
		{ 
		  stub_status on;
		  access_log off;
		}
		
		error_page   500 502 503 504  /50x.html;
		
		location = /50x.html 
		{
		    root   html;
		}
	}
}

为了使操作系统不成为瓶颈,调整了一下参数,如下:

[root@logserver etc]# cat sysctl.conf | grep -v "^$" | grep -v "^#"; 
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
fs.file-max = 6553600
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000

我这台是比较老的服务器了,DELL 2850 两颗 Intel(R) Xeon(TM) CPU 2.80GHz,OS认作4个CPU,4GB内存,OS如下:

[root@logserver etc]# uname -a
Linux logserver 2.6.9-78.ELsmp #1 SMP Thu Jul 24 23:54:48 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux
[root@logserver etc]# cat /etc/redhat-release 
CentOS release 4.7 (Final)

测试工具是 apache 的 ab ,用来模拟,大量的并发连接,本来是在另一台虚拟机中模拟客户端,但随着压力的上升,还没压死 nginx 就先将自己压死了 -_- ,最后只能自己压自己了。

测试脚本大概如下:

ab -n 100000 -c >client_number< [-k] http://***********/cms/index.html 

index.html 的大小是:123784 byte

我将测试数据整理到Excel中,猛击这里下载,如下:

nginx 短连接测试结果(1/20抽样展示)

nginx 长连接测试结果(1/20抽样展示)

单看数字可能比较枯燥,还是看图吧:

<ifmodule worker.c> ServerLimit 1000 ThreadLimit 11000 StartServers 40 MaxClients 30000 MinSpareThreads 1000 MaxSpareThreads 1000 ThreadsPerChild 300 MaxRequestsPerChild 0 </ifmodule>

Apache 的结果图形和nginx类似,但是大家请留意横坐标,最大是10000,而nginx最大的是20000,这是由于测到10000的时候,再往上加压力Apache就受不了,不是SWAP用尽就是连接超时。

 

我把nginx和Apache的图标拼在一起,方便对比:

 

从图表可以看到nginx作单纯的WEB服务器,也就是放静态内容,性能上比Apache要好,特别可承受压力、带宽及资源消耗上都要优于Apache。很多大型网站都喜欢把nginx放在前端,可能就是这个原因吧。

相关内容

    暂无相关文章