关于nginx:Nginx一

8次阅读

共计 4732 个字符,预计需要花费 12 分钟才能阅读完成。

一、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
        {
            /// <summary>
            /// 获取商品数据
            /// </summary>
            /// <returns></returns>
            [HttpGet]
            public IActionResult Get()
            {List<Product> list = new List<Product>() {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
        {
            /// <summary>
            /// 主键
            /// </summary>
            public Guid Id {get; set;} = Guid.NewGuid();
            /// <summary>
            /// 名称
            /// </summary>
            public string Name {get; set;}
            /// <summary>
            /// 价格
            /// </summary>
            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.0\conf

    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 过程,一个是主过程,另一个是工作过程,主过程的作用:接管客户端发送过去的申请,而后主过程将申请给工作过程,工作过程负责和后端服务器连贯。


长处:1.  高效的利用 CPU 资源
2.  其中的某个过程宕机了,能够调配个其余过程

这个过程的数量能够在 nginx 配置文件中配置,代码如下:```yml
#配置 nginx 过程数量
worker_processes  1;


```
  • 工作过程如何与后端服务器建设连贯?

    是靠事件驱动(音讯驱动)建设连贯,如图:

    长处:

    1. 节约资源
    2. 晋升并发能力
  • HTTP 虚拟主机,配置文件中的 sever 就是虚拟主机

    配置多个虚拟主机,能够代理多个利用。

  • 反向代理

    对利用代理就是反向代理。

    作用:

    1. 为了负载平衡
    2. 后端服务的安全性失去了保障,因为是裸露的 nginx 代理服务器的地址。
五、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】

      一致性哈希算法不反对备机。

正文完
 0