nginx 负载均衡1

遇到的问题
大型网站都要面对庞大的用户量,高并发,海量数据等挑战。为了提升系统整体的性能,可以采用垂直扩展和水平扩展两种方式。
垂直扩展:在网站发展早期,可以从单机的角度通过增加硬件处理能力,比如 CPU 处理能力,内存容量,磁盘等方面,实现服务器处理能力的提升。但是,单机是有性能瓶颈的,一旦触及瓶颈,再想提升,付出的成本和代价会极高。这显然不能满足大型分布式系统(网站)所有应对的大流量,高并发,海量数据等挑战。
水平扩展:通过集群来分担大型网站的流量。集群中的应用服务器(节点)通常被设计成无状态,用户可以请求任何一个节点,这些节点共同分担访问压力。水平扩展有两个要点:
应用集群:将同一应用部署到多台机器上,组成处理集群,接收负载均衡设备分发的请求,进行处理,并返回相应数据。
负载均衡:将用户访问请求,通过某种算法,分发到集群中的节点。


什么是负载均衡
负载均衡(Load Balance,简称 LB)是高并发、高可用系统必不可少的关键组件,目标是 尽力将网络流量平均分发到多个服务器上,以提高系统整体的响应速度和可用性。
负载均衡的主要作用如下
高并发:负载均衡通过算法调整负载,尽力均匀的分配应用集群中各节点的工作量,以此提高应用集群的并发处理能力(吞吐量)。
伸缩性:添加或减少服务器数量,然后由负载均衡进行分发控制。这使得应用集群具备伸缩性。
高可用:负载均衡器可以监控候选服务器,当服务器不可用时,自动跳过,将请求分发给可用的服务器。这使得应用集群具备高可用的特性。
安全防护:有些负载均衡软件或硬件提供了安全性功能,如:黑白名单处理、防火墙,防 DDos 攻击等。


负载均衡分为两类:硬件负载均衡、软件负载均衡
硬件负载均衡的 优点:
功能强大:支持全局负载均衡并提供较全面的、复杂的负载均衡算法。
性能强悍:硬件负载均衡由于是在专用处理器上运行,因此吞吐量大,可支持单机百万以上的并发。
安全性高:往往具备防火墙,防 DDos 攻击等安全功能。
硬件负载均衡
硬件负载均衡,一般是在定制处理器上运行的独立负载均衡服务器,价格昂贵,土豪专属。硬件负载均衡的主流产品有:F5 和 A10。
硬件负载均衡的 缺点:
成本昂贵:购买和维护硬件负载均衡的成本都很高。
扩展性差:当访问量突增时,超过限度不能动态扩容。

软件负载均衡
软件负载均衡,应用最广泛,无论大公司还是小公司都会使用。
软件负载均衡从软件层面实现负载均衡,一般可以在任何标准物理设备上运行。
软件负载均衡的 主流产品 有:Nginx、HAProxy、LVS。
LVS 可以作为四层负载均衡器。其负载均衡的性能要优于 Nginx。
HAProxy 可以作为 HTTP 和 TCP 负载均衡器。
Nginx、HAProxy 可以作为四层或七层负载均衡器

实验机器

server端 test3192.168.23.103
server端 test1192.168.23.101
代理端 test2192.168.23.102
win客户端本机window系统

nginx负载均衡

官网
https://nginx.org/en/docs/http/ngx_http_upstream_module.html
首先三台机器都要先安装nginx
[root@test3 ~]# yum install nginx -y
[root@test2 ~]# yum install nginx -y
[root@test1 ~]# yum install nginx -y

搭建实验数据
[root@test3 ~]# systemctl restart nginx
[root@test3 ~]# echo this is test3 > /usr/share/nginx/html/index.html 
	
[root@test1 ~]# echo this is test1 > /usr/share/nginx/html/index.html 
[root@test1 ~]# systemctl restart nginx
[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 server  192.168.23.103;
 server   192.168.23.101;
}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
}

此时访问他就以轮询的方式展出
[root@test2 ~]# curl 127.0.0.1
this is test1
[root@test2 ~]# curl 127.0.0.1
this is test3
[root@test2 ~]# curl 127.0.0.1
this is test1
[root@test2 ~]# curl 127.0.0.1
this is test3


如果test3和test1做了wordpress博客,并且test3和test1中的wordpress配置name_server 都是 www.wordpress.com 那么在代理端加上请求头和http1.1就可以以轮询的方式访问test3和test1的wordpress
[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 server  192.168.23.103;
 server   192.168.23.101;
}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
}
upstream test {
server 192.168.23.103;
server 192.168.23.101;
}
server {
listen 80;
server_name www.wordpress.com;
location / {
proxy_pass http://test;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
}

地址池也可以相互调用,因为upstream都是一样的,那么写一个就可以了,但是前提是俩proxy_pass 要和upstream 后面要保持一致
[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 server  192.168.23.103;
 server   192.168.23.101;
}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
}
server {
listen 80;
server_name www.wordpress.com;
location / {
proxy_pass http://web-pool;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
}
	
如果两台nginx挂了其中一个没有问题
负载均衡默认机制,后端nginx挂了,不会访问,但是后端php-fpm挂了,他还会继续访问
但是php-fpm挂了,那怎么办?php-fpm是连接后端数据库的,会报502bad gateway
但nginx会轮询到502这一台机器上 用户会看到502这个报错,如果看到这个报错那还得了


解决
只要后端挂掉了,我就不能再让你访问这台了,虽然你nginx是正常的,但是也不能让你访问,因为已经无法给用户提供给=服务了
在对应的localion中加上这一个参数,意思是只要遇到500,502,503,504就让他他自动的next,让他访问下一个
    proxy_next_upstream error timeout http_500 http_502 http_503 http_504;

[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 server  192.168.23.103;
 server   192.168.23.101;
}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
}
server {
listen 80;
server_name www.wordpress.com;
location / {
proxy_pass http://web-pool;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
}
用这个的问题是,如果是真有问题的话,你发现不了,用户是没有任何感知的,比如说有9台服务器,挂了5台,用户只会用着非常的卡,建议先注释,有问题无法快速解决的时候才用

nginx负载均衡调度算法

轮询按时间顺序逐一分配到不同的后端服务器(默认) 也叫rr轮询
weight加权轮询,weight值越大,分配到的访问几率越高
ip_hash每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn最少链接数,那个机器链接数少就分发

面试题

1.如何实现负载均衡
2.负载均衡的调度算法

[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 server  192.168.23.103 weight=5;
 server   192.168.23.101;
}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
}
server {
listen 80;
server_name www.wordpress.com;
location / {
proxy_pass http://web-pool;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
}

[root@test2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@test2 ~]# systemctl restart nginx

此时用浏览器访问,就是test3访问5次,test1访问1次
在这里插入图片描述

iphash

[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 ip_hash;
 server  192.168.23.103;
 server   192.168.23.101;
}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
}
server {
listen 80;
server_name www.wordpress.com;
location / {
proxy_pass http://web-pool;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
}

[root@test2 ~]# curl 127.0.0.1
this is test3
[root@test2 ~]# curl 127.0.0.1
this is test3
[root@test2 ~]# curl 127.0.0.1
this is test3
[root@test2 ~]# curl 127.0.0.1
this is test3

只要你第一次访问那个服务器,后面访问的一直是这个
劣势:会导致负载均衡不均衡
优势:可以解决session会话的问题
session就是把自己的用户名和密码保存到客户端,下次在登录的时候,我们回去验证这个保存的东西, 

nginx负载均衡后端服务器的状态

状态概述
down当前的server暂时不参与负载均衡
backup预留的备份服务器
max_fails允许请求失败的次数
fail_timeout经过max_fails失败后, 服务暂停时间
max_conns限制最大的接收连接数

测试down 服务器 不参与请求

[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 server  192.168.23.103 down;
 server   192.168.23.101;
}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
}
server {
listen 80;
server_name www.wordpress.com;
location / {
proxy_pass http://web-pool;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
}
[root@test2 ~]# curl 192.168.23.102
this is test1
[root@test2 ~]# curl 192.168.23.102
this is test1
[root@test2 ~]# curl 192.168.23.102
this is test1

测试backup

[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 server  192.168.23.103;
 server  192.168.23.100;
 server   192.168.23.101 backup;
}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
}
server {
listen 80;
server_name www.wordpress.com;
location / {
proxy_pass http://web-pool;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
}192.168.23.100 192.168.23.103 全部都挂了之后 backup才参与调度‘
[root@test2 ~]# systemctl restart nginx
[root@test2 ~]# curl 192.168.23.102
this is test3
[root@test2 ~]# curl 192.168.23.102
this is test3

此时把test3的nginx停了,在测试
[root@test2 ~]# curl 192.168.23.102
this is test1
[root@test2 ~]# curl 192.168.23.102
this is test1

nginx编译安装

什么情况下会用到编译安装呢
当yum安装或者二进制安装的版本不满足你的需求,或者当yum安装或者二进制安装没有你需要的模块,此时需要用到编译安装,也可以只能安装位置
现在的需求是想加一个nginx负载均衡健康检查模块  nginx_upstream_check_module 
[root@test2 ~]# cat /etc/nginx/conf.d/1.conf 
upstream web-pool {
 server  192.168.23.103:80 max_fails=2 fail_timeout=10s;
 server  192.168.23.101:80 max_fails=2 fail_timeout=10s; 
 check  interval=3000 rise=2 fall=3 timeout=1000 type=http;

}
server {
listen 80;
server_name _;
location / {
  proxy_pass http://web-pool;
} 
location /upstream_check {
   check_status;
}
}
server {
listen 80;
server_name www.wordpress.com;
location / {
proxy_pass http://web-pool;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}

}

[root@test2 ~]# nginx -t
nginx: [emerg] unknown directive "check_status" in /etc/nginx/conf.d/1.conf:24
nginx: configuration file /etc/nginx/nginx.conf test failed

访问  192.168.23.102/upstream_check  ,监控后端服务器的一个工作情况

编译安装,解决一些依赖
yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
下载nginx的源码包和第三方的依赖
wget http://nginx.org/download/nginx-1.22.1.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

解压
unzip maaster.zip

创建一个专门用于存放nginx模块的目录
mkdir /root/nginx_module
[root@test2 ~]# mv nginx_upstream_check_module-master /root/nginx_module/nginx_upstream_check_module

./configure --prefix=/opt/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --add-module=/root/nginx_module/nginx_upstream_check_module/ --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

checking for --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E" ... not found
./configure: error: the invalid value in --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E"

[root@test2 nginx-1.20.1]# yum -y install redhat-rpm-config.noarch
重新编译即可
报
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.

yum install libxslt* -y
yum install libxml* -y
或者
yum -y install libxml2 libxml2-devel  libxslt-devel

报
./configure: error: the Google perftools module requires the Google perftools
library. You can either do not enable the module or install the library.
解决
yum -y install gperftools  

报
./configure: error: perl module ExtUtils::Embed is required
yum -y install perl-devel perl-ExtUtils-Embed

报
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library
yum -y install GeoIP GeoIP-devel GeoIP-data

此时编译完成
  nginx path prefix: "/opt/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/lib64/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/lib/nginx/tmp/client_body"
  nginx http proxy temporary files: "/var/lib/nginx/tmp/proxy"
  nginx http fastcgi temporary files: "/var/lib/nginx/tmp/fastcgi"
  nginx http uwsgi temporary files: "/var/lib/nginx/tmp/uwsgi"
  nginx http scgi temporary files: "/var/lib/nginx/tmp/scgi"



[root@test2 nginx-1.20.1]# make 
make: *** No rule to make target `build', needed by `default'.  Stop.
再次安装依赖
[root@test2 nginx-1.20.1]# yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel
再次执行make && make install 

cd /root/nginx-1.20.1/objs/
指定一下配置文件
[root@test2 objs]# ./nginx -c /etc/nginx/nginx.conf



重启nginx,访问发现报500
日志报
2024/09/06 21:38:09 [error] 38965#38965: *14 http upstream check module can not find any check server, make sure you've added the check servers, client: 192.168.23.1, server: 192.168.23.102, request: "GET /upstream_check HTTP/1.1", host: "192.168.23.102"


在这里插入图片描述

解决

给nginx打补丁(根据nginx版本号选择补丁包)
[root@test2 nginx-1.20.1]# patch -p1 < /root/nginx_module/nginx_upstream_check_module/check_1.20.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h

在重新编译安装一下nginx即可


nginx 常用模块

nginx如何显示这样的界面
在这里插入图片描述

1.配置 index 索引列表
[root@test3 conf.d]# cat aotu.conf 
server {
listen 80;
server_name 192.168.23.102;
location / {
root /code/index;
autoindex on;
}
}
创建代码目录
[root@test3 conf.d]# mkdir /code/index
[root@test3 conf.d]# touch /code/index/1.txt
[root@test3 conf.d]# mkdir /code/index/2jianduan
[root@test3 conf.d]# touch  /code/index/2jianduan/3.txt

重启nginx
[root@test3 conf.d]# systemctl restart nginx


做hosts解析,浏览器访问

在这里插入图片描述

此时创建一个长的文件名,他就会乱码,中文乱码
[root@test3 conf.d]# touch /code/index/06.第81期视频-反向代理优化配置.mp4

解决乱码
[root@test3 conf.d]# cat aotu.conf 
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
location / {
root /code/index;
autoindex on;   #开启目录索引,以列表的方式显示 
}
}

在这里插入图片描述

此时乱码的问题解决了,接下来就是时间,时间不对

在这里插入图片描述

[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
location / {
root /code/index;
autoindex on;
autoindex_localtime on;    #以本地创建时间为准
autoindex_exact_size off;  #大小以KB M G显示
}
}


限速

[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;       下载的时候前2M不限速
limit_rate       50k;       2M之后只允许50k的下载速度,也可以不用上面那个,让他全部都50k下载
}
}

在这里插入图片描述

nginx状态监控模块

http_stub_status nginx默认就有的
[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
}
}

在这里插入图片描述

nginx访问控制

[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
  allow 192.168.23.102;   #多个用逗号隔开
  deny all;
}
}
[root@test3 ~]# systemctl restart nginx

#此时除了102之外剩下的都不能访问了
[root@test3 ~]# curl 192.168.23.103/nginx_stub
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

基于用户登录的认证方式

ngx_http_auth_basic_module

[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
  auth_basic           "welcome my website";    # 一个类似宇 描述信息,没啥用,但必须要有
  auth_basic_user_file conf/htpasswd;           #密码必须是hash值
}
}

手动生成密码
[root@test3 ~]# yum install httpd-tools -y
[root@test3 ~]# htpasswd -b -c /etc/nginx/conf/htpasswd root 123456
Adding password for user root
-c 创建新文件
-b 运行命令行输入密码

重启nginx

此时日志里面的远程用户就可有了  $remote_user

nginx访问限制

在企业中经常遇到这种情况,服务器流量异常,负载过大等等,对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip连接数,请求数,进行限制
ngx_http_limit_conn_module 模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数
limit_conn_module 连接频率限制
limit_req_module请求频率限制

语法:
连接数
Syntax:	limit_conn_zone key zone=name:size;
Default:	—
Context:	http

请求数
Syntax:	limit_conn zone number;
Default:	—
Context:	http, server, location

#在http层设置一个内存空间,连接数限制
[root@test3 ~]# grep -Ew 'http|limit_conn_zone' /etc/nginx/nginx.conf
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
http {
    limit_conn_zone  $remote_addr zone=conn_zone:10M;
      # limit_conn_zone   设置一个内存空间
      # $remote_addr 用于获取nginx访问的ip
      # zone=conn_zone:10M 这个空间大小给他10M,这个空间的名字叫做conn_zone  1M差的不多是10万左右的IP

# 在server 层调用
[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
limit_conn conn_zone 1;                 #同一时间只能有一个连接,一个班40人同一时间只能有一两个能访问到 连接不到的报503,这个可以在日志里面看到
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
  auth_basic           "welcome my website";
  auth_basic_user_file conf/htpasswd;
}
}

# 或者也可以这样写
[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
limit_conn_zone  $remote_addr zone=conn_zone:10M;
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
limit_conn conn_zone 1;
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
  auth_basic           "welcome my website";
  auth_basic_user_file conf/htpasswd;
}
}

# 重启nginx
[root@test3 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@test3 ~]# systemctl restart nginx


# 那这个连接数应该怎么判断呢
[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
limit_conn_zone  $remote_addr zone=conn_zone:10M;
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
limit_conn conn_zone 100;           #一般一个公网下面的连接数 *10
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
  auth_basic           "welcome my website";
  auth_basic_user_file conf/htpasswd;
}
}


# 请求数限制,就是 按f12那个请求
[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
limit_conn_zone  $remote_addr zone=conn_zone:10M;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;    #$binary_remote_addr和$remote_addr一样,只不过比$remote_addr可以多存一点点  rate=1r/s 每秒处理一个请求
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
limit_conn conn_zone 100;
limit_req zone=one burst=5;    #burst=5 缓存,延迟处理个5个,只处理6个请求 5 + 1 ,剩下的 5个延迟处理,就是1s之后在处理
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
  auth_basic           "welcome my website";
  auth_basic_user_file conf/htpasswd;
}
}



#
[root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf 
limit_conn_zone  $remote_addr zone=conn_zone:10M;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
limit_conn conn_zone 100;
limit_req zone=one burst=5 nodelay;   #nodelay 不延迟
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
  auth_basic           "welcome my website";
  auth_basic_user_file conf/htpasswd;
}
}


如果觉得网页报503不好看,可以把他定向到一个页面


[root@test3 code]# cat /etc/nginx/conf.d/aotu.conf 
limit_conn_zone  $remote_addr zone=conn_zone:10M;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
server_name 192.168.23.102;
charset utf-8,gbk;
limit_conn conn_zone 100;
limit_req zone=one burst=5 nodelay;
limit_req_status 404;          #页面自定义返回 404 所有 都报404 这个可以自定义
error_page 404 403 /error.html;     #代码目录下
location / {
root /code/index;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 2M;
limit_rate       50k;
}
location /nginx_stub {
  stub_status;
  auth_basic           "welcome my website";
  auth_basic_user_file conf/htpasswd;
}
}

[root@test3 ~]# cat /code/index/error.html 
<img style='width:100%;higth:100%;' src=/error.png>


location匹配规则

使用nginx location 可以控制访问网站的路径,但一个 sevrer 可以有多个location ,那多个location 的优先级该如何区分呢
通配符匹配规则优先级
=精确匹配1
^~以某个字符串开头2
~区分大小写的正则匹配3
~*不区分大小写的正则匹配4
/通用匹配。任何请求都会匹配5
[root@test3 ~]# cat /etc/nginx/conf.d/test.conf 
server {
listen 192.168.23.103;
default_type test/html;                           #默认给他一个文件,就不用写配置文件了
location = / {
return 200 "configuration A";
}
location  / {                                       #优先级最低,当什么都匹配不到的时候就会匹配到这个
return 200 "configuration B";
}
location  /documents/ {
return 200 "configuration C";
}
location ^~ /images/ {
return 200 "configuration D";
}
location ~* \.(gif|jpg|jpeg)$ {                  
return 200 "configuration E";
}

}

[root@test3 ~]#  nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@test3 ~]# systemctl restart nginx




测试结果
[root@test3 ~]# curl 192.168.23.103
configuration A[root@test3 ~]# curl 192.168.23.103/
configuration A[root@test3 ~]# curl 192.168.23.103/documents/
configuration C[root@test3 ~]# curl 192.168.23.103/imageS/
configuration B[root@test3 ~]# curl 192.168.23.103/images/
configuration D[root@test3 ~]# curl 192.168.23.103/imageS/aaaaaa/gif
configuration B[root@test3 ~]# curl 192.168.23.103/imageS/aaaaaa/gif/
configuration B[root@test3 ~]# curl 192.168.23.103/aaaaaaaaaaaaaabbbbbbbbbbbbbbb
configuration B[root@test3 ~]# curl 192.168.23.103/imageS/aaaaaa/aaa.gif
configuration E

nginx四层负载

四层负载均衡是基于传输层协议来封装的(如tcp/ip),那我们前面使用到的七层是指应用层,他的组装在四层的基础之上,无论四层还是七层都是指os网络模型

四层负载均衡的应用场景
1.四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性:如nginx就算无法保证自己服务高可用,需要依赖lvs或者keepalive
2.如:tcp协议的负载均衡,有写请求是tcp协议的(mysql,ssh),或者说有些请求只需要使用四层进行端口转发就可以了,所以使用四层负载均衡

四层负载总结
1.仅能转发tcp/ip协议、udp协议,通常用来转发端口,如:tcp/22,udp/53
2.四层负载可以用来解决七层负载端口限制问题(七层负载最大使用65535个端口)
3.四层负载可以解决七层负载高可用的问题(多台后端七层负载均衡能同时的使用)
4.四层负载转发效率比七层高得多,但仅支持tcp/ip协议,不支持https和http协议
5.通常大并发场景通常会选择使用七层负载前面增加四层负载
创建四层负载均衡配置文件的目录
vim /etc/nginx/nginx.conf
events {
....
}
include /etc/nginx/conf.c/*.conf    #需要放到http层的外面
http {
.....
}

mkdir /etc/nginx/conf.c

1.安装部署nginx
yum install nginx -y


2.配置nginx四层负载,xshell远程连接192.168.23.102的2222 端口 看看是不是连接到 192.168.23.103:22,端口的四层转发
[root@test3 conf.d]# cat /etc/nginx/conf.c/1.conf 
stream {
upstream web01 {
   server 192.168.23.102:22;
}
server {
listen 2222;
proxy_pass web01;
}
}
[root@test3 conf.d]# ssh 192.168.23.103 -p2222
The authenticity of host '[192.168.23.103]:2222 ([192.168.23.103]:2222)' can't be established.
ECDSA key fingerprint is SHA256:KL0Kxcu2FE2TN12tjh7xHD4F5aj3QCk0ibsZbEd6fOU.
ECDSA key fingerprint is MD5:75:05:fa:a3:0e:e5:da:86:92:12:20:3c:a1:11:24:cc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.23.103]:2222' (ECDSA) to the list of known hosts.
Last login: Sun Oct  6 06:22:44 2024 from 192.168.23.1
[root@test2 ~]# 



# 业务的四层转发
stream {
upstream web01 {
   server 192.168.23.102:80;
}
server {
listen 2222;
proxy_pass web01;
}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/887717.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode讲解篇之239. 滑动窗口最大值

文章目录 题目描述题解思路题解代码题目链接 题目描述 题解思路 我们维护一个长度为k的窗口&#xff0c;然后窗口从数组最左边一直移动到最右边&#xff0c;记录过程中窗口中的最大值&#xff0c;就是答案 我们每次查询长度为k的窗口最大值是什么时间复杂度是O(k)的&#xff0…

黑神话:仙童,数据库自动反射魔法棒

黑神话&#xff1a;仙童&#xff0c;数据库自动反射魔法棒 Golang 通用代码生成器仙童发布了最新版本电音仙女尝鲜版十一及其介绍视频&#xff0c;视频请见&#xff1a;https://www.bilibili.com/video/BV1ET4wecEBk/ 此视频介绍了使用最新版的仙童代码生成器&#xff0c;将 …

使用 Python 遍历文件夹

要解决这个问题&#xff0c;使用 Python 的标准库可以很好地完成。我们要做的是遍历目录树&#xff0c;找到所有的 text 文件&#xff0c;读取内容&#xff0c;处理空行和空格&#xff0c;并将处理后的内容合并到一个新的文件中。 整体思路&#xff1a; 遍历子目录&#xff1…

计算机毕业设计 基于Hadoop的智慧校园数据共享平台的设计与实现 Python 数据分析 可视化大屏 附源码 文档

&#x1f34a;作者&#xff1a;计算机编程-吉哥 &#x1f34a;简介&#xff1a;专业从事JavaWeb程序开发&#xff0c;微信小程序开发&#xff0c;定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事&#xff0c;生活就是快乐的。 &#x1f34a;心愿&#xff1a;点…

国外电商系统开发-运维系统拓扑布局

点击列表中设备字段&#xff0c;然后定位到【拓扑布局】中&#xff0c;可以看到拓扑发生了变化 再回头&#xff0c;您再次添加一个服务器到系统中&#xff0c;并且选择该服务器的连接节点为您刚才创建的“SDN路由器”&#xff0c;保存后&#xff0c;您可以看到这个服务器连接着…

RabbbitMQ篇(环境搭建 - 下载 安装)(持续更新迭代)

目录 一、Windows 1. 下载安装程序 2. 安装配置erlang 3. 安装rabbitMQ 4. 验证 二、Linux 1. 下载rpm包 1.1. 下载Erlang的rpm包 1.2. 下载socat的rpm包 1.3. 下载RabbitMQ的rpm包 2. 安装 2.1. 安装Erlang 2.2. 安装socat 2.3. 安装RabbitMQ 3. 启动RabbitMQ服…

小程序原生-利用setData()对不同类型的数据进行增删改

1. 声明和绑定数据 wxml文件 <view> {{school}} </view> <view>{{obj.name}}</view> <view id"{{id}}" > 绑定属性值 </view> <checkbox checked"{{isChecked}}"/> <!--算数运算--> <view>{{ id …

数理统计(第1章第2节:一些常用的抽样分布)

目录 统计量的概率分布称为“抽样分布” 1. 正态母体的子样平均数的抽样分布 正态分布 2. 卡方分布 3. t分布 4. F分布 5. 例题 6. 总结 统计量的概率分布称为“抽样分布” 1. 正态母体的子样平均数的抽样分布 正态分布 若随机变量X的概率密度为&#xff1a; 则称X服…

Qt开发技巧(九)去掉切换按钮,直接传样式文件,字体设置,QImage超强,巧用Qt的全局对象,信号槽断连,低量数据就用sqlite

继续讲一些Qt开发中的技巧操作&#xff1a; 1.去掉切换按钮 QTabWidget选项卡有个自动生成按钮切换选项卡的机制&#xff0c;有时候不想看到这个烦人的切换按钮&#xff0c;可以设置usesScrollButtons为假&#xff0c;其实QTabWidget的usesScrollButtons属性最终是应用到QTabWi…

重学SpringBoot3-集成Redis(三)之注解缓存策略设置

更多SpringBoot3内容请关注我的专栏&#xff1a;《SpringBoot3》 期待您的点赞&#x1f44d;收藏⭐评论✍ 重学SpringBoot3-集成Redis&#xff08;三&#xff09;之注解缓存策略设置 1. 引入 Redis 依赖2. 配置 RedisCacheManager 及自定义过期策略2.1 示例代码&#xff1a;自定…

Vue - 路由用法

前端路由就是URL中的hash与组件之间的对应关系。Vue Router是Vue的官方路由。 组成&#xff1a; VueRouter&#xff1a;路由器类&#xff0c;根据路由请求在路由视图中动态渲染选中的组件。<router-link>&#xff1a;请求链接组件&#xff0c;浏览器会解析成<a>。…

【易上手快捷开发新框架技术】nicegui组件button用法庖丁解牛深度解读源代码IDE运行和调试通过截图为证

传奇开心果微博文系列 前言一、button 组件基本用法1. 最基本用法示例2. 创建带图标按钮 二、button按钮组件样式定制1. 按钮的尺寸调整2. 改变颜色示例3. 按钮的自定义字体大小4. 圆角形状示例5. 自定义边框6. 添加阴影7. 复合按钮8. 浮动按钮9. 可扩展浮动操作按钮QFAB10. 按…

【MAUI】CommunityToolkit社区工具包介绍

一、为什么需要声明式开发 .NET的MVVM,始于WPF,很古典,它甚至可能是现代前端框架“声明式开发”的鼻祖。声明式开发,之所以出现,是因为命令式开发在UI层和代码层上无法解耦的问题。如下图所示: 1、命令式开发:后台代码需要调用UI层的控件(label.Text),如果更新UI层…

Bellman-Ford算法和SPFA算法

Bellman-Ford算法 能够处理存在负边权的情况。 算法时间复杂度:O(n*m)&#xff0c;n是顶点数&#xff0c;m是边数。 算法实现: 设s为起点&#xff0c;dis[v]即为s到v的最短距离&#xff0c;pre[v]为v前驱。w[j]是边j的长度&#xff0c;且j连接u、v。 dis[s] 0;dis[v] 0x3…

4款专业电脑数据恢复软件,帮你保障数据安全。

电脑里面会出现的数据丢失场景有很多&#xff0c;像硬盘故障、回收站清空、电脑格式化、系统崩溃、病毒入侵等等&#xff1b;如果发现数据丢失后&#xff0c;建议应停止使用电脑&#xff0c;避免新的数据写入覆盖丢失的数据。然后再尝试进行数据找回&#xff0c;如果想自己进行…

UGUI(六大UI根基组件)

Rect Transform 各种参数 是显示pos还是width/height 还是left/top/right/bottom之类巴拉巴拉&#xff0c;各种混合的展示baby&#xff0c;都是看anchor的设置 pivot的设置影响具体数值 至于blueprint mode &#xff0c;就是用了之后框框不变&#xff0c;who wanna do thi…

从WIFI到NB-IoT,探秘智能门锁的高科技接入方式

我是小米,一个喜欢分享技术的29岁程序员。如果你喜欢我的文章,欢迎关注我的微信公众号“软件求生”,获取更多技术干货! Hello大家好!我是小米,一个29岁、活力满满、热爱分享技术的小米!今天,我想和大家聊聊一个与智能家居密切相关的技术话题——智能门锁的接入方式。无…

哪个编程工具让你的工作效率翻倍?

文章目录 哪个编程工具让你的工作效率翻倍&#xff1f;1. 编辑器与 IDE&#xff1a;高效编码的基础1.1 Visual Studio Code提升效率的关键功能&#xff1a; 1.2 JetBrains 系列 IDE提升效率的关键功能&#xff1a; 1.3 Vim提升效率的关键功能&#xff1a; 2. 版本控制工具&…

使用Java调用OpenAI API并解析响应:详细教程

使用Java调用OpenAI API并解析响应&#xff1a;详细教程 在现代应用程序中&#xff0c;API调用是一个非常常见的任务。本文将通过一个完整的示例&#xff0c;讲解如何使用Java调用OpenAI的ChatGPT API&#xff0c;并通过ObjectMapper处理JSON响应。本文的示例不仅适用于OpenAI…

习题5 循环

选择题 1、如下程序的运行结果为 【 正确答案: B】。 A.9 B.8 C.7 D.6 2、C语言的for语句中的表达式可以部分或全部省略&#xff0c;但两个 【 正确答案: C】不能省略。 但当三个表达式均省略后&#xff0c;因缺少判断条件&#xff0…