如何测试Nginx的高性能
1、基础架构

2、Nginx如何实现高并发:I/O模型采用异步非阻塞的事件驱动机制,由进程循环处理多个准备好的事件,如epoll机制;
3、Nginx与Apache对高并发处理上的区别:对于Apache,每个请求都会独占一个工作线程,当并发量增大时,也会产生大量的工作线程,导致内存占用急剧上升,同时线程的上下文切换也会导致CPU开销增大,导致在高并发场景下性能下降严重;对于Nginx,一个worker进程只有一个主线程,通过事件驱动机制,实现循环处理多个准备好的事件,从而实现轻量级和高并发;
4、部署配置安装yum-y groupinstall“Developmenttools”yum-y groupinstall“S髫潋啜缅erverPlatformDevelopment”yum install gcc openssl-devel pcre-devel zlib-develgroupadd-r nginxuseradd-r-g nginx-s/sbin/nologin-M nginxtar xf nginx-1.4.7.tar.gzcd nginx-1.4.7mkdir-pv/var/tmp/nginx./configure \--prefix=/usr \--sbin-path=/usr/sbin/nginx \--conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--pid-path=/var/run/nginx/nginx.pid \--lock-path=/var/lock/nginx.lock\--user=nginx \--group=nginx \--with-http_ssl_module \--with-http_flv_module \--with-http_stub_status_module \--with-http_gzip_static_module \--http-client-body-temp-path=/var/tmp/nginx/client/\--http-proxy-temp-path=/var/tmp/nginx/proxy/\--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \--http-scgi-temp-path=/var/tmp/nginx/scgi \--with-pcremake&&make install
5、配置vi/etc/init.d/nginx# 配置服务脚本#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /etc/nginx/nginx.conf# config: /etc/sysconfig/nginx# pidfile: /var/run/nginx.pid# Source function library../etc/rc.d/init.d/functions# Source networking configuration../etc/sysconfig/network# Check that networking is up.["$NETWORKING"="no"]&&exit0nginx="/usr/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[-f/etc/sysconfig/nginx]&&./etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs(){# make required directoriesuser=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`options=`$nginx -V 2>&1 | grep 'configure arguments:'`foroptin$options;doif[`echo $opt | grep '.*-temp-path'`];thenvalue=`echo $opt | cut -d "=" -f 2`if[!-d"$value"];then# echo "creating" $valuemkdir-p $value&&chown-R $user $valuefifidone}start(){[-x $nginx]||exit5[-f $NGINX_CONF_FILE]||exit6make_dirsecho-n $"Starting $prog: "daemon $nginx-c $NGINX_CONF_FILEretval=$?echo[$retval-eq0]&&touch $lockfilereturn$retval}stop(){echo-n $"Stopping $prog: "killproc $prog-QUITretval=$?echo[$retval-eq0]&&rm-f $lockfilereturn$retval}restart(){configtest||return$?stopsleep1start}reload(){configtest||return$?echo-n $"Reloading $prog: "killproc $nginx-HUPRETVAL=$?echo}force_reload(){restart}configtest(){$nginx-t-c $NGINX_CONF_FILE}rh_status(){status $prog}rh_status_q(){rh_status>/dev/null2>&1}case"$1"instart)rh_status_q&&exit0$1;;stop)rh_status_q||exit0$1;;restart|configtest)$1;;reload)rh_status_q||exit7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q||exit0;;*)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit2esacchmod+x/etc/init.d/nginx# 复***务脚本执行权限vi/etc/nginx/nginx.conf# 编辑主配置文件worker_processes2;error_log/var/log/nginx/nginx.error.log;pid/var/run/nginx.pid;events{worker_connections1024;}http{include mime.types;default_type application/octet-stream;log_format main'$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';sendfile on;keepalive_timeout65;server{listen80;server_name xxrenzhe.lnmmp.com;access_log/var/log/nginx/nginx.access.log main;location/{root/www/lnmmp.com;index index.php index.html index.htm;}error_page404/404.html;error_page500502503504/50x.html;location=/50x.html{root/www.linuxprobe.com;}location~\.php${root/www/lnmmp.com;fastcgi_pass127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}}vi/etc/nginx/fastcgi_params# 编辑fastcgi参数文件fastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;启动服务service nginx configtest# 服务启动前先验证配置文件是否正确service nginx startps-ef|grep nginx# 检查nginx进程,尤其是worker进程是否与worker_processes值一致ss-antupl|grep80# 检查服务端口是否启动
6、性能测试测试说明每次测试都进行3次,最后数据取平均值;对比测试中的Apache采用event的MPM机制,最大化提高Apache的并发性能;每次测试后,都需重新启动服务(httpd或nginx),以防止多次测试数据不准;
7、测试工具:webbench优点:比ab能更好的模拟并发请求,最大支持模拟30000并发连接;
8、测试方法# 安装wenbenchwget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gztar xf webbench-1.5.tar.gzcd webbench-1.5make&&make install# 测试webbench-c100-t30http://172.16.25.112/nginx.html # 测试静态文件访问webbench-c20-t30http://172.16.25.112/test_mem.php # 测试动态文件访问
9、测试数据


10、分析趋势图静态文件访问趋势图

11、动态文件访问趋势图

12、总结综合上面测试得出的趋鲛止敢携势图可以看出:1、静态文件测试时,低并发(200以下)情况下,Nginx和Apach的处理能力相当(2000pages/sec左右),当并发数超过200后,则Apache的处理能力开始下降,而Nginx保持稳定;同时随着并发量的增大,Apache令人诟病的内存占用和负载开始急剧上升,与此同时,Nginx在内存占用和负载方面的略微提升则可以忽略不计了;2、动态文件测试时,低并发(100以下)情况下,Nginx和Apache的处理能力相当(650pages/sec左右),但Nginx的内存占用和负载峰值只有Apache的50%左右;在高并发情况下(100以上),Apach的动态处理能力开始下滑,当并发达到500时,开始出现失败的请求,说明此时已达到的Apache的处理上限了,而反观Nginx,虽然处理动态请求会消耗更多的内存,但其处理能力随着并发量的上升而上升,即使并发1000动态请求,也未达到其处理能力上限;3、故不管是在静态文件请求还是动态文件请求方面,Nginx的性能都是强势优于Apache的;虽然可以通过系统调优的方式提高Apache的处理性能,但和Nginx相比,还是不足以打动技术狂热份子的吧,哈哈!