这里是我自己的总结

参考B站简易入门教程:【【狂神说】Nginx最新教程通俗易懂,40分钟搞定!】

Nginx简介

Nginx的三个功能:

  • 反向代理
  • 负载均衡
  • 动静分离
    使用Nginx的场景:

反向代理: 当需要高并发后端处理消息的时候,往往分成很多个服务器分开处理,但是客户端的会话消息就分散在了很多服务器上,就需要一个中间件:Nginx对此进行转发。

负载均衡: 后端的服务器很多台,但是可能性能(内存啊空间啊什么的)不一样,因此还可以按照权重进行转发。

动静分离: 将动态资源和静态资源可以分布在不同服务器上从而达到动静分离.

什么是正向代理什么是反向代理

正向代理: 代理客户端:也就是帮助客户端发送消息的,叫做正向代理

反向代理: 代理服务端:也就是客户端没有感知,但是消息转发给很多服务器的,叫做反向代理

Nginx配置解析

参考csdn的文章【Nginx配置文件详解】

nginx主配置文件:/usr/local/nginx/conf/nginx.conf

默认启动 nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件。

可以在启动 nginx时通过 -c选项来指定要读取的配置文件。

原装配置详解

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
[root@centos7 nginx]# cat ./conf/nginx.conf

# 1、main全局块(全局设置), 作用域是全局

#Nginx用户及组:用户 组。window下不指定
#user nobody;
#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes 1;

#错误日志:存放路径。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid(进程标识符):存放路径。
#pid logs/nginx.pid;

# 2、events块(nginx工作模式)
events {
#每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。
#每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections
worker_connections 1024;
}

# 3、http块(http设置)
http {
#设定支持的mime类型,类型查看./conf/mime.types文件定义
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 推送
#tcp_nopush on;

#连接超时时间。
#keepalive_timeout 0;
keepalive_timeout 65;

# 开启压缩文件
#gzip on;

## ====sever块(主机设置)
server {
# 提供服务的端口,默认80
listen 80;
# 提供服务的域名主机名
server_name localhost;

#charset koi8-r;

# 访问日志记录 以及位置
#access_log logs/host.access.log main;

# location块(URL匹配):支持正则表达式
# 对 "/" 启用反向代理,第一个location区块开始
location / {
#服务启动目录 默认在nginx安装目录下html目录
root html;
# 默认的首页文件,多个用空格分开
index index.html index.htm;
}

# 错误页面路由
# 出现对应的http状态码时,使用50x.html回应客户
#error_page 404 /404.html;

# 将服务器错误页重定向到静态页/50x.html
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
# location区块开始,访问50x.html
location = /50x.html {
# 指定对应的站点目录为html
root html;
}

# 实例 入 将访问尾缀为 \.php 跳转到 127.0.0.1
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# 将PHP脚本传递给正在侦听127.0.0.1:9000的FastCGI服务器
# 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;
#}

# 拒绝访问.htaccess文件,如果Apache的文档根
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# 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;
# }
#}

}

可以发现,nginx.conf配置文件中的内容分为三个部分:

第一部分:main全局块(全局设置), 作用域是全局

第二部分:events块(nginx工作模式)

第三部分:http块(http设置)

下面逐一了解。

main全局块(全局设置)

events块(nginx工作模式)

http块(http设置)

以下全都来自黑马程序员笔记,自存方便看而已

nginx反向代理和负载均衡

对登录功能测试完毕后,接下来,我们思考一个问题:前端发送的请求,是如何请求到后端服务的?

前端请求地址:http://localhost/api/employee/login

后端接口地址:http://localhost:8080/admin/employee/login

image-20221107151607921 image-20221107151623005

很明显,两个地址不一致,那是如何请求到后端服务的呢?

image-20221107152041371

1). nginx反向代理

nginx 反向代理,就是将前端发送的动态请求由 nginx 转发到后端服务器

image-20221107152112092

那为什么不直接通过浏览器直接请求后台服务端,需要通过nginx反向代理呢?

nginx 反向代理的好处:

  • 提高访问速度

    因为nginx本身可以进行缓存,如果访问的同一接口,并且做了数据缓存,nginx就直接可把数据返回,不需要真正地访问服务端,从而提高访问速度。

  • 进行负载均衡

    所谓负载均衡,就是把大量的请求按照我们指定的方式均衡的分配给集群中的每台服务器。

  • 保证后端服务安全

    因为一般后台服务地址不会暴露,所以使用浏览器不能直接访问,可以把nginx作为请求访问的入口,请求到达nginx后转发到具体的服务中,从而保证后端服务的安全。

image-20221107153808368

nginx 反向代理的配置方式:

1
2
3
4
5
6
7
8
server{
listen 80;
server_name localhost;

location /api/{
proxy_pass http://localhost:8080/admin/; #反向代理
}
}

proxy_pass该指令是用来设置代理服务器的地址,可以是主机名称,IP地址加端口号等形式。

如上代码的含义是:监听80端口号, 然后当我们访问 http://localhost:80/api/../.. 这样的接口的时候,它会通过 location /api/ {}
这样的反向代理到 http://localhost:8080/admin/上来。

接下来,进到nginx-1.20.2\conf,打开nginx配置

1
2
3
4
5
# 反向代理,处理管理端发送的请求
location /api/ {
proxy_pass http://localhost:8080/admin/;
#proxy_pass http://webservers/admin/;
}

当在访问 http://localhost/api/employee/login ,nginx接收到请求后转到 http://localhost:8080/admin/
故最终的请求地址为 http://localhost:8080/admin/employee/login, 和后台服务的访问地址一致。

2). nginx 负载均衡

当如果服务以集群的方式进行部署时,那nginx在转发请求到服务器时就需要做相应的负载均衡。其实,负载均衡从本质上来说也是基于反向代理来实现的,最终都是转发请求。

nginx 负载均衡的配置方式:

1
2
3
4
5
6
7
8
9
10
11
12
upstream webservers{
server 192.168.100.128:8080;
server 192.168.100.129:8080;
}
server{
listen 80;
server_name localhost;

location /api/{
proxy_pass http://webservers/admin;#负载均衡
}
}

upstream:如果代理服务器是一组服务器的话,我们可以使用upstream指令配置后端服务器组。

如上代码的含义是:监听80端口号, 然后当我们访问 http://localhost:80/api/../..这样的接口的时候,它会通过 location /api/ {}
这样的反向代理到 http://webservers/admin ,根据webservers名称找到一组服务器,根据设置的负载均衡策略(默认是轮询)转发到具体的服务器。

注:upstream后面的名称可自定义,但要上下保持一致。

nginx 负载均衡策略:

名称说明
轮询默认方式
weight权重方式,默认为1,权重越高,被分配的客户端请求就越多
ip_hash依据ip分配方式,这样每个访客可以固定访问一个后端服务
least_conn依据最少连接方式,把请求优先分配给连接数少的后端服务
url_hash依据url分配方式,这样相同的url会被分配到同一个后端服务
fair依据响应时间方式,响应时间短的服务将会被优先分配

具体配置方式:

轮询:

1
2
3
4
upstream webservers{
server 192.168.100.128:8080;
server 192.168.100.129:8080;
}

weight:

1
2
3
4
upstream webservers{
server 192.168.100.128:8080 weight=90;
server 192.168.100.129:8080 weight=10;
}

ip_hash:

1
2
3
4
5
upstream webservers{
ip_hash;
server 192.168.100.128:8080;
server 192.168.100.129:8080;
}

least_conn:

1
2
3
4
5
upstream webservers{
least_conn;
server 192.168.100.128:8080;
server 192.168.100.129:8080;
}

url_hash:

1
2
3
4
5
upstream webservers{
hash &request_uri;
server 192.168.100.128:8080;
server 192.168.100.129:8080;
}

fair:

1
2
3
4
5
upstream webservers{
server 192.168.100.128:8080;
server 192.168.100.129:8080;
fair;
}