转自次元立方网:http://www.it165.net/admin/html/201405/2928.html
服务器负载均衡资料:http://www.it165.net/admin/serverfzjh/
简介
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器;
作为一款轻量级的Web服务器,具有占有内存少,并发能力强等优势,是高连接并发场景下Apache的不错的替代品;
本篇主要介绍Nginx作为Web服务器时,相对于Apache的性能优势;
下一篇将会介绍Nginx作为方向代理服务器的实现;
重要特点
非阻塞:数据复制时,磁盘I/O的第一阶段是非阻塞的;
事件驱动:通信机制采用epoll模型,支持更大的并发连接;
master/worker结构:一个master进程,生成一个或多个worker进程;
基础架构
Nginx如何实现高并发:
I/O模型采用异步非阻塞的事件驱动机制,由进程循环处理多个准备好的事件,如epoll机制;
Nginx与Apache对高并发处理上的区别:
对于Apache,每个请求都会独占一个工作线程,当并发量增大时,也会产生大量的工作线程,导致内存占用急剧上升,同时线程的上下文切换也会导致CPU开销增大,导致在高并发场景下性能下降严重;
对于Nginx,一个worker进程只有一个主线程,通过事件驱动机制,实现循环处理多个准备好的事件,从而实现轻量级和高并发;
部署配置
安装
01.
yum -y groupinstall “Development tools”
02.
yum -y groupinstall “Server Platform Development”
03.
yum install gcc openssl-devel pcre-devel zlib-devel
04.
groupadd -r nginx
05.
useradd -r -g nginx -s /sbin/nologin -M nginx
06.
tar xf nginx-
1.4
.
7
.tar.gz
07.
cd nginx-
1.4
.
7
08.
mkdir -pv /var/tmp/nginx
09.
./configure \
10.
--prefix=/usr \
11.
--sbin-path=/usr/sbin/nginx \
12.
--conf-path=/etc/nginx/nginx.conf \
13.
--error-log-path=/var/log/nginx/error.log \
14.
--http-log-path=/var/log/nginx/access.log \
15.
--pid-path=/var/run/nginx/nginx.pid \
16.
--lock-path=/var/lock/nginx.lock \
17.
--user=nginx \
18.
--group=nginx \
19.
--with-http_ssl_module \
20.
--with-http_flv_module \
21.
--with-http_stub_status_module \
22.
--with-http_gzip_static_module \
23.
--http-client-body-temp-path=/var/tmp/nginx/client/ \
24.
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
25.
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
26.
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
27.
--http-scgi-temp-path=/var/tmp/nginx/scgi \
28.
--with-pcre
29.
make && make install
配置:
001.
vi /etc/init.d/nginx # 配置服务脚本
002.
#!/bin/sh
003.
#
004.
# nginx -
this
script starts and stops the nginx daemon
005.
#
006.
# chkconfig: -
85
15
007.
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
008.
# proxy and IMAP/POP3 proxy server
009.
# processname: nginx
010.
# config: /etc/nginx/nginx.conf
011.
# config: /etc/sysconfig/nginx
012.
# pidfile: /var/run/nginx.pid
013.
# Source function library.
014.
. /etc/rc.d/init.d/functions
015.
# Source networking configuration.
016.
. /etc/sysconfig/network
017.
# Check that networking is up.
018.
[
"$NETWORKING"
=
"no"
] && exit
0
019.
nginx=
"/usr/sbin/nginx"
020.
prog=$(basename $nginx)
021.
NGINX_CONF_FILE=
"/etc/nginx/nginx.conf"
022.
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
023.
lockfile=/var/lock/subsys/nginx
024.
make_dirs() {
025.
# make required directories
026.
user=`nginx -V
2
>&
1
| grep
"configure arguments:"
| sed
's/[^*]*--user=\([^ ]*\).*/\1/g'
-`
027.
options=`$nginx -V
2
>&
1
| grep
'configure arguments:'
`
028.
for
opt in $options;
do
029.
if
[ `echo $opt | grep
'.*-temp-path'
` ]; then
030.
value=`echo $opt | cut -d
"="
-f
2
`
031.
if
[ ! -d
"$value"
]; then
032.
# echo
"creating"
$value
033.
mkdir -p $value && chown -R $user $value
034.
fi
035.
fi
036.
done
037.
}
038.
start() {
039.
[ -x $nginx ] || exit
5
040.
[ -f $NGINX_CONF_FILE ] || exit
6
041.
make_dirs
042.
echo -n $
"Starting $prog: "
043.
daemon $nginx -c $NGINX_CONF_FILE
044.
retval=$?
045.
echo
046.
[ $retval -eq
0
] && touch $lockfile
047.
return
$retval
048.
}
049.
stop() {
050.
echo -n $
"Stopping $prog: "
051.
killproc $prog -QUIT
052.
retval=$?
053.
echo
054.
[ $retval -eq
0
] && rm -f $lockfile
055.
return
$retval
056.
}
057.
restart() {
058.
configtest ||
return
$?
059.
stop
060.
sleep
1
061.
start
062.
}
063.
reload() {
064.
configtest ||
return
$?
065.
echo -n $
"Reloading $prog: "
066.
killproc $nginx -HUP
067.
RETVAL=$?
068.
echo
069.
}
070.
force_reload() {
071.
restart
072.
}
073.
configtest() {
074.
$nginx -t -c $NGINX_CONF_FILE
075.
}
076.
rh_status() {
077.
status $prog
078.
}
079.
rh_status_q() {
080.
rh_status >/dev/
null
2
>&
1
081.
}
082.
case
"$1"
in
083.
start)
084.
rh_status_q && exit
0
085.
$
1
086.
;;
087.
stop)
088.
rh_status_q || exit
0
089.
$
1
090.
;;
091.
restart|configtest)
092.
$
1
093.
;;
094.
reload)
095.
rh_status_q || exit
7
096.
$
1
097.
;;
098.
force-reload)
099.
force_reload
100.
;;
101.
status)
102.
rh_status
103.
;;
104.
condrestart|
try
-restart)
105.
rh_status_q || exit
0
106.
;;
107.
*)
108.
echo $
"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
109.
exit
2
110.
esac
111.
chmod +x /etc/init.d/nginx # 复***务脚本执行权限
112.
vi /etc/nginx/nginx.conf # 编辑主配置文件
113.
worker_processes
2
;
114.
error_log /var/log/nginx/nginx.error.log;
115.
pid /var/run/nginx.pid;
116.
events {
117.
worker_connections
1024
;
118.
}
119.
http {
120.
include mime.types;
121.
default_type application/octet-stream;
122.
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
123.
'$status $body_bytes_sent "$http_referer" '
124.
'"$http_user_agent" "$http_x_forwarded_for"'
;
125.
sendfile on;
126.
keepalive_timeout
65
;
127.
server {
128.
listen
80
;
129.
server_name xxrenzhe.lnmmp.com;
130.
access_log /var/log/nginx/nginx.access.log main;
131.
location / {
132.
root /www/lnmmp.com;
133.
index index.php index.html index.htm;
134.
}
135.
error_page
404
/
404
.html;
136.
error_page
500
502
503
504
/50x.html;
137.
location = /50x.html {
138.
root /www/lnmmp.com;
139.
}
140.
location ~ \.php$ {
141.
root /www/lnmmp.com;
142.
fastcgi_pass
127.0
.
0.1
:
9000
;
143.
fastcgi_index index.php;
144.
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
145.
include fastcgi_params;
146.
}
147.
}
148.
}
149.
vi /etc/nginx/fastcgi_params # 编辑fastcgi参数文件
150.
fastcgi_param GATEWAY_INTERFACE CGI/
1.1
;
151.
fastcgi_param SERVER_SOFTWARE nginx;
152.
fastcgi_param QUERY_STRING $query_string;
153.
fastcgi_param REQUEST_METHOD $request_method;
154.
fastcgi_param CONTENT_TYPE $content_type;
155.
fastcgi_param CONTENT_LENGTH $content_length;
156.
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
157.
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
158.
fastcgi_param REQUEST_URI $request_uri;
159.
fastcgi_param DOCUMENT_URI $document_uri;
160.
fastcgi_param DOCUMENT_ROOT $document_root;
161.
fastcgi_param SERVER_PROTOCOL $server_protocol;
162.
fastcgi_param REMOTE_ADDR $remote_addr;
163.
fastcgi_param REMOTE_PORT $remote_port;
164.
fastcgi_param SERVER_ADDR $server_addr;
165.
fastcgi_param SERVER_PORT $server_port;
166.
fastcgi_param SERVER_NAME $server_name;
启动服务:
1.
service nginx configtest # 服务启动前先验证配置文件是否正确
2.
service nginx start
3.
ps -ef |grep nginx # 检查nginx进程,尤其是worker进程是否与worker_processes值一致
4.
ss -antupl |grep
80
# 检查服务端口是否启动
性能测试
测试说明
每次测试都进行3次,最后数据取平均值;
对比测试中的Apache采用event的MPM机制,最大化提高Apache的并发性能;
每次测试后,都需重新启动服务(httpd或nginx),以防止多次测试数据不准;
测试工具:webbench
优点:比ab能更好的模拟并发请求,最大支持模拟30000并发连接;
测试方法
1 2 3 4 5 6 7 8 # 安装wenbench
wget http:
//blog
.s135.com
/soft/linux/webbench/webbench-1
.5.
tar
.gz
tar
xf webbench-1.5.
tar
.gz
cd
webbench-1.5
make
&&
make
install
# 测试
webbench -c 100 -t 30 http:
//172
.16.25.112
/nginx
.html
# 测试静态文件访问
webbench -c 20 -t 30 http:
//172
.16.25.112
/test_mem
.php
# 测试动态文件访问
测试数据
分析趋势图
静态文件访问趋势图
动态文件访问趋势图
总结
综合上面测试得出的趋势图可以看出:
静 态文件测试时,低并发(200以下)情况下,Nginx和Apach的处理能力相当(2000pages/sec左右),当并发数超过200后,则 Apache的处理能力开始下降,而Nginx保持稳定;同时随着并发量的增大,Apache令人诟病的内存占用和负载开始急剧上升,与此同 时,Nginx在内存占用和负载方面的略微提升则可以忽略不计了;
动态文件测试时,低并发 (100以下)情况下,Nginx和Apache的处理能力相当(650pages/sec左右),但Nginx的内存占用和负载峰值只有Apache的 50%左右;在高并发情况下(100以上),Apach的动态处理能力开始下滑,当并发达到500时,开始出现失败的请求,说明此时已达到的Apache 的处理上限了,而反观Nginx,虽然处理动态请求会消耗更多的内存,但其处理能力随着并发量的上升而上升,即使并发1000动态请求,也未达到其处理能 力上限;
故不管是在静态文件请求还是动态文件请求方面,Nginx的性能都是强势优于Apache的;虽然可以通过系统调优的方式提高Apache的处理性能,但和Nginx相比,还是不足以打动技术狂热份子的吧,哈哈!