nginx 负载均衡1

[复制链接]
发表于 2026-1-12 14:49:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. 遇到的问题
  2. 大型网站都要面对庞大的用户量,高并发,海量数据等挑战。为了提升系统整体的性能,可以采用垂直扩展和水平扩展两种方式。
  3. 垂直扩展:在网站发展早期,可以从单机的角度通过增加硬件处理能力,比如 CPU 处理能力,内存容量,磁盘等方面,实现服务器处理能力的提升。但是,单机是有性能瓶颈的,一旦触及瓶颈,再想提升,付出的成本和代价会极高。这显然不能满足大型分布式系统(网站)所有应对的大流量,高并发,海量数据等挑战。
  4. 水平扩展:通过集群来分担大型网站的流量。集群中的应用服务器(节点)通常被设计成无状态,用户可以请求任何一个节点,这些节点共同分担访问压力。水平扩展有两个要点:
  5. 应用集群:将同一应用部署到多台机器上,组成处理集群,接收负载均衡设备分发的请求,进行处理,并返回相应数据。
  6. 负载均衡:将用户访问请求,通过某种算法,分发到集群中的节点。
  7. 什么是负载均衡
  8. 负载均衡(Load Balance,简称 LB)是高并发、高可用系统必不可少的关键组件,目标是 尽力将网络流量平均分发到多个服务器上,以提高系统整体的响应速度和可用性。
  9. 负载均衡的主要作用如下
  10. 高并发:负载均衡通过算法调整负载,尽力均匀的分配应用集群中各节点的工作量,以此提高应用集群的并发处理能力(吞吐量)。
  11. 伸缩性:添加或减少服务器数量,然后由负载均衡进行分发控制。这使得应用集群具备伸缩性。
  12. 高可用:负载均衡器可以监控监控候选服务器,当服务器不可用时,自动跳过,将请求分发给可用的服务器。这使得应用集群具备高可用的特性。
  13. 安全防护:有些负载均衡软件或硬件提供了安全性功能,如:黑白名单处理、防火墙,防 DDos 攻击等。
  14. 负载均衡分为两类:硬件负载均衡、软件负载均衡
  15. 硬件负载均衡的 优点:
  16. 功能强大:支持全局负载均衡并提供较全面的、复杂的负载均衡算法。
  17. 性能强悍:硬件负载均衡由于是在专用处理器上运行,因此吞吐量大,可支持单机百万以上的并发。
  18. 安全性高:往往具备防火墙,防 DDos 攻击等安全功能。
  19. 硬件负载均衡
  20. 硬件负载均衡,一般是在定制处理器上运行的独立负载均衡服务器,价格昂贵,土豪专属。硬件负载均衡的主流产品有:F5 和 A10。
  21. 硬件负载均衡的 缺点:
  22. 成本昂贵:购买和维护硬件负载均衡的成本都很高。
  23. 扩展性差:当访问量突增时,超过限度不能动态扩容。
  24. 软件负载均衡
  25. 软件负载均衡,应用最广泛,无论大公司还是小公司都会使用。
  26. 软件负载均衡从软件层面实现负载均衡,一般可以在任何标准物理设备上运行。
  27. 软件负载均衡的 主流产品 有:Nginx、HAProxy、LVS。
  28. LVS 可以作为四层负载均衡器。其负载均衡的性能要优于 Nginx。
  29. HAProxy 可以作为 HTTP 和 TCP 负载均衡器。
  30. Nginx、HAProxy 可以作为四层或七层负载均衡器
复制代码
实验呆板

server端 test3192.168.23.103server端 test1192.168.23.101署理端 test2192.168.23.102win客户端本机window体系nginx负载均衡
  1. 官网
  2. https://nginx.org/en/docs/http/ngx_http_upstream_module.html
  3. 首先三台机器都要先安装nginx
  4. [root@test3 ~]# yum install nginx -y
  5. [root@test2 ~]# yum install nginx -y
  6. [root@test1 ~]# yum install nginx -y
  7. 搭建实验数据
  8. [root@test3 ~]# systemctl restart nginx
  9. [root@test3 ~]# echo this is test3 > /usr/share/nginx/html/index.html
  10.        
  11. [root@test1 ~]# echo this is test1 > /usr/share/nginx/html/index.html
  12. [root@test1 ~]# systemctl restart nginx
  13. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  14. upstream web-pool {
  15. server  192.168.23.103;
  16. server   192.168.23.101;
  17. }
  18. server {
  19. listen 80;
  20. server_name _;
  21. location / {
  22.   proxy_pass http://web-pool;
  23. }
  24. }
  25. 此时访问他就以轮询的方式展出
  26. [root@test2 ~]# curl 127.0.0.1
  27. this is test1
  28. [root@test2 ~]# curl 127.0.0.1
  29. this is test3
  30. [root@test2 ~]# curl 127.0.0.1
  31. this is test1
  32. [root@test2 ~]# curl 127.0.0.1
  33. this is test3
  34. 如果test3和test1做了wordpress博客,并且test3和test1中的wordpress配置name_server 都是 www.wordpress.com 那么在代理端加上请求头和http1.1就可以以轮询的方式访问test3和test1的wordpress
  35. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  36. upstream web-pool {
  37. server  192.168.23.103;
  38. server   192.168.23.101;
  39. }
  40. server {
  41. listen 80;
  42. server_name _;
  43. location / {
  44.   proxy_pass http://web-pool;
  45. }
  46. }
  47. upstream test {
  48. server 192.168.23.103;
  49. server 192.168.23.101;
  50. }
  51. server {
  52. listen 80;
  53. server_name www.wordpress.com;
  54. location / {
  55. proxy_pass http://test;
  56. proxy_set_header Host $http_host;
  57. proxy_set_header X-Real-IP $remote_addr;
  58. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  59. proxy_http_version 1.1;
  60. }
  61. }
  62. 地址池也可以相互调用,因为upstream都是一样的,那么写一个就可以了,但是前提是俩proxy_pass 要和upstream 后面要保持一致
  63. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  64. upstream web-pool {
  65. server  192.168.23.103;
  66. server   192.168.23.101;
  67. }
  68. server {
  69. listen 80;
  70. server_name _;
  71. location / {
  72.   proxy_pass http://web-pool;
  73. }
  74. }
  75. server {
  76. listen 80;
  77. server_name www.wordpress.com;
  78. location / {
  79. proxy_pass http://web-pool;
  80. proxy_set_header Host $http_host;
  81. proxy_set_header X-Real-IP $remote_addr;
  82. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  83. proxy_http_version 1.1;
  84. }
  85. }
  86.        
  87. 如果两台nginx挂了其中一个没有问题
  88. 负载均衡默认机制,后端nginx挂了,不会访问,但是后端php-fpm挂了,他还会继续访问
  89. 但是php-fpm挂了,那怎么办?php-fpm是连接后端数据库的,会报502bad gateway
  90. 但nginx会轮询到502这一台机器上 用户会看到502这个报错,如果看到这个报错那还得了
  91. 解决
  92. 只要后端挂掉了,我就不能再让你访问这台了,虽然你nginx是正常的,但是也不能让你访问,因为已经无法给用户提供给=服务了
  93. 在对应的localion中加上这一个参数,意思是只要遇到500,502,503,504就让他他自动的next,让他访问下一个
  94.     proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  95. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  96. upstream web-pool {
  97. server  192.168.23.103;
  98. server   192.168.23.101;
  99. }
  100. server {
  101. listen 80;
  102. server_name _;
  103. location / {
  104.   proxy_pass http://web-pool;
  105. }
  106. }
  107. server {
  108. listen 80;
  109. server_name www.wordpress.com;
  110. location / {
  111. proxy_pass http://web-pool;
  112. proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  113. proxy_set_header Host $http_host;
  114. proxy_set_header X-Real-IP $remote_addr;
  115. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  116. proxy_http_version 1.1;
  117. }
  118. }
  119. 用这个的问题是,如果是真有问题的话,你发现不了,用户是没有任何感知的,比如说有9台服务器,挂了5台,用户只会用着非常的卡,建议先注释,有问题无法快速解决的时候才用
复制代码
nginx负载均衡调治算法

轮询按时间序次逐一分配到差别的后端服务器(默认)  也叫rr轮询weight加权轮询,weight值越大,分配到的访问几率越高ip_hash每个哀求按访问IP的hash结果分配,如许来自同一IP的固定访问一个后端服务器url_hash按照访问URL的hash结果来分配哀求,是每个URL定向到同一个后端服务器least_conn最少链接数,谁人呆板链接数少就分发口试题
  1. 1.如何实现负载均衡
  2. 2.负载均衡的调度算法
复制代码
  1. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  2. upstream web-pool {
  3. server  192.168.23.103 weight=5;
  4. server   192.168.23.101;
  5. }
  6. server {
  7. listen 80;
  8. server_name _;
  9. location / {
  10.   proxy_pass http://web-pool;
  11. }
  12. }
  13. server {
  14. listen 80;
  15. server_name www.wordpress.com;
  16. location / {
  17. proxy_pass http://web-pool;
  18. proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  19. proxy_set_header Host $http_host;
  20. proxy_set_header X-Real-IP $remote_addr;
  21. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  22. proxy_http_version 1.1;
  23. }
  24. }
  25. [root@test2 ~]# nginx -t
  26. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  27. nginx: configuration file /etc/nginx/nginx.conf test is successful
  28. [root@test2 ~]# systemctl restart nginx
复制代码
此时用欣赏器访问,就是test3访问5次,test1访问1次

iphash
  1. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  2. upstream web-pool {
  3. ip_hash;
  4. server  192.168.23.103;
  5. server   192.168.23.101;
  6. }
  7. server {
  8. listen 80;
  9. server_name _;
  10. location / {
  11.   proxy_pass http://web-pool;
  12. }
  13. }
  14. server {
  15. listen 80;
  16. server_name www.wordpress.com;
  17. location / {
  18. proxy_pass http://web-pool;
  19. proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  20. proxy_set_header Host $http_host;
  21. proxy_set_header X-Real-IP $remote_addr;
  22. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  23. proxy_http_version 1.1;
  24. }
  25. }
  26. [root@test2 ~]# curl 127.0.0.1
  27. this is test3
  28. [root@test2 ~]# curl 127.0.0.1
  29. this is test3
  30. [root@test2 ~]# curl 127.0.0.1
  31. this is test3
  32. [root@test2 ~]# curl 127.0.0.1
  33. this is test3
  34. 只要你第一次访问那个服务器,后面访问的一直是这个
  35. 劣势:会导致负载均衡不均衡
  36. 优势:可以解决session会话的问题
  37. session就是把自己的用户名和密码保存到客户端,下次在登录的时候,我们回去验证这个保存的东西,
复制代码
nginx负载均衡后端服务器的状态

状态概述down当前的server临时不参加负载均衡backup预留的备份服务器max_fails允许哀求失败的次数fail_timeout颠末max_fails失败后, 服务停息时间max_conns限定最大的吸取毗连数测试down 服务器 不参加哀求
  1. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  2. upstream web-pool {
  3. server  192.168.23.103 down;
  4. server   192.168.23.101;
  5. }
  6. server {
  7. listen 80;
  8. server_name _;
  9. location / {
  10.   proxy_pass http://web-pool;
  11. }
  12. }
  13. server {
  14. listen 80;
  15. server_name www.wordpress.com;
  16. location / {
  17. proxy_pass http://web-pool;
  18. proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  19. proxy_set_header Host $http_host;
  20. proxy_set_header X-Real-IP $remote_addr;
  21. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  22. proxy_http_version 1.1;
  23. }
  24. }
  25. [root@test2 ~]# curl 192.168.23.102
  26. this is test1
  27. [root@test2 ~]# curl 192.168.23.102
  28. this is test1
  29. [root@test2 ~]# curl 192.168.23.102
  30. this is test1
复制代码
测试backup
  1. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  2. upstream web-pool {
  3. server  192.168.23.103;
  4. server  192.168.23.100;
  5. server   192.168.23.101 backup;
  6. }
  7. server {
  8. listen 80;
  9. server_name _;
  10. location / {
  11.   proxy_pass http://web-pool;
  12. }
  13. }
  14. server {
  15. listen 80;
  16. server_name www.wordpress.com;
  17. location / {
  18. proxy_pass http://web-pool;
  19. proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  20. proxy_set_header Host $http_host;
  21. proxy_set_header X-Real-IP $remote_addr;
  22. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  23. proxy_http_version 1.1;
  24. }
  25. }
  26. 当 192.168.23.100 192.168.23.103 全部都挂了之后 backup才参与调度‘
  27. [root@test2 ~]# systemctl restart nginx
  28. [root@test2 ~]# curl 192.168.23.102
  29. this is test3
  30. [root@test2 ~]# curl 192.168.23.102
  31. this is test3
  32. 此时把test3的nginx停了,在测试
  33. [root@test2 ~]# curl 192.168.23.102
  34. this is test1
  35. [root@test2 ~]# curl 192.168.23.102
  36. this is test1
复制代码
nginx编译安装
  1. 什么情况下会用到编译安装呢
  2. 当yum安装或者二进制安装的版本不满足你的需求,或者当yum安装或者二进制安装没有你需要的模块,此时需要用到编译安装,也可以只能安装位置
  3. 现在的需求是想加一个nginx负载均衡健康检查模块  nginx_upstream_check_module
  4. [root@test2 ~]# cat /etc/nginx/conf.d/1.conf
  5. upstream web-pool {
  6. server  192.168.23.103:80 max_fails=2 fail_timeout=10s;
  7. server  192.168.23.101:80 max_fails=2 fail_timeout=10s;
  8. check  interval=3000 rise=2 fall=3 timeout=1000 type=http;
  9. }
  10. server {
  11. listen 80;
  12. server_name _;
  13. location / {
  14.   proxy_pass http://web-pool;
  15. }
  16. location /upstream_check {
  17.    check_status;
  18. }
  19. }
  20. server {
  21. listen 80;
  22. server_name www.wordpress.com;
  23. location / {
  24. proxy_pass http://web-pool;
  25. proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  26. proxy_set_header Host $http_host;
  27. proxy_set_header X-Real-IP $remote_addr;
  28. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  29. proxy_http_version 1.1;
  30. }
  31. }
  32. [root@test2 ~]# nginx -t
  33. nginx: [emerg] unknown directive "check_status" in /etc/nginx/conf.d/1.conf:24
  34. nginx: configuration file /etc/nginx/nginx.conf test failed
  35. 访问  192.168.23.102/upstream_check  ,监控监控后端服务器的一个工作情况
  36. 编译安装,解决一些依赖
  37. yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
  38. 下载nginx的源码包和第三方的依赖
  39. wget http://nginx.org/download/nginx-1.22.1.tar.gz
  40. wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
  41. 解压
  42. unzip maaster.zip
  43. 创建一个专门用于存放nginx模块的目录
  44. mkdir /root/nginx_module
  45. [root@test2 ~]# mv nginx_upstream_check_module-master /root/nginx_module/nginx_upstream_check_module
  46. ./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'
  47. checking for --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E" ... not found
  48. ./configure: error: the invalid value in --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E"
  49. [root@test2 nginx-1.20.1]# yum -y install redhat-rpm-config.noarch
  50. 重新编译即可
  51. 报
  52. ./configure: error: the HTTP XSLT module requires the libxml2/libxslt
  53. libraries. You can either do not enable the module or install the libraries.
  54. yum install libxslt* -y
  55. yum install libxml* -y
  56. 或者
  57. yum -y install libxml2 libxml2-devel  libxslt-devel
  58. 报
  59. ./configure: error: the Google perftools module requires the Google perftools
  60. library. You can either do not enable the module or install the library.
  61. 解决
  62. yum -y install gperftools  
  63. 报
  64. ./configure: error: perl module ExtUtils::Embed is required
  65. yum -y install perl-devel perl-ExtUtils-Embed
  66. 报
  67. ./configure: error: the GeoIP module requires the GeoIP library.
  68. You can either do not enable the module or install the library
  69. yum -y install GeoIP GeoIP-devel GeoIP-data
  70. 此时编译完成
  71.   nginx path prefix: "/opt/nginx"
  72.   nginx binary file: "/usr/sbin/nginx"
  73.   nginx modules path: "/usr/lib64/nginx/modules"
  74.   nginx configuration prefix: "/etc/nginx"
  75.   nginx configuration file: "/etc/nginx/nginx.conf"
  76.   nginx pid file: "/run/nginx.pid"
  77.   nginx error log file: "/var/log/nginx/error.log"
  78.   nginx http access log file: "/var/log/nginx/access.log"
  79.   nginx http client request body temporary files: "/var/lib/nginx/tmp/client_body"
  80.   nginx http proxy temporary files: "/var/lib/nginx/tmp/proxy"
  81.   nginx http fastcgi temporary files: "/var/lib/nginx/tmp/fastcgi"
  82.   nginx http uwsgi temporary files: "/var/lib/nginx/tmp/uwsgi"
  83.   nginx http scgi temporary files: "/var/lib/nginx/tmp/scgi"
  84. [root@test2 nginx-1.20.1]# make
  85. make: *** No rule to make target `build', needed by `default'.  Stop.
  86. 再次安装依赖
  87. [root@test2 nginx-1.20.1]# yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel
  88. 再次执行make && make install
  89. cd /root/nginx-1.20.1/objs/
  90. 指定一下配置文件
  91. [root@test2 objs]# ./nginx -c /etc/nginx/nginx.conf
  92. 重启nginx,访问发现报500
  93. 日志日志报
  94. 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"
复制代码

办理
  1. 给nginx打补丁(根据nginx版本号选择补丁包)
  2. [root@test2 nginx-1.20.1]# patch -p1 < /root/nginx_module/nginx_upstream_check_module/check_1.20.1+.patch
  3. patching file src/http/modules/ngx_http_upstream_hash_module.c
  4. patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
  5. patching file src/http/modules/ngx_http_upstream_least_conn_module.c
  6. patching file src/http/ngx_http_upstream_round_robin.c
  7. patching file src/http/ngx_http_upstream_round_robin.h
  8. 在重新编译安装一下nginx即可
复制代码
nginx 常用模块

nginx怎样表现如许的界面
  1. 1.配置 index 索引列表
  2. [root@test3 conf.d]# cat aotu.conf
  3. server {
  4. listen 80;
  5. server_name 192.168.23.102;
  6. location / {
  7. root /code/index;
  8. autoindex on;
  9. }
  10. }
  11. 创建代码目录
  12. [root@test3 conf.d]# mkdir /code/index
  13. [root@test3 conf.d]# touch /code/index/1.txt
  14. [root@test3 conf.d]# mkdir /code/index/2jianduan
  15. [root@test3 conf.d]# touch  /code/index/2jianduan/3.txt
  16. 重启nginx
  17. [root@test3 conf.d]# systemctl restart nginx
  18. 做hosts解析,浏览器访问
复制代码
  1. 此时创建一个长的文件名,他就会乱码,中文乱码
  2. [root@test3 conf.d]# touch /code/index/06.第81期视频-反向代理优化配置.mp4
  3. 解决乱码
  4. [root@test3 conf.d]# cat aotu.conf
  5. server {
  6. listen 80;
  7. server_name 192.168.23.102;
  8. charset utf-8,gbk;
  9. location / {
  10. root /code/index;
  11. autoindex on;   #开启目录索引,以列表的方式显示
  12. }
  13. }
复制代码

此时乱码的标题办理了,接下来就是时间,时间不对

  1. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  2. server {
  3. listen 80;
  4. server_name 192.168.23.102;
  5. charset utf-8,gbk;
  6. location / {
  7. root /code/index;
  8. autoindex on;
  9. autoindex_localtime on;    #以本地创建时间为准
  10. autoindex_exact_size off;  #大小以KB M G显示
  11. }
  12. }
复制代码
限速
  1. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  2. server {
  3. listen 80;
  4. server_name 192.168.23.102;
  5. charset utf-8,gbk;
  6. location / {
  7. root /code/index;
  8. autoindex on;
  9. autoindex_localtime on;
  10. autoindex_exact_size off;
  11. limit_rate_after 2M;       下载的时候前2M不限速
  12. limit_rate       50k;       2M之后只允许50k的下载速度,也可以不用上面那个,让他全部都50k下载
  13. }
  14. }
复制代码

nginx状态监控监控模块
  1. http_stub_status nginx默认就有的
  2. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  3. server {
  4. listen 80;
  5. server_name 192.168.23.102;
  6. charset utf-8,gbk;
  7. location / {
  8. root /code/index;
  9. autoindex on;
  10. autoindex_localtime on;
  11. autoindex_exact_size off;
  12. limit_rate_after 2M;
  13. limit_rate       50k;
  14. }
  15. location /nginx_stub {
  16.   stub_status;
  17. }
  18. }
复制代码

nginx访问控制
  1. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  2. server {
  3. listen 80;
  4. server_name 192.168.23.102;
  5. charset utf-8,gbk;
  6. location / {
  7. root /code/index;
  8. autoindex on;
  9. autoindex_localtime on;
  10. autoindex_exact_size off;
  11. limit_rate_after 2M;
  12. limit_rate       50k;
  13. }
  14. location /nginx_stub {
  15.   stub_status;
  16.   allow 192.168.23.102;   #多个用逗号隔开
  17.   deny all;
  18. }
  19. }
  20. [root@test3 ~]# systemctl restart nginx
  21. #此时除了102之外剩下的都不能访问了
  22. [root@test3 ~]# curl 192.168.23.103/nginx_stub
  23. <html>
  24. <head><title>403 Forbidden</title></head>
  25. <body>
  26. <center><h1>403 Forbidden</h1></center>
  27. <hr><center>nginx/1.20.1</center>
  28. </body>
  29. </html>
复制代码
基于用户登录的认证方式
  1. ngx_http_auth_basic_module
  2. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  3. server {
  4. listen 80;
  5. server_name 192.168.23.102;
  6. charset utf-8,gbk;
  7. location / {
  8. root /code/index;
  9. autoindex on;
  10. autoindex_localtime on;
  11. autoindex_exact_size off;
  12. limit_rate_after 2M;
  13. limit_rate       50k;
  14. }
  15. location /nginx_stub {
  16.   stub_status;
  17.   auth_basic           "welcome my website";    # 一个类似宇 描述信息,没啥用,但必须要有
  18.   auth_basic_user_file conf/htpasswd;           #密码必须是hash值
  19. }
  20. }
  21. 手动生成密码
  22. [root@test3 ~]# yum install httpd-tools -y
  23. [root@test3 ~]# htpasswd -b -c /etc/nginx/conf/htpasswd root 123456
  24. Adding password for user root
  25. -c 创建新文件
  26. -b 运行命令行输入密码
  27. 重启nginx
  28. 此时日志日志里面的远程用户就可有了  $remote_user
复制代码
nginx访问限定

在企业中常常碰到这种环境,服务器流量非常,负载过大等等,对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力,影响业务,每每思量对同一个ip毗连数,哀求数,举行限定
ngx_http_limit_conn_module 模块可以根据界说的key来限定每个键值的毗连数,如同一个IP泉源的毗连数
limit_conn_module 毗连频率限定
limit_req_module哀求频率限定
  1. 语法:
  2. 连接数
  3. Syntax:        limit_conn_zone key zone=name:size;
  4. Default:        —
  5. Context:        http
  6. 请求数
  7. Syntax:        limit_conn zone number;
  8. Default:        —
  9. Context:        http, server, location
  10. #在http层设置一个内存空间,连接数限制
  11. [root@test3 ~]# grep -Ew 'http|limit_conn_zone' /etc/nginx/nginx.conf
  12. #   * Official English Documentation: http://nginx.org/en/docs/
  13. #   * Official Russian Documentation: http://nginx.org/ru/docs/
  14. http {
  15.     limit_conn_zone  $remote_addr zone=conn_zone:10M;
  16.       # limit_conn_zone   设置一个内存空间
  17.       # $remote_addr 用于获取nginx访问的ip
  18.       # zone=conn_zone:10M 这个空间大小给他10M,这个空间的名字叫做conn_zone  1M差的不多是10万左右的IP
  19. # 在server 层调用
  20. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  21. server {
  22. listen 80;
  23. server_name 192.168.23.102;
  24. charset utf-8,gbk;
  25. limit_conn conn_zone 1;                 #同一时间只能有一个连接,一个班40人同一时间只能有一两个能访问到 连接不到的报503,这个可以在日志日志里面看到
  26. location / {
  27. root /code/index;
  28. autoindex on;
  29. autoindex_localtime on;
  30. autoindex_exact_size off;
  31. limit_rate_after 2M;
  32. limit_rate       50k;
  33. }
  34. location /nginx_stub {
  35.   stub_status;
  36.   auth_basic           "welcome my website";
  37.   auth_basic_user_file conf/htpasswd;
  38. }
  39. }
  40. # 或者也可以这样写
  41. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  42. limit_conn_zone  $remote_addr zone=conn_zone:10M;
  43. server {
  44. listen 80;
  45. server_name 192.168.23.102;
  46. charset utf-8,gbk;
  47. limit_conn conn_zone 1;
  48. location / {
  49. root /code/index;
  50. autoindex on;
  51. autoindex_localtime on;
  52. autoindex_exact_size off;
  53. limit_rate_after 2M;
  54. limit_rate       50k;
  55. }
  56. location /nginx_stub {
  57.   stub_status;
  58.   auth_basic           "welcome my website";
  59.   auth_basic_user_file conf/htpasswd;
  60. }
  61. }
  62. # 重启nginx
  63. [root@test3 ~]# nginx -t
  64. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  65. nginx: configuration file /etc/nginx/nginx.conf test is successful
  66. [root@test3 ~]# systemctl restart nginx
  67. # 那这个连接数应该怎么判断呢
  68. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  69. limit_conn_zone  $remote_addr zone=conn_zone:10M;
  70. server {
  71. listen 80;
  72. server_name 192.168.23.102;
  73. charset utf-8,gbk;
  74. limit_conn conn_zone 100;           #一般一个公网下面的连接数 *10
  75. location / {
  76. root /code/index;
  77. autoindex on;
  78. autoindex_localtime on;
  79. autoindex_exact_size off;
  80. limit_rate_after 2M;
  81. limit_rate       50k;
  82. }
  83. location /nginx_stub {
  84.   stub_status;
  85.   auth_basic           "welcome my website";
  86.   auth_basic_user_file conf/htpasswd;
  87. }
  88. }
  89. # 请求数限制,就是 按f12那个请求
  90. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  91. limit_conn_zone  $remote_addr zone=conn_zone:10M;
  92. limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;    #$binary_remote_addr和$remote_addr一样,只不过比$remote_addr可以多存一点点  rate=1r/s 每秒处理一个请求
  93. server {
  94. listen 80;
  95. server_name 192.168.23.102;
  96. charset utf-8,gbk;
  97. limit_conn conn_zone 100;
  98. limit_req zone=one burst=5;    #burst=5 缓存,延迟处理个5个,只处理6个请求 5 + 1 ,剩下的 5个延迟处理,就是1s之后在处理
  99. location / {
  100. root /code/index;
  101. autoindex on;
  102. autoindex_localtime on;
  103. autoindex_exact_size off;
  104. limit_rate_after 2M;
  105. limit_rate       50k;
  106. }
  107. location /nginx_stub {
  108.   stub_status;
  109.   auth_basic           "welcome my website";
  110.   auth_basic_user_file conf/htpasswd;
  111. }
  112. }
  113. #
  114. [root@test3 ~]# cat /etc/nginx/conf.d/aotu.conf
  115. limit_conn_zone  $remote_addr zone=conn_zone:10M;
  116. limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  117. server {
  118. listen 80;
  119. server_name 192.168.23.102;
  120. charset utf-8,gbk;
  121. limit_conn conn_zone 100;
  122. limit_req zone=one burst=5 nodelay;   #nodelay 不延迟
  123. location / {
  124. root /code/index;
  125. autoindex on;
  126. autoindex_localtime on;
  127. autoindex_exact_size off;
  128. limit_rate_after 2M;
  129. limit_rate       50k;
  130. }
  131. location /nginx_stub {
  132.   stub_status;
  133.   auth_basic           "welcome my website";
  134.   auth_basic_user_file conf/htpasswd;
  135. }
  136. }
复制代码
如果以为网页报503不悦目,可以把他定向到一个页面
  1. [root@test3 code]# cat /etc/nginx/conf.d/aotu.conf
  2. limit_conn_zone  $remote_addr zone=conn_zone:10M;
  3. limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  4. server {
  5. listen 80;
  6. server_name 192.168.23.102;
  7. charset utf-8,gbk;
  8. limit_conn conn_zone 100;
  9. limit_req zone=one burst=5 nodelay;
  10. limit_req_status 404;          #页面自定义返回 404 所有 都报404 这个可以自定义
  11. error_page 404 403 /error.html;     #代码目录下
  12. location / {
  13. root /code/index;
  14. autoindex on;
  15. autoindex_localtime on;
  16. autoindex_exact_size off;
  17. limit_rate_after 2M;
  18. limit_rate       50k;
  19. }
  20. location /nginx_stub {
  21.   stub_status;
  22.   auth_basic           "welcome my website";
  23.   auth_basic_user_file conf/htpasswd;
  24. }
  25. }
  26. [root@test3 ~]# cat /code/index/error.html
  27. <img style='width:100%;higth:100%;' src=/error.png>
复制代码
location匹配规则
  1. 使用nginx location 可以控制访问网站的路径,但一个 sevrer 可以有多个location ,那多个location 的优先级该如何区分呢
复制代码
通配符匹配规则优先级=正确匹配1^~以某个字符串开头2~区分巨细写的正则匹配3~*不区分巨细写的正则匹配4/通用匹配。任何哀求都会匹配5
  1. [root@test3 ~]# cat /etc/nginx/conf.d/test.conf
  2. server {
  3. listen 192.168.23.103;
  4. default_type test/html;                           #默认给他一个文件,就不用写配置文件了
  5. location = / {
  6. return 200 "configuration A";
  7. }
  8. location  / {                                       #优先级最低,当什么都匹配不到的时候就会匹配到这个
  9. return 200 "configuration B";
  10. }
  11. location  /documents/ {
  12. return 200 "configuration C";
  13. }
  14. location ^~ /images/ {
  15. return 200 "configuration D";
  16. }
  17. location ~* \.(gif|jpg|jpeg)$ {                  
  18. return 200 "configuration E";
  19. }
  20. }
  21. [root@test3 ~]#  nginx -t
  22. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  23. nginx: configuration file /etc/nginx/nginx.conf test is successful
  24. [root@test3 ~]# systemctl restart nginx
  25. 测试结果
  26. [root@test3 ~]# curl 192.168.23.103
  27. configuration A[root@test3 ~]# curl 192.168.23.103/
  28. configuration A[root@test3 ~]# curl 192.168.23.103/documents/
  29. configuration C[root@test3 ~]# curl 192.168.23.103/imageS/
  30. configuration B[root@test3 ~]# curl 192.168.23.103/images/
  31. configuration D[root@test3 ~]# curl 192.168.23.103/imageS/aaaaaa/gif
  32. configuration B[root@test3 ~]# curl 192.168.23.103/imageS/aaaaaa/gif/
  33. configuration B[root@test3 ~]# curl 192.168.23.103/aaaaaaaaaaaaaabbbbbbbbbbbbbbb
  34. configuration B[root@test3 ~]# curl 192.168.23.103/imageS/aaaaaa/aaa.gif
  35. configuration E
复制代码
nginx四层负载
  1. 四层负载均衡是基于传输层协议来封装的(如tcp/ip),那我们前面使用到的七层是指应用层,他的组装在四层的基础之上,无论四层还是七层都是指os网络模型
  2. 四层负载均衡的应用场景
  3. 1.四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性:如nginx就算无法保证自己服务高可用,需要依赖lvs或者keepalive
  4. 2.如:tcp协议的负载均衡,有写请求是tcp协议的(mysql,ssh),或者说有些请求只需要使用四层进行端口转发就可以了,所以使用四层负载均衡
  5. 四层负载总结
  6. 1.仅能转发tcp/ip协议、udp协议,通常用来转发端口,如:tcp/22,udp/53
  7. 2.四层负载可以用来解决七层负载端口限制问题(七层负载最大使用65535个端口)
  8. 3.四层负载可以解决七层负载高可用的问题(多台后端七层负载均衡能同时的使用)
  9. 4.四层负载转发效率比七层高得多,但仅支持tcp/ip协议,不支持https和http协议
  10. 5.通常大并发场景通常会选择使用七层负载前面增加四层负载
复制代码
  1. 创建四层负载均衡配置文件的目录
  2. vim /etc/nginx/nginx.conf
  3. events {
  4. ....
  5. }
  6. include /etc/nginx/conf.c/*.conf    #需要放到http层的外面
  7. http {
  8. .....
  9. }
  10. mkdir /etc/nginx/conf.c
  11. 1.安装部署nginx
  12. yum install nginx -y
  13. 2.配置nginx四层负载,xshell远程连接192.168.23.102的2222 端口 看看是不是连接到 192.168.23.103:22,端口的四层转发
  14. [root@test3 conf.d]# cat /etc/nginx/conf.c/1.conf
  15. stream {
  16. upstream web01 {
  17.    server 192.168.23.102:22;
  18. }
  19. server {
  20. listen 2222;
  21. proxy_pass web01;
  22. }
  23. }
  24. [root@test3 conf.d]# ssh 192.168.23.103 -p2222
  25. The authenticity of host '[192.168.23.103]:2222 ([192.168.23.103]:2222)' can't be established.
  26. ECDSA key fingerprint is SHA256:KL0Kxcu2FE2TN12tjh7xHD4F5aj3QCk0ibsZbEd6fOU.
  27. ECDSA key fingerprint is MD5:75:05:fa:a3:0e:e5:da:86:92:12:20:3c:a1:11:24:cc.
  28. Are you sure you want to continue connecting (yes/no)? yes
  29. Warning: Permanently added '[192.168.23.103]:2222' (ECDSA) to the list of known hosts.
  30. Last login: Sun Oct  6 06:22:44 2024 from 192.168.23.1
  31. [root@test2 ~]#
  32. # 业务的四层转发
  33. stream {
  34. upstream web01 {
  35.    server 192.168.23.102:80;
  36. }
  37. server {
  38. listen 2222;
  39. proxy_pass web01;
  40. }
  41. }
  42. 远程连接mysql
  43. [root@registry conf]# cat /etc/nginx/conf.d/ceshi.conf
  44. stream {
  45. upstream web01 {
  46.    server 192.168.23.103:3306;
  47. }
  48. server {
  49. listen 2222;
  50. proxy_pass web01;
  51. }
  52. }
  53. [root@registry conf]# mysql -uroot  -P2222
  54. Welcome to the MariaDB monitor.  Commands end with ; or \g.
  55. Your MariaDB connection id is 6
  56. Server version: 5.5.68-MariaDB MariaDB Server
  57. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  58. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  59. MariaDB [(none)]> \q
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表