nginx---反向代理 node.js

  1. 安装 node.js
$ yum install nodejs
  1. 写一个 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
  1. 配置 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;
        }
    }
  1. 重新加载 nginx 配置文件。
nginx -s reload
  1. 访问
    在这里插入图片描述
  2. 看一下压测结果。
$ 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值