一、Nginx核心概念
什么是Nginx?
Nginx 是高性能的 HTTP 和反向代理的服务器同时也是邮件代理服务器。
官方地址:https://nginx.org/
什么是反向代理服务器
没有Nginx之前我们的请求是从客户端直接到后端服务,后端服务器响应后直接返回客户端,如图:
现在是Nginx代理后端服务器来接收客户端发送的请求,这就是Nginx的反向代理,如图:
二、Nginx的应用场景
应用场景
Nginx主要应用在集群系统中。
三、Nginx项目落地查询商品为例落地,启动两个实例,一个端口号:5000,另一个端口号为:5001,如图:
查询商品的项目新建一个API控制器【ProductController】,代码如下:
[Route("api/[controller]")]
[ApiController]
public class ProductController : Controllerbase
{
///
///获取商品数据
///
///
[HttpGet]
public IActionResult Get()
{
List list = new List() {
new Product(){
Name="手机",
Price = 1000
},
new Product(){
Name="笔记本电脑",
Price = 5000
},
new Product() {
Name="电视机",
Price = 5000
}
};
System.Console.WriteLine("查询商品");
return Ok(list);
}
}
新建一个商品领域模型类【Product】,代码如下:
public class Product
{
///
/// 主键
///
public Guid Id { get; set; } = Guid.NewGuid();
///
/// 名称
///
public string Name { get; set; }
///
/// 价格
///
public decimal Price { get; set; }
}
启动后,如图:
第一个实例:端口:5000
第二个实例,端口:5001
Nginx使用的是Windows版为例,后期会出Linux版本,Windows使用的版本是:nginx-1.20.0,
百度网盘盘下载地址:
链接:https://pan.baidu.com/s/1IZ-GWD3Al_QwqsJ-is9HqA 提取码:g2wt
添加配置文件信息
修改Nginx配置文件信息 (nginx.conf),配置文件文件路径:nginx-1.20.0conf
server {
listen 80;
server_name localhost;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#代理
location / {
proxy_pass http://Demo.Application;
}
}
#负载均衡(分流配置)
upstream Demo.Application{
server localhost:5000;
server localhost:5001;
}
启动 Nginx
先进入Nginx根目录下
cd nginx根目录下
启动命令
nginx.exe
请求代理服务器,如图:
刷新了三次请求Nginx服务器,请求结果,如图
四、Nginx的运行原理
Nginx是模块化设计,里面包括很多模块,其中核心模块:邮件模块、HTTP模块、事件模块
如图:
配置文件
全局模块和事件模块 【核心模块】
#全局模块
#user nobody;
worker_processes 1;
#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模块
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;
#}
#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;
#}
#代理
location / {
proxy_pass http://Demo.Application;
}
# 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;
#}
}
#负载均衡(分流配置)
upstream Demo.Application{
server localhost:5000;
server localhost:5001;
}
# 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 ,由master转发给多个worke,然后由worke就会转发到我们的后端服务实例(5000,5001),这种形式我们就称之为 多进程模型。 
查看任务管理器有两个Nginx进程,一个是主进程,另一个是工作进程,主进程的作用:接收客户端发送过来的请求,然后主进程将请求给工作进程,工作进程负责和后端服务器连接。
优点:
- 高效的利用CPU资源其中的某个进程宕机了,可以分配个其他进程
这个进程的数量可以在nginx配置文件中配置,代码如下:
#配置nginx进程数量 worker_processes 1;
工作进程如何与后端服务器建立连接?
是靠事件驱动(消息驱动)建立连接,如图:
优点: 1. 节约资源 2. 提升并发能力
HTTP虚拟主机,配置文件中的sever就是虚拟主机
配置多个虚拟主机,可以代理多个应用。
反向代理
对应用代理就是反向代理。
作用:
- 为了负载均衡后端服务的安全性得到了保障,因为是暴露的nginx代理服务器的地址。
作用:
将访问流量均分到指定的后端服务实例。
配置文件代码,如下
#负载均衡(分流配置)
upstream Demo.Application{
server localhost:5000;
server localhost:5001;
}
负载均衡算法 均分流量
轮询算法 默认算法
缺陷:
请求堆积,会堆积一些请求在性能差的服务器上。
方案:最小活跃数算法
最小活跃数算法【自动识别请求能力 推荐】
条件
least_conn;
配置代码如下:
upstream Demo.Application{
least_conn;
server localhost:5000;
server localhost:5001;
}
原理:
通过请求量来实现的 通过字典工具实现
记录请求量判断请求量根据请求了的累计大小,在动态决定转发到哪个实例
哈希一致性算法
条件:
ip_hash
配置代码如下:
upstream Demo.Application{
ip_hash;
server localhost:5000;
server localhost:5001;
}
优点:
可以解决缓存命中率下降的问题。
如果项目中的数据有缓存,用哈希一致性算法,如果项目数据没有缓存那么就用最小活跃数算法。
原理:
根据IP地址,基于hash算法然后取模得到的。
负载均衡失败重试
条件
max_fails=2 fail_timeout=10s;
代码如下:
upstream Demo.Application{
ip_hash;
server localhost:5000 max_fails=2 fail_timeout=10s;
server localhost:5001 max_fails=2 fail_timeout=10s;
}
如果5000由于某种原因宕机了,会重试2次并且失败超时的时间为10s,之后还是不能处理请求,则会转发到5001实例。
备机
条件
backup
代码如下
upstream Demo.Application{
server localhost:5000 max_fails=2 fail_timeout=10s;
server localhost:5002 backup;
}
如果5000与5001实例由于某种原因都宕机了,那么请求会到 5002 来处理请求。【5002为备用实例,如果5000和5001 没有宕机,则不会访问5002】
一致性哈希算法不支持备机。



