Nginx - PM2 - Redis
Nginx安装与使用:
1.安装
Windows下只需要下载解压即可使用,下载地址:http://nginx.org/en/download.html
2.运行
双击打开文件夹根目录中nginx.exe
注:打开exe时不可以有中文路径,更改完后重新启动。最好在cmd面板中定位执行(否则 无法将“nginx”项识别为 cmdlet、函数、脚本文件或可运行程序的名称)
3.常用命令
nginx -h 查看帮助信息
nginx -v 查看Nginx的版本号
nginx -V 显示Nginx的版本号和编译信息
start nginx 启动Nginx
nginx -s stop 快速停止和关闭Nginx
nginx -s quit 正常停止或关闭Nginx
nginx -s reload 配置文件修改重新加载
nginx -t 测试Nginx配置文件的正确性及配置文件的详细信息
4.配置nginx.conf
#使员工Nginx的用户名
#user nobody;
#cpu数,一般设置成和服务器的cpu数一致
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程id
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
#设置mime类型,类型由mime.type文件定义
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指令指定Nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设定为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime
sendfile on;
#tcp_nopush on;
#设置超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#是否开启gzip压缩(网页速度优化非常有用,开启后通常可以达到70%的压缩率)
#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;
}
#定义错误提示页面,你还可以在这里添加500,403等,以空格分开
#error_page 404 /404.html;
#重定向
# redirect server error pages to the static page /50x.html
#定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 81;
server_name localhost;
location / {
#默认网站的根目录
root html;
#首页索引文件的名称
index index.html index.htm;
}
#location ~ *^.+\.*$ {
# root E:\huruji\blog\wechat_v1.1\public;
# access_log off;
# expires 24h;
#}
}
upstream foo { #负载均衡
least_conn; #负载均衡
server jsfan.net weight=3; #weight 为权重
server vectorjzc.top;
}
server {
listen 82;
server_name localhost;
location / { #调用负载均衡 访问 http://localhost:82 两个页面随机跳
proxy_pass http://foo;
index index.html index.htm;
}
location /movies/ { #访问 http://localhost:82/movies/ 跳到实战页
proxy_pass http://jsfan.net;
index index.html index.htm;
}
location /life/ { #访问 http://localhost:82/life/ 跳到生活页
index index.html index.htm;
proxy_pass http://jsfan.net;
}
location /getPassFriendLink/ { #跳转至请求数据页
index index.html index.htm;
proxy_pass http://jsfan.net:3000;
}
#location ~ *^.+\.*$ {
# root E:\huruji\blog\wechat_v1.1\public;
# access_log off;
# expires 24h;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
PM2安装与使用:
1.安装
cnpm install -g pm2 //安装PM2
2.常用命令
#启动和守护任何应用程序
pm2 start port.js
#查看已启动的服务(显示所有进程状态)
pm2 list
#查看某个进程具体情况
pm2 describe app
#命名进程
pm2 start port.js --name my-api
#显示所有进程日志
pm2 logs
#停止所有进程
pm2 stop all
#停止指定的进程
pm2 stop 0
#重启所有进程
pm2 restart all
#重启指定的进程
pm2 restart 0
#0秒停机重载进程 (用于 NETWORKED 进程)
pm2 reload all
#后台运行pm2,启动4个port.js
pm2 start port.js -i 4
#监控生产
pm2 monitor
#让pm2在服务器重启时自动引导
pm2 startup
#杀死指定的进程
pm2 delete 0
#杀死全部进程
pm2 delete all
#开启自动重启模式(监听当⽂件变化时重启项⽬)
pm2 start port.js --watch
#根据有效CPU数目启动最大进程数目
pm2 start port.js -i max
#后台运行pm2,启动3个进程
pm2 start port.js -i 3
#用fork模式启动 port.js 而不是使用 cluster
pm2 start port.js -x
#用fork模式启动 port.js 并且传递参数 (-a 23)
pm2 start port.js -x -- -a 23
#启动一个进程并把它命名为 serverone
pm2 start port.js --name serverone
#停止 serverone 进程
pm2 stop serverone
#启动进程, 在 app.json里设置选项
pm2 start app.json
#在--之后给 port.js 传递参数
pm2 start port.js -i max -- -a 23
#启动 并 生成一个配置文件
pm2 start port.js -i max -e err.log -o out.log
Redis安装与使用:
1.Node项目安装
cnpm install redis --save //安装redis
2.下载Redis安装包(例如:Redis-x64-3.2.100.zip)
3.解压此文件redis压缩包
4.cmd中定位此解压文件目录且运行 redis-server.exe redis.windows.conf
5.运行成功如下显示
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 17824
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[17824] 07 Jul 21:20:48.307 # Server started, Redis version 3.2.100
[17824] 07 Jul 21:20:48.311 * The server is now ready to accept connections on port 6379
6.常用命令
var redis = require('redis');
var client = redis.createClient(6379,'127.0.0.1');
client.on('error',function(err){
console.log('Error'+err)
})
//值的存取
// 命令 行为 返回值 使用示例(略去回调函数)
// set 设置存储在给定键中的值 OK set('key', 'value')
// get 获取存储在给定键中的值 value/null get('key')
// del 删除存储在给定键中的值(任意类型) 1/0 del('key')
// incrby 将键存储的值加上整数increment incrby('key', increment)
// decrby 将键存储的值减去整数increment decrby('key', increment)
// incrbyfloat 将键存储的值加上浮点数increment incrbyfloat('key', increm ent)
// append 将值value追加到给定键当前存储值的末尾 append('key', 'new-value')
// getrange 获取指定键的index范围内的所有字符组成的子串 getrange('key', 'start-index', 'end-index')
// setrange 将指定键值从指定偏移量开始的子串设为指定值 setrange('key', 'offset', 'new-string')
client.set('hello','This is a value',redis.print);
client.get('hello',function(err,value){
if(err) throw err
console.log('Get:'+value) //Get:This is a value
client.quit()
console.log('-----------以下为哈希表------------')
})
//哈希表
//哈希表有点类似ES6中的Map
// 命令 行为 返回值 使用示例(略去回调函数)
// hset 在散列里面关联起给定的键值对 1(新增)/0(更新) hset('hash-key', 'sub-key', 'value') (不支持数组、字符串)
// hget 获取指定散列键的值 hget('hash-key', 'sub-key')
// hgetall 获取散列包含的键值对 json hgetall('hash-key')
// hdel 如果给定键存在于散列里面,则移除这个键 hdel('hash-key', 'sub-key')
// hmset 为散列里面的一个或多个键设置值 OK hmset('hash-key', obj)
// hmget 从散列里面获取一个或多个键的值 array hmget('hash-key', array)
// hlen 返回散列包含的键值对数量 hlen('hash-key')
// hexists 检查给定键是否在散列中 1/0 hexists('hash-key', 'sub-key')
// hkeys 获取散列包含的所有键 array hkeys('hash-key')
// hvals 获取散列包含的所有值 array hvals('hash-key')
// hincrby 将存储的键值以指定增量增加 返回增长后的值 hincrby('hash-key', 'sub-key', increment) (注:假如当前value不为为字符串,则会无输出,程序停止在此处)
// hincrbyfloat 将存储的键值以指定浮点数增加
client.hmset('yj',{
'age':'22-year-old',
'sex':'man'
},redis.print)
client.hget('yj','age',function(err,value){
if(err) throw err
console.log('yj age is '+value)
//yj age is 22-year-old
})
client.hkeys('yj',function(err,keys){
if(err) throw err
keys.forEach(function(key,i){
console.log(key,i)
//age 0
//sex 1
})
client.quit()
console.log('-----------以下为链表------------')
})
//链表
//Redis链表类似JS数组,lpush向链表中添加值,lrange获取参数start和end范围内的链表元素, 参数end为-1,表明到链表中最后一个元素。
//注意:随着链表长度的增长,数据获取也会逐渐变慢(大O表示法中的O(n))
// 命令 行为 返回值 使用示例(略去回调函数)
// lpush 将给定值推入列表的右端 当前列表长度 rpush('key', 'value1' [,'value2']) (支持数组赋值)
// lrange 获取列表在给定范围上的所有值 array lrange('key', 0, -1) (返回所有值)
// lindex 获取列表在给定位置上的单个元素 lindex('key', 1)
// lpop 从列表左端弹出一个值,并返回被弹出的值 lpop('key')
// rpop 从列表右端弹出一个值,并返回被弹出的值 rpop('key')
// ltrim 将列表按指定的index范围裁减 ltrim('key', 'start', 'end')
client.lpush('tasks','Paint the house red.',redis.print)
client.lpush('tasks','Paint the house green.',redis.print)
client.lrange('tasks',0,-1,function(err,items){
if(err) throw err
items.forEach(function(item,i){
console.log(item)
//Paint the house green.
//Paint the house red. ps:如果后续再次执行程序则会累加
})
client.quit()
console.log('-----------以下为集合------------')
})
//集合
//类似JS中的Set,集合中的元素必须是唯一的(如不唯一则则不添加),其性能: 大O表示法中的O(1)
// 命令 行为 返回值 使用示例(略去回调函数)
// sadd 将给定元素添加到集合 插入元素数量 sadd('key', 'value1'[, 'value2', ...]) (不支持数组赋值)(元素不允许重复)
// smembers 返回集合中包含的所有元素 array(无序) smembers('key')
// sismenber 检查给定的元素是否存在于集合中 1/0 sismenber('key', 'value')
// srem 如果给定的元素在集合中,则移除此元素 1/0 srem('key', 'value')
// scad 返回集合包含的元素的数量 sacd('key')
// spop 随机地移除集合中的一个元素,并返回此元素 spop('key')
// smove 集合元素的迁移 smove('source-key'dest-key', 'item')
// sdiff 返回那些存在于第一个集合,但不存在于其他集合的元素(差集) sdiff('key1', 'key2'[, 'key3', ...])
// sdiffstore 将sdiff操作的结果存储到指定的键中 sdiffstore('dest-key', 'key1', 'key2' [,'key3...])
// sinter 返回那些同事存在于所有集合中的元素(交集) sinter('key1', 'key2'[, 'key3', ...])
// sinterstore 将sinter操作的结果存储到指定的键中 sinterstore('dest-key', 'key1', 'key2' [,'key3...])
// sunion 返回那些至少存在于一个集合中的元素(并集) sunion('key1', 'key2'[, 'key3', ...])
// sunionstore 将sunion操作的结果存储到指定的键中 sunionstore('dest-key', 'key1', 'key2' [,'key3...])
client.sadd('ip','123.0.0.1',redis.print)
client.sadd('ip','123.0.0.1',redis.print)
client.sadd('ip','123.0.0.2',redis.print)
client.sadd('ip','123.0.0.3',redis.print)
client.smembers('ip',function(err,members){
if(err) throw err
console.log(members)
//[ '123.0.0.3', '123.0.0.2', '123.0.0.1' ]
client.quit()
console.log('-----------信道------------')
})
//信道
//Redis超越了数据存储的传统职责,它还提供了信道,信道是数据传递机制,提供了发布/预定功能。
// var clientA = redis.createClient(6379,'127.0.0.1');
// var clientB = redis.createClient(6379,'127.0.0.1');
// clientA.on('message',function(channel,message){
// console.log(`${channel}-message:${JSON.stringify(message)}`)
// })
// clientB.on('subscribe',function(channel,count){
// console.log(channel,count)
// clientB.publish('main_chat_room','Hello world!')
// })
// clientA.subscribe('main_chat_room')
//上面代码中,clientA订阅了main_chat_room,这时clientB捕获到订阅事件,
//执行回调函数,clientB向main_chat_room发送了一条信息Hello world!
//clientA接受到信息后,在控制台打印出了相关信息。