• nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇


    前言

    1. 看这边博文时,强烈推荐看上一篇nginx实操博文:nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
    2. 上一篇博文,讲的很详细;但是并没有对配置文件、负载均衡算法做进一步的解析和学习,这一次我就单写一篇作为理论篇。
    3. nginx 版本:nginx-1.22.1

    一、对上一篇博文的分析

    1. 案例 1.1、1.2 都是反向代理的运行
    2. 案例 2 是反向代理+负载均衡的运行
    3. 案例 3 是反向代理+动静分离的运用
    4. 案例 4 是nginx高可用集群的运用

    二、配置文件分析

    1. nginx 官方网址(很详细)

    1. nginx 官网http://nginx.org/en/
    2. nginx 文档http://nginx.org/en/docs/
    3. nginx 官网(中文)http://nginx.p2hp.com/
    4. nginx 文档(中文)http://nginx.p2hp.com/en/docs/index.html
    5. 推荐看英文

    2、配置文件(全)

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

    3、配置文件(去掉注释)

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
    
        keepalive_timeout  65;
    
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    
    • 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

    4、讲解

    a、nginx 配置文件有三部分组成

    1. 第一部分:全局块
      从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以 及配置文件的引入等。
      比如上面第一行配置的

        worker_processes  1;
      
      • 1
    2. 第二部分:events块
      events 块涉及的指令 主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否 允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
      上述例子就表示每个 work process 支持的最大连接数为 1024.
      这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。

    3. 第二部分:http块
      这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。
      需要注意的是http 块也可以包括 http全局块、server 块。

      http全局块
      http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
       
      server 块
      这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,
      该技术的产生是为了 节省互联网服务器硬件成本。
       
      每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。
      而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。
       
      全局 server 块
      最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。
       
      location 块
      一个 server 块可以配置多个 location 块。
      这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),
      对虚拟主机名称 (也可以是IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,
      对特定的请求进行处理。 地址定向、数据缓 存和应答控制等功能,
      还有许多第三方模块的配置也在这里进行。
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19

    b、全局块

    1. #user nobody;#配置用户或者组,默认为nobody nobody。
    2. worker_processes 1;:允许生成的进程数,默认为1;这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是 会受到硬件、软件等设备的制约。
      nginx分为两类:master(管理者)和worker(工作者),这里就是配置工作进程的地方。worker 数和服务器的 cpu 数相等是最为适宜的。设少了会浪费 cpu,设多了会造成 cpu 频繁切换上下文带来的损耗
      在这里插入图片描述
    3. #error_log:2.2中默认注释掉的配置,制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
    4. #pid:2.2中默认注释掉的配置,指定nginx进程运行文件存放地址

    c、events块

    1. worker_connections 204800;:没个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为:worker_processes*worker_connections
    2. accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
    3. multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
    4. #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport

    d、http块

    1. include mime.types;:#文件扩展名与文件类型映射表
    2. default_type application/octet-stream;:#默认文件类型,默认为text/plain
    3. #access_log off; #取消服务日志
    4. #log_format main ...:#自定义格式 为 main
      #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
      #                  '$status $body_bytes_sent "$http_referer" '
      #                  '"$http_user_agent" "$http_x_forwarded_for"';
      
      • 1
      • 2
      • 3
    5. #access_log logs/access.log main;:# main 为日志格式的默认值
    6. sendfile on;:#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    7. #tcp_nopush on;
    8. keepalive_timeout 65;:#连接超时时间,默认为65s,可以在http,server,location块。
    9. upstream块定义服务器组合 myTomcats。在 proxy_pass 指令的后面使用
      upstream myTomcats {
       server 192.168.0.100:8080;
       server 192.168.0.101:8080;
       server example.com:8080 backup;  #热备
       }
      
      • 1
      • 2
      • 3
      • 4
      • 5
    10. #gzip on;
    11. server块
      • keepalive_requests 120; #单连接请求上限次数。
      • listen 80;:#监听端口
      • server_name localhost; :#监听地址
      • #charset koi8-r;
      • #access_log logs/host.access.log main;
      • location块:#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
        • #root path; #根目录
        • #index vv.txt; #设置默认页
        • proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
        • deny 127.0.0.1; #拒绝的ip
        • allow 172.18.5.54; #允许的ip
        location / {
                    root   html;
                    index  index.html index.htm;
                }
        
        • 1
        • 2
        • 3
        • 4
    12. #error_page 404 /404.html;:#错误页

    5、http块中location

    a、描述

    location 中可用的匹配命令有两种:普通字符串和正则表达式。~ 和~* 用于正则表达式,其他前缀和无任何前缀都用于普通字符串。正则表达式会根据匹配顺序,匹配到第一个正则表达式后停止搜索。普通字符串匹配则无视顺序,只会选择最精确的匹配。常用的匹配命令和作用如下:

    命令	|作用
    |-------|------|
    ~|	表示执行一个正则匹配,区分大小写
    ~*|	表示执行一个正则匹配,不区分大小写
    ^~ |	表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配其他。一般用来匹配目录
    = |	进行普通字符精确匹配
    无前缀 |	用于普通字符串
    @ |	定义一个命名的location,使用在内部定向时,例如error_page,try_files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    b、示例

    1. 只匹配“/”。
      location = /{
      
      }
      
      • 1
      • 2
      • 3
    2. 匹配任何请求,所有URI都是以“/”开始;更长字符匹配或正则表达式匹配会优先。
      location / {
      }
      
      • 1
      • 2
    3. 匹配以gif、jpg、jpeg结尾的请求;但是遇到^~由它处理 。
      ocation ~* .(gif|jpg|jpeg)$ {
      }
      
      • 1
      • 2
    4. 以/index/开头的请求,如果链接的状态为404。则会匹配到@index_error。
      location /index/ {
      
      error_page 404 @index_error;
      }
      location @index_error {}
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

    c、更多详细

    nginx.conf 配置文件中 location 代码块详解:https://blog.csdn.net/lch551218/article/details/104256019

    三、负载均衡篇

    1、算法分析(共5个)

    先放官网文档:http://nginx.org/en/docs/http/load_balancing.html

    1. round-robin:轮询算法(默认),每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

      http {
          upstream myapp1 {
              server srv1.example.com;
              server srv2.example.com;
              server srv3.example.com;
          }
      
          server {
              listen 80;
      
              location / {
                  proxy_pass http://myapp1;
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
    2. least-connected:最少连接算法, — next request is assigned to the server with the least number of active connections,

      upstream myapp1 {
          least_conn;
          server srv1.example.com;
          server srv2.example.com;
          server srv3.example.com;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    3. ip-hash:IP哈希算法,每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

      upstream myapp1 {
          ip_hash;
          server srv1.example.com;
          server srv2.example.com;
          server srv3.example.com;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    4. weight,指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

      upstream myapp1 {
          server srv1.example.com weight=3;
          server srv2.example.com;
          server srv3.example.com;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5

      With this configuration, every 5 new requests will be distributed across the application instances as the following: 3 requests will be directed to srv1, one request will go to srv2, and another one — to srv3.
      It is similarly possible to use weights with the least-connected and ip-hash load balancing in the recent versions of nginx.

    5. fair(第三方)
      按后端服务器的响应时间来分配请求,响应时间短的优先分配。

      upstream backend {
      server server1;
      server server2;
      fair;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
    6. url_hash(第三方)
      按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
      例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

      upstream backend {
      server squid1:3128;
      server squid2:3128;
      hash $request_uri;
      hash_method crc32;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

    2、upstream块及参数详解

    1. upstream 块 白话一点就是: 定义的一个 服务器IP 组合,每一个服务器可以单独设置参数。
    2. weight 就不说,上面负载均衡算法中已经说过了
    3. max_fails 默认为1。某台Server允许请求失败的次数,超过最大次数后,在fail_timeout时间内,新的请求将不会分配给这台机器。如果设置为0,Nginx会将这台Server置为永久无效状态,然后将请求发给定义了proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, and memcached_next_upstream指令来处理这次错误的请求。
    4. fail_timeout 默认为10秒。某台Server达到max_fails次失败请求后,在fail_timeout期间内,nginx会认为这台Server暂时不可用,不会将请求分配给它
    5. max_fails和 fail_timeout 案例:192.168.0.100这台机器,如果有3次请求失败,nginx在15秒内,不会将新的请求分配给它。
      upstream tomcats {
       server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;
       server 192.168.0.101:8080 weight=3;
       server 192.168.0.102:8080 weight=1;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
    6. backup 备份机,所有服务器挂了之后才会生效
      在100和101都挂了之前,102为不可用状态,不会将请求分配给它。只有当100和101都挂了,102才会被启用。
      upstream tomcats {
       server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;
       server 192.168.0.101:8080 weight=3;
       server 192.168.0.102:8080 backup;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
    7. down 标识某一台server不可用。可能能通过某些参数动态的激活它吧,要不真没啥用。
      表示101这台Server为无效状态,不会将请求分配给它。
      upstream tomcats {
       server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;
       server 192.168.0.101:8080 down;
       server 192.168.0.102:8080 backup;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
    8. max_conns 限制分配给某台Server处理的最大连接数量,超过这个数量,将不会分配新的连接给它。默认为0,表示不限制。注意:1.5.9之后的版本才有这个配置
      表示最多给100这台Server分配1000个请求,如果这台Server正在处理1000个请求,nginx将不会分配新的请求给到它。假如有一个请求处理完了,还剩下999个请求在处理,这时nginx也会将新的请求分配给它。
      upstream tomcats {
       server 192.168.0.100:8080 max_conns=1000;
      }
      
      • 1
      • 2
      • 3
    9. esolve 将server指令配置的域名,指定域名解析服务器。需要在http模块下配置resolver指令,指定域名解析服务
      表示example.com域名,由10.0.0.1服务器来负责解析。
      http {
       resolver 10.0.0.1;
       upstream u {
       zone ...;
       ...
       server example.com resolve;
       }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

    ==upstream模块server指令的其它参数和详细配置说明,请参考官方文档。 ==

    四、更多内容在官网文档中,有时间在继续输出

    http://nginx.org/en/docs/

  • 相关阅读:
    学习相对论知识备忘
    python--短路运算,把0、空字符串和None看成 False,其他数值和非空字符串都看成 True
    Uniapp自定义动态加载组件(2024.7更新)
    CCF秀湖会议:“第五存储架构”引发关注
    TX Text Control.NET 32.0 For WPF
    Rust中打印语句为什么使用宏实现?
    在springboot工程中修改使用quartz创建的定时任务
    程序包简单解释
    ADS 使用调整器动态调参教程(Tuning)
    使用VBA将多个txt批量转换成excel表并保存
  • 原文地址:https://blog.csdn.net/qq_40036754/article/details/127775066