测试Nginx 和 Tomcat 高并发情况下处理静态页面的性能

本文通过ab压力测试对比了Nginx和Tomcat在处理静态页面时的性能。结果显示,Nginx在吞吐量和请求处理时间上优于Tomcat。建议通常配置Nginx负责静态资源,Tomcat处理动态请求,以实现更高的系统性能和负载均衡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是 ab 压力测试的结果(为了得到比较科学的数据可以进行多次的测试,一般至少10次)

ab 可执行文件的位置 /usr/local/web/apache/bin
测试命令:ab -n1000 -c100 192.168.1.155:9090/index.html

[root@bogon bin]# pwd
/usr/local/web/apache/bin
[root@bogon bin]# ab -n1000 -c100 ip:port/index.html

一、测试结果:

1、tomcat 处理静态请求

[root@bogon bin]# pwd
/usr/local/web/apache/bin
[root@bogon bin]# ab -n1000 -c100 192.168.1.155:9090/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 192.168.1.155 (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-Coyote/1.1
Server Hostname:        192.168.1.155
Server Port:            9090

Document Path:          /index.html
Document Length:        119 bytes

Concurrency Level:      100
Time taken for tests:   0.895 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      385476 bytes
HTML transferred:       126021 bytes
Requests per second:    1117.31 [#/sec] (mean)
Time per request:       89.501 [ms] (mean)
Time per request:       0.895 [ms] (mean, across all concurrent requests)
Transfer rate:          420.60 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   34  18.3     37      77
Processing:     7   42  18.5     38     100
Waiting:        4   33  16.8     29      94
Total:         19   76  17.7     78     170

Percentage of the requests served within a certain time (ms)
  50%     78
  66%     85
  75%     87
  80%     90
  90%     96
  95%    102
  98%    104
  99%    112
 100%    170 (longest request)

2、Nginx 处理静态请求

[root@bogon bin]# pwd
/usr/local/web/apache/bin
[root@bogon bin]# ab -n1000 -c100 192.168.1.155:8888/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 192.168.1.155 (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/1.11.9
Server Hostname:        192.168.1.155
Server Port:            8888

Document Path:          /index.html
Document Length:        123 bytes

Concurrency Level:      100
Time taken for tests:   0.207 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      451339 bytes
HTML transferred:       130011 bytes
Requests per second:    4841.40 [#/sec] (mean)
Time per request:       20.655 [ms] (mean)
Time per request:       0.207 [ms] (mean, across all concurrent requests)
Transfer rate:          2133.90 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    8   3.7      8      18
Processing:     2   10   4.8     11      25
Waiting:        0    7   3.4      7      16
Total:          4   19   6.9     21      35

Percentage of the requests served within a certain time (ms)
  50%     21
  66%     23
  75%     24
  80%     25
  90%     27
  95%     27
  98%     34
  99%     34
 100%     35 (longest request)

二、结果分析

1、tomcat 处理静态请求
这里写图片描述

2、Nginx 处理静态请求

这里写图片描述

三、总结

吞吐量,每个请求的处理时间等,Nginx代理处理静态请求 都是优于 Tomcat处理

相同的处理静态文件,Nginx处理的静态性能比Tomcat 好

一般在Nginx配置文件中,配置静态交给Nginx处理,动态请求交给Tomcat,提高了性能

Nginx +Tomcat 负载均衡与容错

在高并发的情况下,为了提高服务器的性能,减少了单台服务器的并发压力,我们采用了集群部署,还能解决为了避免单台服务器挂掉,服务不能访问这种情况下,处理容错问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值