在我的其中一台 vps 服务器上,我设置了 Apache2 和 nginx,nginx 在端口 8080 上,Apache2 在端口 80 上,并创建了一个静态 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 更少。
这是 nginx 的配置:
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 - 非线程):
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,但它提供静态 HTML 的速度比 nginx 更快。我有点困惑,难道我只需要重新配置 nginx 的设置吗?任何建议都值得赞赏。
更新 #1- 我安装apache2-utils
并运行了dstat
。我还更改了测试文件,因此它现在使用 9.7 mb 的 html 文件,Apache2 和 nginx 仍然非常一致。也许我需要限制可用内存量或其他方法来限制它的瓶颈。
这是dstat
我连续查询 9.7 mb 时的运行情况:
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
答案1
为了获得现实的结果,您必须进行现实的测试。对于您的测试场景来说,Apache 速度更快是完全可行的,但您真的只服务于一个 1 千字节的文件吗?
由于您使用的是 mpm-prefork,因此可以肯定地说,当有多个并发传输时,nginx 会消耗更少的内存。如果您有大文件或您的客户端的互联网连接速度很慢,则并发传输很容易堆积起来。当您的并发传输足以让 Apache 耗尽所有内存时,Nginx 将毫无疑问地获胜。
有人可能会说,只要 Apache 有足够的内存,这实际上就不是问题。然而,这并不是全部事实。当 http 服务器消耗的内存较少时,文件系统中的更多内容将被缓存,并且每次磁盘寻道的消除都将是一次小小的性能胜利。
答案2
在不受带宽限制的连接上(我怀疑您这里的小连接就是这样),gzip 压缩内容的传输速度会比非 gzip 压缩内容慢,因为需要额外的 CPU。压缩内容是通常速度更快,因为更小的块数据传输速度更快,但通过这种小测试可能没什么帮助。尝试比较同类产品,看看结果如何。
答案3
您是否尝试过通过从 ramdisk 提供文件来优化性能?某些 VPS 的 IOwait 时间非常长,这是由磁盘访问争用造成的。
尝试在进程运行dstat
时在服务器上运行ab
,看看磁盘是否受到了严重影响。
答案4
您的测试存在缺陷。 -c 5
没有正确测试任何服务器。像 nginx 这样的基于事件的服务器最适合同时处理数千个并发且可能很慢的下载。您测试了 5 个并发下载。-n 20000 -c 1000 可能会开始显示 nginx 性能更好。
尝试运行此工具对两台服务器进行测试,看看哪台服务器先崩溃
我敢打赌它不会是 nginx :-)