我如何优化nginx? 从基准testing看来,Apache2对于静态传输来说更快

在我的一台vps服务器上,我已经设置了Apache2和nginx,端口8080上的nginx和80上的Apache2,并创build了一个静态的HTML文件。

静态HTML / Apache2:

meder@meder-desktop:~$ sudo ab -n 1000 -c 5 http://medero.org/index.html This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking medero.org (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: Apache/2.2.9 Server Hostname: medero.org Server Port: 80 Document Path: /index.html Document Length: 1014 bytes Concurrency Level: 5 Time taken for tests: 6.186 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 1334000 bytes HTML transferred: 1014000 bytes Requests per second: 161.67 [#/sec] (mean) Time per request: 30.928 [ms] (mean) Time per request: 6.186 [ms] (mean, across all concurrent requests) Transfer rate: 210.61 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 12 15 2.1 14 35 Processing: 12 16 3.4 15 48 Waiting: 12 16 2.8 15 37 Total: 25 31 4.3 29 63 Percentage of the requests served within a certain time (ms) 50% 29 66% 30 75% 31 80% 32 90% 35 95% 39 98% 47 99% 51 100% 63 (longest request) 

静态HTML / Nginx:

 meder@meder-desktop:~$ sudo ab -n 1000 -c 5 http://medero.org:8080/index.html This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking medero.org (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: nginx/0.6.32 Server Hostname: medero.org Server Port: 8080 Document Path: /index.html Document Length: 1014 bytes Concurrency Level: 5 Time taken for tests: 6.424 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 1226000 bytes HTML transferred: 1014000 bytes Requests per second: 155.67 [#/sec] (mean) Time per request: 32.119 [ms] (mean) Time per request: 6.424 [ms] (mean, across all concurrent requests) Transfer rate: 186.38 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 13 15 2.5 14 36 Processing: 12 17 11.0 15 184 Waiting: 11 16 9.6 14 171 Total: 25 32 11.4 30 200 Percentage of the requests served within a certain time (ms) 50% 30 66% 31 75% 33 80% 33 90% 35 95% 38 98% 45 99% 50 100% 200 (longest request) 

我做了这么多次,结果几乎相同,Apache2比Nginx处理的时间更less。

这里是nginx的configuration:

 user www-data; worker_processes 4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; gzip on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 

和Apache 2.2.9-10(prefork – nonthreaded):

 MaxKeepAliveRequests 100 KeepAliveTimeout 15 <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule> 

加载模块:

 meder@host:~$ sudo apache2ctl -t -D DUMP_MODULES Loaded Modules: core_module (static) log_config_module (static) logio_module (static) mpm_prefork_module (static) http_module (static) so_module (static) alias_module (shared) auth_basic_module (shared) authn_file_module (shared) authz_default_module (shared) authz_groupfile_module (shared) authz_host_module (shared) authz_user_module (shared) autoindex_module (shared) cgi_module (shared) dir_module (shared) env_module (shared) mime_module (shared) negotiation_module (shared) php5_module (shared) rewrite_module (shared) setenvif_module (shared) status_module (shared) wsgi_module (shared) Syntax OK 

服务器详情:

 Debian Lenny 5.0.3 32-bit Unmanaged VPS 384MB Ram processor : 7 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Intel(R) Xeon(R) CPU E5405 @ 2.00GHz stepping : 6 cpu MHz : 1995.006 cache size : 6144 KB physical id : 1 siblings : 4 core id : 3 cpu cores : 4 apicid : 7 fpu : yes fpu_exception : yes cpuid level : 10 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx tm2 cx16 xtpr lahf_lm bogomips : 3990.03 clflush size : 64 cache_alignment : 64 address sizes : 38 bits physical, 48 bits virtual power management: 

记忆信息:

 cat /proc/meminfo MemTotal: 393216 kB MemFree: 304828 kB Buffers: 0 kB Cached: 0 kB SwapCached: 0 kB Active: 0 kB Inactive: 0 kB HighTotal: 0 kB HighFree: 0 kB LowTotal: 393216 kB LowFree: 304828 kB SwapTotal: 0 kB SwapFree: 0 kB Dirty: 0 kB Writeback: 0 kB AnonPages: 0 kB Mapped: 88388 kB Slab: 0 kB PageTables: 0 kB NFS_Unstable: 0 kB Bounce: 0 kB CommitLimit: 0 kB Committed_AS: 354892 kB VmallocTotal: 0 kB VmallocUsed: 0 kB VmallocChunk: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 Hugepagesize: 2048 kB 

看起来, mod_deflate甚至没有启用,所以我甚至没有在Apache2上使用gzip,但它比nginx更快地服务于静态HTML。 我有点困惑,难道是我需要重新configurationnginx的设置? 任何意见赞赏。

更新#1 – 我安装了apache2-utils并运行dstat 。 我还更改了testing文件,现在使用9.7 MB的html文件,Apache2和nginx仍然非常一致。 也许我需要限制可用的内存量或者瓶颈。

下面是我连续查询dstat

 sudo dstat ----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system-- usr sys idl wai hiq siq| read writ| recv send| in out | int csw 0 0 100 0 0 0| 0 0 | 0 0 | 0 0 | 0 7230 0 0 100 0 0 0| 0 0 |6071B 20k| 0 0 | 0 5534 0 0 100 0 0 0| 0 0 | 720B 21k| 0 0 | 0 4749 0 0 100 0 0 0| 0 0 | 822B 4788B| 0 0 | 0 5487 0 0 100 0 0 0| 0 0 | 288B 408B| 0 0 | 0 4625 0 0 100 0 0 0| 0 0 |5595B 4057B| 0 0 | 0 5966 0 0 100 0 0 0| 0 0 | 957B 3710B| 0 0 | 0 4904 0 0 100 0 0 0| 0 0 | 986B 5013B| 0 0 | 0 6906 0 0 100 0 0 0| 0 0 | 872B 3636B| 0 0 | 0 5614 0 0 100 0 0 0| 0 0 | 80B 368B| 0 0 | 0 5506 0 0 100 0 0 0| 0 0 | 80B 660B| 0 0 | 0 4883 0 0 100 0 0 0| 0 0 |1604B 5105B| 0 0 | 0 5087 0 0 100 0 0 0| 0 0 | 860B 3708B| 0 0 | 0 13k 0 0 100 0 0 0| 0 0 | 909B 3619B| 0 0 | 0 11k 0 0 100 0 0 0| 0 0 | 16k 44k| 0 0 | 0 5920 0 0 100 0 0 0| 0 0 | 132B 3256B| 0 0 | 0 6946 0 0 100 0 0 0| 0 0 | 184B 3589B| 0 0 | 0 5083 0 0 100 0 0 0| 0 0 | 869B 3637B| 0 0 | 0 5528 0 0 100 0 0 0| 0 0 | 917B 3576B| 0 0 | 0 5638 0 0 100 0 0 0| 0 0 | 9.8k 2299B| 0 0 | 0 5255 0 0 100 0 0 0| 0 0 |6205B 11k| 0 0 | 0 7230 0 0 100 0 0 0| 0 0 |1712B 35k| 0 0 | 0 4863 0 1 99 0 0 0| 0 0 | 243k 25M| 0 0 | 0 7432 0 1 99 0 0 0| 0 0 | 337k 33M| 0 0 | 0 8716 0 1 99 0 0 0| 0 0 | 297k 35M| 0 0 | 0 6786 0 1 99 0 0 0| 0 0 | 349k 33M| 0 0 | 0 7655 0 1 99 0 0 0| 0 0 | 338k 33M| 0 0 | 0 7605 0 1 99 0 0 0| 0 0 | 324k 34M| 0 0 | 0 7967 0 1 99 0 0 0| 0 0 | 320k 35M| 0 0 | 0 7235 0 1 99 0 0 0| 0 0 | 333k 35M| 0 0 | 0 7062 0 1 99 0 0 0| 0 0 | 355k 35M| 0 0 | 0 6209 0 1 99 0 0 0| 0 0 | 299k 33M| 0 0 | 0 8732 0 1 99 0 0 0| 0 0 | 369k 34M| 0 0 | 0 8610 0 0 100 0 0 0| 0 0 | 352k 34M| 0 0 | 0 7635 0 1 99 0 0 0| 0 0 | 331k 34M| 0 0 | 0 8087 0 1 99 0 0 0| 0 0 | 312k 35M| 0 0 | 0 6445 0 0 100 0 0 0| 0 0 | 81k 7879k| 0 0 | 0 6131 0 0 100 0 0 0| 0 0 | 80B 1848B| 0 0 | 0 5124 0 0 100 0 0 0| 0 0 | 120B 6216B| 0 0 | 0 5426 0 0 100 0 0 0| 0 0 | 120B 3256B| 0 0 | 0 4947 0 0 100 0 0 0| 0 0 | 15k 43k| 0 0 | 0 5632 0 0 100 0 0 0| 0 0 | 829B 8504B| 0 0 | 0 5913 0 0 100 0 0 0| 0 0 | 92B 384B| 0 0 | 0 8680 0 0 100 0 0 0| 0 0 | 926B 571B| 0 0 | 0 4843 0 0 100 0 0 0| 0 0 | 795B 675B| 0 0 | 0 5479 0 0 100 0 0 0| 0 0 | 280B 2048B| 0 0 | 0 4536 0 0 100 0 0 0| 0 0 | 172B 1760B| 0 0 | 0 6334 0 0 100 0 0 0| 0 0 | 120B 456B| 0 0 | 0 5710 0 0 100 0 0 0| 0 0 | 80B 408B| 0 0 | 0 6225 0 0 100 0 0 0| 0 0 | 120B 368B| 0 0 | 0 6639 0 0 100 0 0 0| 0 0 | 140B 328B| 0 0 | 0 5507 0 0 100 0 0 0| 0 0 |7487B 9697B| 0 0 | 0 7201 0 0 100 0 0 0| 0 0 | 920B 37k| 0 0 | 0 6086 0 0 100 0 0 0| 0 0 | 320B 536B| 0 0 | 0 5756 0 0 100 0 0 0| 0 0 | 40B 384B| 0 0 | 0 7153 0 0 100 0 0 0| 0 0 | 80B 368B| 0 0 | 0 5227 0 0 100 0 0 0| 0 0 | 80B 408B| 0 0 | 0 6042 0 0 100 0 0 0| 0 0 | 160B 368B| 0 0 | 0 6730 0 0 100 0 0 0| 0 0 | 80B 280B| 0 0 | 0 5424 0 0 100 0 0 0| 0 0 | 80B 336B| 0 0 | 0 8042 0 0 100 0 0 0| 0 0 | 40B 384B| 0 0 | 0 5559 0 0 100 0 0 0| 0 0 | 80B 280B| 0 0 | 0 6266 0 0 100 0 0 0| 0 0 | 80B 296B| 0 0 | 0 6198 0 0 100 0 0 0| 0 0 | 80B 456B| 0 0 | 0 6499 0 0 100 0 0 0| 0 0 | 80B 368B| 0 0 | 0 7143 

为了得到逼真的结果,你必须有真实的testing。 这是完全可行的apache是​​你的testing场景更快,但你真的只服务一个千字节的文件?

当你使用mpm-prefork时,可以肯定地说,当有多个并发传输时,​​nginx会消耗更less的内存。 如果您的文件较大,或者您的客户端的互联网连接速度较慢,那么并发传输很容易。 当Apache有足够的并发传输来吞噬所有的内存时,Nginx将胜出。

有人可能会认为,只要Apache有足够的内存空间,这个问题就不成问题了。 但是,这不是全部的事实。 当http服务器消耗更less的内存时,文件系统中的更多内容将被caching,而每个磁盘寻找消除将是一个小的性能胜利。

在一个没有带宽限制的连接上(因为我怀疑你的微小的连接是这样的),由于涉及额外的CPU,gzip压缩的内容将比非gzip压缩的内容传输慢。 压缩你的内容通常会更快,因为更小的数据块传输速度更快,但通过这个小testing可能无济于事。 尝试比较苹果和苹果,看看你得到什么。

您是否尝试通过从虚拟磁盘提供文件来优化性能? 一些VPS对于IOwait时间而言是非常糟糕的,这是由争用的磁盘访问引起的。

尝试在ab进程运行的同时在服务器上运行dstat ,查看磁盘是否遭到重创。

你的testing是有缺陷的。 -c 5不能正确testing任何一台服务器。 像nginx这样基于事件的服务器最好能同时处理数以千计的并发和可能较慢的下载。 您testing了5个并发下载。 -n 20000 -c 1000可能会开始显示nginxperformance更好。

试着在两台服务器上运行这个工具 ,看看哪一个先倒下

我敢打赌,它不会是nginx 🙂