首页 > 系统相关 >nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇

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

时间:2022-12-30 18:36:52浏览次数:62  
标签:index http 配置文件 server nginx 实操 upstream location


文章目录

  • ​​前言​​
  • ​​一、对上一篇博文的分析​​
  • ​​二、配置文件分析​​
  • ​​1. nginx 官方网址(很详细)​​
  • ​​2、配置文件(全)​​
  • ​​3、配置文件(去掉注释)​​
  • ​​4、讲解​​
  • ​​a、nginx 配置文件有三部分组成​​
  • ​​b、全局块​​
  • ​​c、events块​​
  • ​​d、http块​​
  • ​​5、http块中location​​
  • ​​a、描述​​
  • ​​b、示例​​
  • ​​c、更多详细​​
  • ​​三、负载均衡篇​​
  • ​​1、算法分析(共5个)​​
  • ​​2、upstream块及参数详解​​
  • ​​四、更多内容在官网文档中,有时间在继续输出​​

前言

  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;
  1. ​第二部分:events块​​ events 块涉及的指令 主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否 允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
    上述例子就表示每个 work process 支持的最大连接数为 1024.
    这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
  2. ​第二部分: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"';
  1. ​#access_log logs/access.log main;​​:# main 为日志格式的默认值
  2. ​sendfile on;​​:#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  3. ​#tcp_nopush on;​​:
  4. ​keepalive_timeout 65;​​:#连接超时时间,默认为65s,可以在http,server,location块。
  5. ​upstream块​​:定义服务器组合 myTomcats。在 proxy_pass 指令的后面使用
upstream myTomcats {
server 192.168.0.100:8080;
server 192.168.0.101:8080;
server example.com:8080 backup; #热备
}
  1. ​#gzip on;​​:
  2. ​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. ​#error_page 404 /404.html;​​:#错误页

5、http块中location

a、描述

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

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

b、示例

  1. 只匹配“/”。
location = /{

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

error_page 404 @index_error;
}
location @index_error {
… }

c、更多详细

​​nginx.conf 配置文件中 location 代码块详解:javascript:void(0)​​

三、负载均衡篇

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. ​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. ​ip-hash​​:IP哈希算法,每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
  1. ​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.

  1. fair(第三方)
    按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
server server1;
server server2;
fair;
}
  1. 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;
}
  1. ​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. 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. ​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. ​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/​


标签:index,http,配置文件,server,nginx,实操,upstream,location
From: https://blog.51cto.com/u_15926676/5981137

相关文章

  • websocket 多个nginx转发
    第一个nginxserver{listen6794;root/mnt/dist;location/analyze/{proxy_passhttp://ip:port;......
  • Linux一键部署ELK+Filebeat+Nginx+Redis日志平台自动化脚本
    此脚本是Linux一键部署ELK+Filebeat+Nginx+Redis日志平台自动化脚本,有需要朋友可以参考,脚本内容如下:环境准备操作系统:CentOSLinuxrelease7.8.2003软件版本Elasticsearch:e......
  • Linux搭建ELK+Filebeat+Nginx+Redis分布式日志管理平台
    ELK介绍需求背景业务发展越来越庞大,服务器越来越多各种访问日志、应用日志、错误日志量越来越多,导致运维人员无法很好的去管理日志开发人员排查问题,需要到服务器上查日志,不......
  • Nginx WEB服务器入门简介及安装
    Nginx(WEB服务器)简介Nginx (enginex)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(......
  • Zabbix监控Nginx性能状态
    Zabbix监控Nginx性能状态Nginx在生产环境中的应用越来越广泛,所以需要对nginx的性能状态做一些监控,从而发现故障隐患,Ngnx的监控指标可分为:基本活动指标,错误指标,性能指标。需......
  • Nginx动态添加模块
    说明:有时我们需要在现有的web服务器上增加新的模块实现更为丰富的功能,Nginx算是比较常用的web服务器,但是nginx也不仅仅只可以做web服务器,只要有对应的插件还可用作反向代理......
  • nginx使用stream实现透传
    nginx配置如下:1#Formoreinformationonconfiguration,see2#*OfficialEnglishDocumentation:http://nginx.org/en/docs/3#*OfficialRussianDoc......
  • Docker 安装 Nginx
    Docker安装NginxNginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文......
  • 分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试
    一、安装Nginx和fastdfs-nginx-module安装Nginx请看:​​从零开始学Java-CentOS下安装Nginx​​,其实我只想放这一句话。但想想我还是一步一步写详细吧。1.下载Ngi......
  • Nginx页面不能正常访问排除方法
    nginx页面不能访问 ​nginx页面不能访问​​​检查服务端服务是否启动成功​​​​在服务端使用wget和curl测试下返回的是否正常​​​​浏览器wget或者curl等软件访问不了......