Node部署 - [反向代理 + 负载均衡 + 线上部署 + PM2 + 缓存策略] - 看我的就够了

准备工作

方向代理
负载均衡
nginx 负载均衡的实现
HTTP Uptream模块
其他负载均衡的方法

缓存策略

实战

首先安装nginx

  • centos 可以用 yum神器
  • mac 环境使用 Homebrew神器 来安装
    brow 来源
    brew search nginx
    brew install nginx
    brew info nginx
    nginx -v 有版本输出;安装成功
  • win10 安装 nginx
    下载资源很多.这里跳过,讲下使用方法

    在nginx.exe目录,打开命令行工具,用命令 启动/关闭/重启nginx ; 或者配置环境变量
    start nginx : 启动nginx
    nginx -s reload  :修改配置后重新加载生效
    nginx -s reopen  :重新打开日志文件
    nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确
    nginx -s stop  :快速停止nginx
    nginx -s quit: 完整有序的停止nginx
    

nginx.conf 文件 里的模块介绍

//看一眼; 就可以了
#user  nobody;

# 工作进程 就是cpu 核数
worker_processes  1;

# nginx的日志
# 出错
#error_log  logs/error.log;
# 警告
#error_log  logs/error.log  notice;
# 基础信息
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


# 最大链接数
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    # 压缩
    #gzip  on;

    server {
        # 端口
        listen       80;
        # 路径
        server_name  localhost;

        # 设置语言
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # 项目路径;
        location / {
            root   html;
            index  index.html index.htm;
        }
        ....

配置我们自己的 nginx.conf

这里我们配置我们自己的 nginx.conf 文件负载均衡(但是原文件建议是备份一个)

worker_processes  auto;
events {
    worker_connections  1024;
}
http {
    #配置一 默认 这两个ip地址 权重一样 都是1:1 的概率访问到;
    #upstream firsttest {
        #server 47.104.64.78:3389;
        #server 47.104.64.79:3389;
    #}

    #配置二 ip_hash; 让用户落在第一个次访问ip上
    #upstream firsttest {
        #ip_hash;
        #server 47.104.64.78:3389;
        #server 47.104.64.79:3389;
    #}
    #配置三 ip 添加weight; 使当前被访问的 ip权重 为2:1
    upstream firsttest {
        server 47.104.64.78:3389 weight=2;
        server 47.104.64.79:3389;
    }

    server {
        listen 8088;
        location / {
            proxy_pass http://firsttest;
        }
   }
 }
nginx -t  执行文件
nginx -s reload 重启 nginx服务
本地打开localhost:8080 nginx代理到了阿里服务器地址 
这是我在阿里服务器的nginx欢迎页 ; 

这里写图片描述

配置我们自己的 pm2.json ##

http://pm2.keymetrics.io/ 官方网站
在此之前要先安装好pm2 >> npm install pm2 -g
这里写图片描述

  • pm2 命令 启动文件可以是.js 或者是.json 等很多文件类型.按着官方文档的格式写就可以了;
    • pm2 start app.js 启动
    • pm2 monit 监控
    • pm2 list 显示服务状态
    • pm2 stop app.js
    • pm2 restart app.js
    • pm2 delete app.js
PM2.json
{
    "name": "worker",
    //启动文件
    "script": "app.js",
    //监听
    "watch": true,
    "log_data_format":"YYYY-MM-DD HH:mm Z",
    "out_file":"log/node-app.stdout.log",
    //让node 占满cpu
    "instances" : "max",
    //让我们node以主线程启动
    "exec_mode" : "cluster" 
}

keymetrics平台监控 https://app.keymetrics.io/#/ 注册登陆上去就能连上自己的pm2 就能界面化 监控,查看cpu情况
这里写图片描述

把项目扔到服务器上 ##

比如我们用 build.zip 文件做测试

npm install –production 发布模式下npm
ps aux | grep node 产看程序的进程
netstat -anp |grep 3300 查看哪个端口被占用
kill -9 pid 杀死进程
ssh 用户名@地址 连接ssh
““mac环境: 文件 /文件夹
scp course-map.json root@ip地址:/路径
scp - r advance/ root@ip地址:/
unzip build.zip
“““在win环境下 直接使用winscp 傻瓜机;像ftp 一样直接任代码;即可

  • 首先把项目需要的文件. 压个包;

缓存策略 附加的

这里写图片描述

 //nginx.conf 中http 模块里面添加
    # 设置缓存
    # Etag  不过没有添加配置是默认打开的
    Etag off;
    # gzip打开压缩
    gzio on;
    # 过期时间
    expires 30d;
    #设置 告诉浏览器别去缓存它
    add_header Cache-Control no-cache;


这里写图片描述

这里写图片描述

详细到

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章