1.安装

下载windows版本解压后放到C盘

2.运行

1
start nginx     //用系统自带的cmd控制面板

3.其他命令

1
2
3
4
5
6
7
8
9
10
11
12
ps -ef |grep nginx    //  在进程列表里 面找master进程,它的编号就是主进程号了
kill -QUIT nginx主进程号 // 从容停止 要等所有请求结束后才关闭服务
kill -TERM nginx主进程号 // 立刻关闭nginx进程
kill -9 nginx主进程号 // 强制停止
kill -信号类型(HUP|TERM|QUIT) cat /usr/local/nginx/nginx.pid // 不用查看进程号来停止

nginx -s stop fast shutdown
nginx -s quit graceful shutdown
nginx -s reload changing configuration, starting new worker processes with a new configuration, graceful shutdown of old worker processes
nginx -s reopen re-opening log files

nginx -t // 检查配置文件是否正确

4.location命令

~ #波浪线表示执行一个正则匹配,区分大小写
~* #表示执行一个正则匹配,不区分大小写
~ ^ #~ ^表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
= #进行普通字符精确匹配
@ #”@” 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

1
2
deny all;    # 禁止访问
allow all; # 允许访问

5.配置文件 nginx.conf

负载均衡参考:http://www.linuxidc.com/Linux/2015-03/115207.htm
默认采用轮询方式负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
http {
upstream my_node_app { // upstream添加要实现负载均衡的节点
server 127.0.0.1:8000; // Node程序的IP地址和端口
}
server {
listen 80; //代理接收请求的端口
server_name localhost domain.com;
access_log /var/log/nginx/my_node_app.log;
location ~ /static/ { //处理URL路径以/static/开头的文件请求
root /home/node/my_node_app;
if (!-f $request_filename) {
return 404;
}
}
location / { //定义由代理响应的URL路径
proxy_pass http://my_node_app; //配置跳转到相应的负载均衡的节点--可以理解为要转到的*目标*服务器
# proxy_pass http://10.252.214.17:81; // 我测试直接用端口好像不太行,不过可以用反向代理进行多端口转发
# 下边这些是需要加的,否则会偶尔报错
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr; //可以在web服务器端获得用户的真实ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; // 在X-Forwarded-For情况下获取用户的ip
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}

location / {
proxy_pass https://www.baidu.com;
}

location = /Mzhuanzhuan/zzgjapp/detail/23545 { // 精准
proxy_pass https://www.ganji.com;
}

location ~ /b/ { // 包含就可以
proxy_pass https://www.sina.com;
}

location ~ ^/a/ { // 正则匹配开头
proxy_pass http://bj.58.com;
}

location ~ \.html$ { // 正则匹配结尾
proxy_pass https://www.qq.com;
}

}
}

6.负载均衡、集群、分布式

以下是个人理解:
一般集群需要负载均衡来分配工作任务。
分布式一般将应用服务器、图片服务器、数据库服务器这些分开,放在不同的服务器处理。
分布式是指将不同的业务分布在不同的地方,集群指的是将几台服务器集中在一起
Node的子进程可以实现集群,Node有自己的负载均衡,可以处理这些子进程的集群。

第一层:100
第二层: 负载均衡 进行分发(只是一个分发系统,Node手动遍历进行负载均衡,Node内部自己实现了负载均衡算法)
50 50
第三层: 集群(Node通过cluster.fork()生成集群)
Node Node Node Node Node Node(来处理上边第一个50个来源) Node Node Node Node Node Node (来处理上边第二个50个来源)

7.https搭建

7.1 创建服务器私钥

1
2
# 我在conf文件夹下生成的  但不是必须的
openssl genrsa -out zhangpeng25.key 2048

7.2 创建签名请求的证书(生成CSR)

生成CSR用于发给WoTrust/Thawte–证书颁发机构

1
openssl req -new -key zhangpeng25.key -out zhangpeng25.csr

7.3 创建一个自己签署的CA证书

1
openssl x509 -days 365 -req -in zhangpeng25.csr -signkey zhangpeng25.key -out zhangpeng25.crt

7.4 合并证书

1
cat zhangpeng25.crt > zhangpeng25.pem

7.5 证书位置

windows下,将证书文件放到conf目录下,
linux下,将证书文件放到nginx.conf所在的目录下
注:放在要求目录下,就不需要填写引用路径了

7.6 https配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
http {
upstream zz_ganji {
server 127.0.0.1:81;
}
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;
# HTTPS server

server {
listen 443 ssl; // 监听443端口
server_name localhost;

ssl_certificate zhangpeng25.pem; // 证书位置一定要对
ssl_certificate_key zhangpeng25.key; // 证书位置一定要对

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://zz_ganji;
}
}
}

7.7 history路由配置

1
2
3
4
5
6
location /Mzhuanzhuan/ZZMshare {
root /opt/web/preview/Mzhuanzhuan/ZZMshare;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
← Prev Next →