草庐IT

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

java冯坚持 2023-08-26 原文

文章目录

前言

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

}

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

4、讲解

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

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

      worker_processes  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)进行匹配,
    对特定的请求进行处理。 地址定向、数据缓 存和应答控制等功能,
    还有许多第三方模块的配置也在这里进行。
    

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"';
    
  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;  #热备
     }
    
  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;
              }
      
  12. #error_page 404 /404.html;:#错误页

5、http块中location

a、描述

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

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

b、示例

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

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;
            }
        }
    }
    
  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;
    }
    
  3. ip-hash:IP哈希算法,每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

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

    upstream myapp1 {
        server srv1.example.com weight=3;
        server srv2.example.com;
        server srv3.example.com;
    }
    

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

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;
    }
    
  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;
    }
    
  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;
    }
    
  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;
    }
    
  9. esolve 将server指令配置的域名,指定域名解析服务器。需要在http模块下配置resolver指令,指定域名解析服务
    表示example.com域名,由10.0.0.1服务器来负责解析。
    http {
     resolver 10.0.0.1;
     upstream u {
     zone ...;
     ...
     server example.com resolve;
     }
    }
    

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

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

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

有关nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  6. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  7. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  8. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  9. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  10. ruby - 如何使用 Ruby aws/s3 Gem 生成安全 URL 以从 s3 下载文件 - 2

    我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A

随机推荐