简介Nginx (“engine x”) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器;作为一款轻量级的Web服务器,具有占有内存少,并发能力强等优势,是高连接并发场景下Apache的不错的替代品;
本篇主要介绍Nginx作为Web服务器时,相对于Apache的性能优势;下一篇将会介绍Nginx作为方向代理服务器的实现;重要特点1、非阻塞:数据复制时,磁盘I/O的第一阶段是非阻塞的;2、事件驱动:通信机制采用epoll模型,支持更大的并发连接;
3、master/worker结构:一个master进程,生成一个或多个worker进程;基础架构
Nginx如何实现高并发:1、I/O模型采用异步非阻塞的事件驱动机制,由进程循环处理多个准备好的事件,如epoll机制;Nginx与Apache对高并发处理上的区别:1、对于Apache,每个请求都会独占一个工作线程,当并发量增大时,也会产生大量的工作线程,导致内存占用急剧上升,同时线程的上下文切换也会导致CPU开销增大,导致在高并发场景下性能下降严重;
2、对于Nginx,一个worker进程只有一个主线程,通过事件驱动机制,实现循环处理多个准备好的事件,从而实现轻量级和高并发;部署配置安装[root@tiejiang ~]# yum -y groupinstall “Development tools” [root@tiejiang ~]# yum -y groupinstall “Server Platform Development” [root@tiejiang ~]# yum install gcc openssl-devel pcre-devel zlib-devel [root@tiejiang ~]# groupadd -r nginx [root@tiejiang ~]# useradd -r -g nginx -s /sbin/nologin -M nginx [root@tiejiang ~]# tar xf nginx-1.4.7.tar.gz [root@tiejiang ~]# cd nginx-1.4.7 [root@tiejiang nginx-1.4.7]# mkdir -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-pcre [root@tiejiang nginx-1.4.7]# make && make install
配置[root@tiejiang ~]# vim /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” ] && exit 0 nginx=”/usr/sbin/nginx” prog=$(basename $nginx) NGINX_CONF_FILE=”/etc/nginx/nginx.conf” [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep “configure arguments:” | sed s/[^*]*–user=\([^ ]*\).*/\1/g -` options=`$nginx -V 2>&1 | grep configure arguments:` for opt in $options; do if [ `echo $opt | grep .*-temp-path` ]; then value=`echo $opt | cut -d “=” -f 2` if [ ! -d “$value” ]; then # echo “creating” $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $”Starting $prog: ” daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $”Stopping $prog: ” killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $”Reloading $prog: ” killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case “$1″ in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}” exit 2 esac [root@tiejiang ~]# chmod +x /etc/init.d/nginx # 复制主服务脚本执行权限 [root@tiejiang ~]# vim /etc/nginx/nginx.conf # 编辑主配置文件 worker_processes 2; error_log /var/log/nginx/nginx.error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } 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_timeout 65; server { listen 80; 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_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /www/lnmmp.com; } location ~ \.php$ { root /www/lnmmp.com; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } [root@tiejiang ~]# vim /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;
启动服务[root@tiejiang ~]# service nginx configtest # 服务启动前先验证配置文件是否正确 [root@tiejiang ~]# service nginx start [root@tiejiang ~]# ps -ef |grep nginx # 检查nginx进程,尤其是worker进程是否与worker_processes值一致 [root@tiejiang ~]# ss -antupl |grep 80 # 检查服务端口是否启动
性能测试测试说明1、每次测试都进行3次,最后数据取平均值;2、对比测试中的Apache采用event的MPM机制,最大化提高Apache的并发性能;3、每次测试后,都需重新启动服务(httpd或nginx),以防止多次测试数据不准;
测试工具:webbench1、优点:比ab能更好的模拟并发请求,最大支持模拟30000并发连接;测试方法# 安装wenbench [root@tiejiang ~]# wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz [root@tiejiang ~]# tar xf webbench-1.5.tar.gz [root@tiejiang ~]# cd webbench-1.5 [root@tiejiang webbench-1.5]# make && make install # 测试 [root@tiejiang webbench-1.5]# webbench -c 100 -t 30 http://172.16.25.112/nginx.html # 测试静态文件访问 [root@tiejiang webbench-1.5]# webbench -c 20 -t 30 http://172.16.25.112/test_mem.php # 测试动态文件访问
测试数据
分析趋势图静态文件访问趋势图
动态文件访问趋势图
总结综合上面测试得出的趋势图可以看出: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相比,还是不足以打动技术狂热份子的吧,哈哈!
参考资料:http://blog.csdn.net/yankai0219/article/details/8018275说明:测试使用的nginx.html和test_mem.php文件以及测试完整数据nginx_test.txt见附件;