阿里云

1.登录阿里云选择实例中的 “操作” 或 “远程链接” 即可进入服务器。抛开ssh,Workbench 比 vnc 的操作性更强,界面更友好。

2. 浏览器访问共有ip,否则不能访问。要将实例添加到安全组。

3.安装nginx nodejs

yum install nginx -> y

yum install nodejs  ->y

4.创建静态文件

eg: 

/root: 

mkdir static

vim index.html  -> 添加静态资源

cd /etc/nginx

vim nginx.config

修改 user nginx -> user root 因为是用root创建的静态文件防止因权限原因不能访问静态文件

修改 静态文件目录 root /root/static  ,动静分离的好处是不使用node输出静态文件,而是使用更强大的反向代理nginx 输出静态文件。

至此可以访问网页 http:// 公网ip地址

5. node 访问 与niginx 访问对比

编写index.js

const fs = require('fs');
const http = require('http');
const htmlBuffer = fs.readFileSync(__dirname + './index.html');
http.createServer((req, res)=>{
    res.writeHead(200, {'content-Type': 'text/html'});
    res.end(htmlBuffer)
}).listen(3000)

 

node index.js

然后

curl http://127.0.0.1:3000/index.html

 安装压测工具 ab

yum install httpd-tools -> y

条件 400 并发量,1600 次

node:

ab -c 400 -n1600 http://127.0.0.1:3000/

node 访问 qps 2851 每秒访问量

 nginx :

ab -c 400 -n1600 http://127.0.0.1:80/index.html

nginx 访问 qps 7653 每秒访问量, nginx 的 瓶颈在网卡,网络带宽的出口量, Transfer rate 基本会占满整个网卡。并发量大会有更大的优势。

 那我觉得如果服务端渲染的话,nodejs的服务端渲染会严重拖慢 并发请求。

 6. nginx 代理node服务器,路径解析的计算量提交到nginx 的c++上,这样效率会更高。

1.修改nginx 服务器配置

location ~ /node/(d*) {
                proxy_pass http://127.0.0.1:3000/detail?columnid=$1;

        }

  

2. 重启nginx ,否则访问 ip/node/123 会 404。

nginx -s reload

7. 负载均衡

后端会使用大量源站服务器,集群支持服务。

nginx 设置上游集群,可以设置其它的机器。

upstream node.com{
   server 127.0.0.1:3000;
   server 127.0.0.1:3001;  
}

  

 proxy_pass http://node.com/detail?columnid=$1;  

开启2个node 服务器:

 cp index.js index1.js

原文地址:https://www.cnblogs.com/zhangzs000/p/12321458.html