node http-proxy和nginx代理性能对比?

发布于 2022-09-02 11:14:40 字数 5001 浏览 20 评论 0

这几天简单的研究了一下http-proxy这个模块,做了一些简单的反向代理测试(也主要是和nginx对比了一下)
发现相同的并发和请求数,nginx的处理能力是http-proxy的2倍,请问大家一般都是使用nginx作为node的代理么?

http-proxy代理代码

var http = require('http')
var httpProxy = require('http-proxy')
var proxy = httpProxy.createProxyServer({})
var config = require(__dirname + '/config')
var LOCALHOST = 'http://127.0.0.1'

proxy.on('error', function(err, req, res) {
    res.writeHead(500, {'Content-Type': 'text/plain'})
    res.end('Something went wrong.')
})

var server = http.createServer(function(req, res) {
    var host = req.headers.host
    var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
    console.log('client ip: ' + ip + ', host: ' + host)

    switch (host) {
        case config[0].sitename:
            proxy.web(req, res, {target: LOCALHOST + ':' + config[0].port})
            break
        case config[1].sitename:
            proxy.web(req, res, {target: LOCALHOST + ':' + config[1].port})
            break
        case config[2].sitename:
            proxy.web(req, res, {target: LOCALHOST + ':' + config[2].port})
            break
        case config[3].sitename:
            proxy.web(req, res, {target: LOCALHOST + ':' + config[3].port})
            break
        case config[4].sitename:
            proxy.web(req, res, {target: LOCALHOST + ':' + config[4].port})
            break
        default:
            res.writeHead(200, {'Content-Type': 'text/plain'})
            res.end('http-proxy test.')
    }
})

console.log('listening on proxy-port 80')
server.listen(80)

http-proxy代理测试

root@lizi:/usr/local/src/webbench-1.5# ab -n 200000 -c 50 http://test01.lizi.com/
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 test01.lizi.com (be patient)
Completed 20000 requests
Completed 40000 requests
Completed 60000 requests
Completed 80000 requests
Completed 100000 requests
Completed 120000 requests
Completed 140000 requests
Completed 160000 requests
Completed 180000 requests
Completed 200000 requests
Finished 200000 requests


Server Software:        
Server Hostname:        test01.lizi.com
Server Port:            80

Document Path:          /
Document Length:        140 bytes

Concurrency Level:      50
Time taken for tests:   229.573 seconds
Complete requests:      200000
Failed requests:        0
Write errors:           0
Total transferred:      48200000 bytes
HTML transferred:       28000000 bytes
Requests per second:    871.18 [#/sec] (mean)
Time per request:       57.393 [ms] (mean)
Time per request:       1.148 [ms] (mean, across all concurrent requests)
Transfer rate:          205.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       7
Processing:     2   57  29.9     58     162
Waiting:        0   57  29.9     58     162
Total:          7   57  29.9     58     162

Percentage of the requests served within a certain time (ms)
  50%     58
  66%     65
  75%     72
  80%     90
  90%    104
  95%    114
  98%    123
  99%    128
 100%    162 (longest request)

nginx代理测试

root@lizi:/var/log/nginx# ab -n 200000 -c 50 http://test01.lizi.com/
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 test01.lizi.com (be patient)
Completed 20000 requests
Completed 40000 requests
Completed 60000 requests
Completed 80000 requests
Completed 100000 requests
Completed 120000 requests
Completed 140000 requests
Completed 160000 requests
Completed 180000 requests
Completed 200000 requests
Finished 200000 requests


Server Software:        nginx/0.7.67
Server Hostname:        test01.lizi.com
Server Port:            80

Document Path:          /
Document Length:        209 bytes

Concurrency Level:      50
Time taken for tests:   126.081 seconds
Complete requests:      200000
Failed requests:        0
Write errors:           0
Total transferred:      66400000 bytes
HTML transferred:       41800000 bytes
Requests per second:    1586.28 [#/sec] (mean)
Time per request:       31.520 [ms] (mean)
Time per request:       0.630 [ms] (mean, across all concurrent requests)
Transfer rate:          514.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2  97.9      0    9000
Processing:     2   29  17.2     33    3474
Waiting:        2   29  17.2     33    3474
Total:          5   32  99.7     33    9038

Percentage of the requests served within a certain time (ms)
  50%     33
  66%     34
  75%     34
  80%     35
  90%     36
  95%     38
  98%     39
  99%     41
 100%   9038 (longest request)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷又爱笑 2022-09-09 11:14:40

是,而且还是用nginx代理node

深陷 2022-09-09 11:14:40

单纯代理 肯定是nginx强,这个毋庸置疑,问题是nodejs 可以在代理之前处理一次请求,但是nginx不行。

掀纱窥君容 2022-09-09 11:14:40

没有人做这样的测试吗?

淡紫姑娘! 2022-09-09 11:14:40

https://github.com/fastify/fa...
最近看到一个比较强悍的~ 希望对比测试下

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文