Nginx常用基础模块

2023-08-01,,

Nginx常用基础模块

目录
Nginx常用基础模块
目录索引模块
配置方式
nginx的状态模块
配置方式
nginx访问控制模块
配置方式
nginx的访问限制模块
请求限制重定向
Nginx连接限制没有请求限制有效?

添加模块信息时,由于location配置的是网站的URL,所以需要确认配置的是哪个URL,比如站点目录或下一级目录或是其他的站点目录

目录索引模块

目录索引模块简述

ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求,并生成目录列表。

当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。

autoindex模块默认是关闭的

就是找不到页面时就以列表(目录)的形式展现网站

nginx默认是不允许列出整个目录浏览下载

配置方式

语法

Syntax:    autoindex on | off;
Default: autoindex off;
Context: http, server, location
#添加ngx_http_autoindex_module模块
vim /etc/nginx/conf.d/game.wj.com.conf
server{
listen 80; server_name game.wj.com;
location / {
root /code/h5_games;
#index index.html; ---- nginx配置文件默认添加这行配置
#添加并打开autoindex模块
autoindex on;
#修改时间为当前系统时间(不使用格林威治时间)
autoindex_localtime on;
#显示文件大小(显示单位)
autoindex_exact_size off;
}
}
#将站点目录下的网站前端页面移走
[root@web02 /code/h5_games]$ mv index.html /tmp/

打开game.wj.com

nginx的状态模块

状态模块需要配置URL

配置方式

[root@web02 /code/h5_games]$ vim /etc/nginx/conf.d/game.wj.com.conf
server{
listen 80;
server_name game.wj.com;
location / {
root /code/h5_games;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
#在站点目录下一级创建/baba目录
location /baba {
#配置并打开状态模块
stub_status;
} }

打开game.wj.com/baba网站

网站状态信息解析

Active connections  # 当前活动的连接数
accepts             # 当前的总连接数TCP
handled             # 成功的连接数TCP
requests            # 总的http请求数 Reading             # 请求
Writing             # 响应
Waiting             # 等待的请求数,开启了keepalive # 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout  0;   # 类似于关闭长连接
keepalive_timeout  65;  # 65s没有活动则断开连接

nginx访问控制模块

配置方式

基于IP的访问控制模块(控制IP)

allow:指定允许访问的IP或者网段

deny:拒绝指定的IP或网段访问

all:全部/所有

#查看访问的IP
tail -f /var/log/nginx/access.log
10.0.0.1 - - [19/Jul/2021:17:17:13 +0800] "GET /js/common.js HTTP/1.1" 404 555 "http://game.wj.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36" "-"
[root@web02 /code/h5_games]$ vim /etc/nginx/conf.d/game.wj.com.conf
server{
listen 80;
server_name game.wj.com;
location / {
root /code/h5_games;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
#指定允许一个IP访问
allow 10.0.0.1;
#指定允许一个网段访问
allow 10.0.0.0/24;
#拒绝所有访问,除开允许的
deny all;
} location /baba {
stub_status;
} }

基于用户登录的访问控制模块(控制用户是否需要登录)

需要使用htpasswd工具

yum install -y httpd-tools

1.创建认证用户登录目录

[root@web02 ~]$ mkdir -p /tmp/nginx/auth

2.创建保存用户名及密码的文件

[root@web02 ~]$ htpasswd -b -c /tmp/nginx/auth/wj_auth wj 123
Adding password for user wj
#保存的密码是被加密后的
[root@web02 ~]$ cat /tmp/nginx/auth/wj_auth
wj:$apr1$WmvsUFWd$dM9KGqiaIFX2jEiu6S84X1 -b:允许命令行输入密码
-c:创建新文件

3.编写配置文件

[root@web02 ~]$ vim /etc/nginx/conf.d/game.wj.com.conf
server{
listen 80;
server_name game.wj.com;
location / {
root /code/h5_games;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny all;
#这一行类似注释,密码输入错误时一般在手机端能够看见,可以随便定义
auth_basic "wo shi ni baba";
#指定存放用户名和密码的文件进行认证
auth_basic_user_file /tmp/nginx/auth/wj_auth;
} location /baba {
stub_status;
} }

打开game.wj.com

输入用户名及密码后

nginx的访问限制模块

防止过多的请求使服务器负载过高,对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,请求数、进行限制

ngx_http_limit_conn_module模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数。

limit_conn_module 连接频率限制

limit_req_module 请求频率限制

ngx_http_limit_conn_module (限制连接数)

1.首先在主配置文件中(公网Nginx中)的HTTP层进行设置

[root@web02 ~]$ vim /etc/nginx/nginx.conf
http{
...
#连接数限制模块 针对远端连接的IP,在磁盘中开辟一个空间 空间名称=conn_zone:空间大小为1mb(此空间用于存放访问的IP)
limit_conn_zone $remote_addr zone=conn_zone:1m
... }

2.在需要的虚拟主机配置文件中的server层或是location层调用

[root@web02 ~]$ vim /etc/nginx/conf.d/game.wj.com.conf
http{
...
location /baba {
stub_status;
#location层调用,允许同时最高2个IP访问
limit_conn conn_zone 2;
}
...
}

ngx_http_limit_req_module (限制请求频率)

1.首先在主配置文件中(公网Nginx中)的HTTP层进行设置

[root@web02 ~]$ vim /etc/nginx/nginx.conf
http{
...
#请求频率限制模块 二进制形式的远端IP 在磁盘中开辟一块空间 空间名称=suibian(随意定义):大小为1mb 限制每秒钟最多只有一个请求
limit_req_zone $binary_remote_addr zone=suibian:1m rate=1r/s
...
}

2.在需要的虚拟主机配置文件中的server层或是location层调用

[root@web02 ~]$ vim /etc/nginx/conf.d/game.wj.com.conf
server{
...
location{
#location层调用 定义的名称 请求数量超过burst定义的数量时,多余的请求返回503 这里定义2,一秒钟有2个请求来时,处理一个,另一个(或者说其他允许的请求将会被延迟处理),而超过2个以外的请求,返回503 nodelay:不延迟,表示即便允许了2个请求可以以来,但是不延迟处理,就只处理一个,其他的全部返回503
limit_req zone=suibian burst=2 nodelay
}
...
}

进入http://game.wj.com/页面后疯狂刷新~~~

PS:我们在调取模块的名称的时候需要注意区分server层和location层,一个server层下面有多个location层时,调取到server层就是调取给了所有location层,总而言之:注意层级关系

nginx不配置站点目录时,默认站点目录在/etc/nginx/html

请求限制重定向

为什么超出请求限制时,转到了503页面呢?

[root@web02 ~]$ vim /etc/nginx/conf.d/game.wj.com.conf
http{
...
server{
...
location / {
limit_req zone=suibian burst=2 nodelay;
#我们在配置请求限制时,系统默认会添加这行配置,让多余请求转入503
limit_req_status 503; ...
} } }

1.修改默认返回状态码

[root@web02 ~]$ vim /etc/nginx/conf.d/game.wj.com.conf
server{
listen 80;
server_name game.wj.com;
limit_req zone=suibian burst=2 nodelay;
#修改返回状态码为478
limit_req_status 478;
...
}

注意:这个状态码必须在400~599之间

进入http://game.wj.com/页面后疯狂刷新~~~

2.重定向报错页面

[root@web02 ~]$ vim /etc/nginx/conf.d/game.wj.com.conf
server{
listen 80;
server_name game.wj.com;
location / {
limit_req zone=suibian burst=2 nodelay;
limit_req_status 478;
#当返回码为478时,定向至/code/h5_games/456.html页面
error_page 478 /code/h5_games/456.html;
}
...
}

进入http://game.wj.com/页面后再次疯狂刷新~~~

真棒~~

Nginx连接限制没有请求限制有效?

http协议的连接与请求,首先HTTP是建立在TCP基础之上,在完成HTTP请求需要先建立TCP三次握手(称为TCP连接),在连接的基础上在完成HTTP的请求。

所以多个HTTP请求可以建立在一次TCP连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效,因为同一时刻只允许一个TCP连接进入,但是同一时刻多个HTTP请求可以通过一个TCP连接进入。所以针对HTTP的请求限制才是比较优的解决方案。

Location语法优先级排列

匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 4
!~ 区分大小写不匹配的正则 5
!~* 不区分大小写不匹配的正则 6
/ 通用匹配,任何请求都会匹配到 7

Nginx常用基础模块的相关教程结束。

《Nginx常用基础模块.doc》

下载本文的Word格式文档,以方便收藏与打印。