n5.Nginx 常见模块

4.1 Nginx 四层访问控制

访问控制基于模块ngx_http_access_module实现,可以通过匹配客户端源IP地址进行限制

注意: 如果能在防火墙设备控制,最好就不要在nginx上配置,可以更好的节约资源

location = /login/ {
   root /data/nginx/html/pc;
   allow 10.0.0.1;
   deny all;
 }

[root@Ubuntu2204 images]#mkdir /data/nginx/html/pc/login
[root@Ubuntu2204 images]#echo login > /data/nginx/html/pc/login/index.html

[root@Rocky8 ~]#curl http://www.anan.org/login/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

[root@Rocky8 ~]#hostname -I
10.0.0.8 

浏览器(10.0.0.1)----> 访问 http://www.anan.org/login/

在这里插入图片描述

4.2 Nginx 账户认证功能

由 ngx_http_auth_basic_module 模块提供此功能

[root@Ubuntu2204 images]#apt update; apt -y install apache2-utils

# 创建用户
# -b 非交互式方式提交密码
[root@Ubuntu2204 images]# htpasswd -cb /apps/nginx/conf/.htpasswd nan 123456
Adding password for user nan
[root@Ubuntu2204 images]# tail /apps/nginx/conf/.htpasswd
nan:$apr1$KinagyD8$kSVRQlOTrKolJr.3bIvAl/

# 安全加固
[root@Ubuntu2204 images]#chown nginx.nginx /apps/nginx/conf/.htpasswd
[root@Ubuntu2204 images]# chmod 600 /apps/nginx/conf/.htpasswd

 location = /login/ {
   root /data/nginx/html/pc;
   index index.html;
   auth_basic           "login password";
   auth_basic_user_file /apps/nginx/conf/.htpasswd;
 }
 
# 测试 
访问 http://www.anan.org/login/

在这里插入图片描述

4.3 自定义错误页面

自定义错误页,同时也可以用指定的响应状态码进行响应, 可用位置:http, server, location, if in location

# 自定义错误页面
error_page  404 /index.html;                                                                      
    location = /error.html {
    root /data/nginx/html;
    }

[root@Ubuntu2204 ~]#echo error > /data/nginx/html/error.html

# 访问不存在的页面进行测试 (在/etc/hosts文件中做域名解析)
[root@Rocky8 ~]#curl http://www.anan.org/lll.html
error

#  如果404,就转到主页
error_page 404 /index.html;

4.4 自定义错误日志

[root@Ubuntu2204 ~]#mkdir /data/nginx/logs
[root@Ubuntu2204 ~]#ls /data/nginx/

server {
   listen 80;
   server_name www.anan.org;
   charset utf-8;
   server_tokens off;   
   access_log /data/nginx/logs/access.log; 
   error_log /data/nginx/logs/error.log;
}

[root@Ubuntu2204 ~]#cat /data/nginx/logs/access.log

4.5 检测文件是否存在

try_files 会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹), 如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起 一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现 内部500错误。

try_files $uri $uri/index.html $uri.html =489;

# 如果不存在页面 (如果是自定义的状态码则会显示在返回数据的状态码中)
[root@Rocky8 ~]#curl http://www.anan.org/lll.html -I
HTTP/1.1 489 
Server: nginx
Date: Wed, 17 Jul 2024 15:26:43 GMT
Content-Length: 0
Connection: keep-alive

4.6 长连接配置

keepalive_timeout timeout [header_timeout];  #设定保持连接超时时长,0表示禁止长连接,默认为75s,通常配置在http字段作为站点全局配置
keepalive_requests number;  #在一次长连接上所允许请求的资源的最大数量,默认为1000次,建议适当调大,比如:500

4.7 作为下载服务器配置

ngx_http_autoindex_module 模块处理以斜杠字符 “/” 结尾的请求,并生成目录列表,可以做为下载服 务配置使用

autoindex on | off;#自动文件索引功能,默为off

autoindex_exact_size on | off;  #计算文件确切大小(单位bytes),off 显示大概大小(单位K、M),默认on

autoindex_localtime on | off ; #显示本机时间而非GMT(格林威治)时间,默认off

charset charset | off;  #指定字符编码,默认为off,中文会乱码,指定为utf8

autoindex_format html | xml | json | jsonp; #显示索引的页面文件风格,默认html

limit_rate rate; #限制响应客户端传输速率(除GET和HEAD以外的所有方法),单位B/s,即bytes/second,默认值0,表示无限制,此指令由ngx_http_core_module提供

set $limit_rate 4k; #也可以通变量限速,单位B/s,同时设置,此项优级高.
[root@Ubuntu2204 ~]#mkdir /data/nginx/html/pc/download

location /download {
	autoindex on;
	autoindex_exact_size off;
	autoindex_localtime on;
	charset utf-8;
	limit_rate 1024k;
	root /data/nginx/html/pc;
	}

# 可以从清华大学镜像源同步下载实现仓库功能
[root@Ubuntu2204 ~]#rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/ubuntu /data/nginx/html/pc/download/ubuntu

在这里插入图片描述

4.8 作为上传服务器

client_max_body_size 1m; #设置允许客户端上传单个文件的最大值,默认值为1m,上传文件超过此值会出413错误

client_body_buffer_size size; #用于接收每个客户端请求报文的body部分的缓冲区大小;默认16k;超出此大小时,其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置

client_body_temp_path path [level1 [level2 [level3]]];
#设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量,目录名为16进制的数字,使用hash之后的值从后往前截取1位、2位、2位作为目录名

4.9 限流限速

为什么要限速

限制某个用户在一定时间内能够产生的Http请求或者限制某个用户的下载速度。如果超过指定的阈值,则 提示503过载保护

限速相关模块

下载限速:限制用户下载资源的速度
ngx_http_core_module

请求限制:限制用户单位时间内所产生的Http请求数
ngx_http_limit_req_module

连接限制:限制同一时间的连接数,即并发数限制
ngx_http_limit_conn_module

请求频率限速原理

先将请求放置缓存中,然后按指定速度持续处理。当请求速度超过了处理速度会导致缓存被占满,如果 还有没有放入缓存的请求,则会被丢弃。工作原理类似于漏斗

限制下载速度

limit_rate_after 100m;  #下载达到100MB数据后开始限速
limit_rate 100k;        #限速100k

限制请求数

http {
   limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
   ...
   server {
       ...
       location /search/ {
           limit_req zone=one burst=5;
           limit_req_status 500; #默认503,可以指定其它状态码
       }
      
#参数说明
limit_req_zone定义在http块中,$binary_remote_addr表示以客户端IP地址的二进制形式为限流依据的key

Zone定义IP状态及URL访问频率的共享内存区域。zone=keyword标识区域的名字,以及冒号后面跟区域大小。8000个IP地址的状态信息约1MB,例子区域可以存储80000个IP地址。

Rate定义最大请求速率。示例中速率不能超过每秒10个请求。超过此速率的请求放入burst队列做延迟处理

burst表示队列大小,当此队列满后,会中断请求报错

nodelay表示超过请求速率后不延迟处理

限制并发连接数

http {
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
}

limit_conn conn_zone 2; #限制并发2个连接

4.10 Nginx 状态页

基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 – with-http_stub_status_module,否则配置完成之后监测会是提示语法错误

[root@Ubuntu2204 ~]#cat /apps/nginx/conf/conf.d/mirrors.conf
server {
    listen 80;
    server_name mirrors.anan.org;
    root /data/nginx/html/mirrors;
    charset utf-8;
    server_tokens off;
    access_log /data/nginx/logs/mirrors-access.log; 
    error_log /data/nginx/logs/mirrors-error.log;
    
    location /nginx_status {
    stub_status;
    }
} 


#状态页用于输出nginx的基本状态信息:
#输出信息示例:
Active connections: 291
server accepts handled requests
       16630948 16630948 31070465
       上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106

Active connections: #当前处于活动状态的客户端连接数,包括连接等待空闲连接数
=reading+writing+waiting

accepts:#统计总值,Nginx自启动后已经接受的客户端请求连接的总数。

handled:#统计总值,Nginx自启动后已经处理完成的客户端请求连接总数,通常等于accepts,除非有因worker_connections限制等被拒绝的失败连接,即失败连接数=accepts-handled

requests:#统计总值,Nginx自启动后客户端发来的总的请求数。因为长连接的原因此值大于上面的accept数

Reading:#当前状态,正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足

Writing:#当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大

Waiting:#当前状态,正在等待客户端发出请求的空闲连接数,开启keep-alive时,Waiting+reading+writing=active connections

r_connections限制等被拒绝的失败连接,即失败连接数=accepts-handled

requests:#统计总值,Nginx自启动后客户端发来的总的请求数。因为长连接的原因此值大于上面的accept数

Reading:#当前状态,正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足

Writing:#当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大

Waiting:#当前状态,正在等待客户端发出请求的空闲连接数,开启keep-alive时,Waiting+reading+writing=active connections


![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/5e69409600eb4dba92cfaddde1c849f1.png)


  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值