- 安装 node.js
$ yum install nodejs
- 写一个 hello.js
$ vim /data/web/hello.js
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type':'text/plain'});
response.end('Hello World\n');
}).listen(8081);
console.log('Server started');
执行 hello.js
$ node hello.js
Server started
- 配置 nginx
$ vim /etc/nginx/nginx.conf
server {
listen 8082; # 监听端口
server_name localhost 106.52.175.128; # 指定 ip 地址或者域名,多个配置之间用空格分隔
location / {
proxy_pass http://106.52.175.128:8081; # node.js 应用的端口
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
- 重新加载 nginx 配置文件。
nginx -s reload
- 访问
- 看一下压测结果。
$ ab -n 1000 -c 100 http://106.52.175.128:8082/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 106.52.175.128 (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.12.2
Server Hostname: 106.52.175.128
Server Port: 8082
Document Path: /
Document Length: 173 bytes
Concurrency Level: 100
Time taken for tests: 1.062 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 325000 bytes
HTML transferred: 173000 bytes
Requests per second: 941.82 [#/sec] (mean)
Time per request: 106.177 [ms] (mean)
Time per request: 1.062 [ms] (mean, across all concurrent requests)
Transfer rate: 298.92 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 2 0.5 2 4
Processing: 2 102 297.9 5 1007
Waiting: 2 102 297.9 5 1007
Total: 3 105 298.0 7 1011
Percentage of the requests served within a certain time (ms)
50% 7
66% 7
75% 8
80% 8
90% 10
95% 1008
98% 1009
99% 1010
100% 1011 (longest request)
参考
服务器 nginx + nodejs 配置. https://www.phpsong.com/2126.html
如何在 CentOS 安装 node.js https://blog.csdn.net/lu_embedded/article/details/79138650
超实用压力测试工具-ab工具 https://www.jianshu.com/p/43d04d8baaf7