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

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

这几天简单的研究了一下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)

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


没有人做这样的测试吗?


是,而且还是用nginx代理node

【热门文章】
【热门文章】