动静分离
- 静态内容
- 基本不会变动,也不会因为请求参数不同而变化
- -> CDN 分发,HTTP 缓存等
- 动态内容
- 各种因为请求参数不同而变动,且变种的数量几乎不可枚举
- -> 用大量的源站机器承载,结合反向代理进程负载均衡
例
- 使用 nginx 做一个接入,针对静态文件的路径会直接返回,如果是动态内容,会转发到一个 Nodejs
在windows下使用 nginx
- 首先检查
80
端口是否有被占用
netstat -aon|findstr "80"
- 如果有,杀掉它【也可以在任务管理器中杀死它】
taskkill /pid pid号 /F
- 双击这个
nginx.exe
- 访问
localhost:80
。成功 Nice!
- 在根目录下创建 static 目录
- 在static文件夹里创建
index.html
,随便往里面写点儿东西
- 修改 nginx 的配置
- 关掉 nginx 进程
taskkill /f /t /im nginx.exe
- 启动 nginx
start nginx
- 会报 403 错误,我没有找到在 windows 下的解决方法
- 所以加一个属性,就可以成为下面这样,凑乎能用
- 实在是没法用,我放弃了在windows下使用nginx,接下来我把人家的过程简述一遍吧
在服务器上的配置过程
- 安装
nginx
yum install nginx
- 安装
nodejs
yum install nodejs
- 启动
nginx
nginx
- 判断在 80 端口占用了这个服务
netstat -nlp | grep 80
- 创建 static 目录
mkdir static
- 在 static 目录中创建 index.html 文件
cd static/ touch index.html
- vi index.html,随便写一个 html 进去
- 修改 nginx 配置
vi /etc/nginx/nginx.conf
- 重启 nginx
nginx -s reload
- 访问
XX.XX.XX.XX:80
,会发现主页改变了 - 在 static 目录中创建 index.js
touch index.js
- 编辑这个页面
const http = require('http');
const fs = require('fs');
const htmlBuffer = fs.readFileSync(__dirname + '/index.html');
http.createServer(function (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
-
进行压测
ab -c 400 -n1600 http://127.0.0.1:3000
-
压测使用nginx的,几乎是使用nodejs的两倍
ab -c 400 -n1600 http://127.0.0.1:80/index.html