Nginx的常用模块介绍
1. Nginx目录索引
目录索引模块:nginx_http_autoindex_module
nginx_http_autoindex_module 模块处理以斜杠字符('/')结尾的请求,并生成目录列表。
当nginx_http_autoindex_module模块找不到索引文件时,通常会将请求传递给nginx_http_autoindex_module模块。
配置
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
# autoindex常用参数
autoindex_exact_size off;
默认为on, 显示出文件的确切大小,单位是bytes。
修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
修改为on, 显示的文件时间为文件的服务器时间。
charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码。
对下载资源进行限速
Syntax: limit_rate rate;
Default:
limit_rate 0;
Context: http, server, location, if in location
Syntax: limit_rate_after size;
Default:
limit_rate_after 0;
Context: http, server, location, if in location配置示例
vim /etc/nginx/conf.d/module.confserver{
listen 80; #监听端口
server_name module.test.com #域名
charset : utf-8,gbk; #设置编码
location / {
root /code;
index index.html;
}
location /download {
alias /module; #别名,指挥皮匹配
autoindex on; #目录索引
autoindex_exact_size off; #以kB或者MB或者GB的方式显示
autoindex_localtime on; #显示系统时间
}
}Nginx状态监控
nginx_http_stub_status_module模块提供对基本状态信息的访问。
默认情况下不构建这个模块,应使用 --with-http_stub_status_module配置启用
配置
Syntax: stub_status;
Default: —
Context: server, location配置示例
server{
listen 80;
server_name module.test.com;
charset utf8,gbk;
location / {
root /code;
index index.html;
}
location /nginx_status {
stub_status;
}
}在本地windows做好域名解析

输入网址: module.test.com/nginx_status 测试

Active connections # 当前活动的连接数
accepts # 已接收的总TCP连接数量
handled # 已处理的TCP连接数量
requests # 当前http请求数
Reading # 当前读取请求头数量
Writing # 当前响应的请求头数量
Waiting # 等待的请求数,开启了keepalive
# 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接Nginx访问控制
基于IP的访问控制:http_access_module
基于用户登录认证:http_auth_basic_module
基于IP的访问控制
配置
#允许配置语法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
#拒绝配置语法
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except配置示例
server{
listen 80;
server_name module.test.com;
charset utf-8,gbk;
location / {
root /code;
index index.html;
}
location /nginx_status {
stub_status;
deny 10.0.0.7; #限制某个IP不能访问
allow all; #其余的都允许访问
}
}server {
listen 80;
server_name module.oldboy.com;
access_log off;
location /nginx_status {
stub_status;
#只允许10.0.0.0网段和IP为127.0.0.1的主机访问,其余的都禁止
allow 10.0.0.0/24;
allow 127.0.0.1;
deny all;
}
}基于用户登录认证配置访问
配置
#访问提示字符串
Syntax: auth_basic string| off;
Default: auth_basic off;
Context: http, server, location, limit_except
#账户密码文件
Syntax: auth_basic_user_file file;
Default: -
Context: http, server, location, limit_except配置示例
#1.首先需要安装httpd-tools,该包中携带了htpasswd命令
yum install httpd-tools
#2.创建新的密码文件, -c创建新文件 -b允许命令行输入密码
htpasswd -b -c /etc/nginx/auth_conf root root
#第一个root是用户名 第二个root是密码
#配置文件
server{
listen 80;
server_name module.test.com;
charset utf-8,gbk;
location / {
root /code;
index index.html;
}
location /nginx_status {
stub_status;
auth_basic "这是提示信息";
#这里的auth_conf是第一步生成的密码文件
#因为这个文件是生成在/etc/nginx下的,所以基于nginx.conf文件是相对路径,所以直接写文件名即可,当然也可以写绝对路径
auth_basic_user_file auth_conf
}
}
Nginx限速
首先应该使用nginx的文件下载服务器
server {
listen 80;
server_name module.test.com;
charset utf-8,gbk;
localtion / {
root /code;
index index.html index.htm;
}
location /download {
alias /module; #地址,alias意思只匹配到/module
autoindex on; #开启目录索引
autoindex_exact_size off;#使用KB、MB、GB的形式显示文件大小
autoindex_localtime on; #显示本地时间
}
}使用nginx的限速模块
server {
listen 80;
server_name module.test.com;
charset utf-8,gbk;
localtion / {
root /code;
index index.html index.htm;
}
location /download {
alias /module; #地址,alias意思只匹配到/module
autoindex on; #开启目录索引
autoindex_exact_size off;#使用KB、MB、GB的形式显示文件大小
autoindex_localtime on; #显示本地时间
limit_rate_after 2m; #当下载速度达到2MB时进入限速模式
limit_rate 1m; #限速为1mb每秒
}
}Nginx访问限制
在企业中经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,请求数、进行限制。
ngx_http_limit_conn_module 模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数。
limit_conn_module 连接频率限制
limit_req_module 请求频率限制
连接频率限制
Nginx连接限制配置
#模块名ngx_http_limit_conn_module
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
Syntax: limit_conn zone number;
Default: —
Context: http, server, locationNginx连接限制配置示例
http{ #http层,设置
# Limit settings
#zone=conn_zone:10m 意思是定义了一个名字叫conn_zone的空间大小为10m
limit_conn_zone $remote_addr zone=conn_zone:10m;
server{ #server层调用
#连接限制,限制同时最高1个连接
limit_conn conn_zone 1;
}
}请求频率限制
Nginx请求限制配置
#模块名ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
Syntax: limit_req zone number [burst=number] [nodelay];
Default: —
Context: http, server, locationNginx请求限制配置示例
#http标签段定义请求限制, rate限制速率,限制一秒钟最多一个IP请求
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
server{
listen 80;
server_name module.test.com;
charset utf-8,gbk;
# 1r/s只接收一个请求,其余请求拒绝处理并返回错误码503给客户端
limit_req zone=req_zone;
# 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
#limit_req zone=req_zone burst=3 nodelay;
location / {
root /code;
index index.html;
}
location /nginx_status {
stub_status;
}
}
nginx请求限制重定向
在nginx请求限制的过程中,我们可以自定义一个返回值,也就是错误页面的状态码。
默认是503
配置示例
server {
listen 80;
server_name module.oldboy.com;
charset utf-8,gbk;
location / {
root /code;
index index.html index.htm;
limit_req zone=req_zone burst=3 nodelay;
#修改返回状态码为:478
limit_req_status 478
}
}重定向界面
server {
listen 80;
server_name module.oldboy.com;
charset utf-8,gbk;
location / {
root /code;
index index.html index.htm;
limit_req zone=req_zone burst=3 nodelay;
limit_req_status 478
#重定错误页面
error_page 478 /err.html;
}
}
vim /code/err.html
<img style='width:100%;height:100%;' src=https://www.linuxnc.com/478_page.png>Nginx Location
使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分
Location语法示例
location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}匹配符 | 匹配规则 | 优先级 |
|---|---|---|
= | 精确匹配 | 1 |
^~ | 以某个字符串开头 | 2 |
~ | 区分大小写的正则匹配 | 3 |
~* | 不区分大小写的正则匹配 | 4 |
/ | 通用匹配,任何请求都会匹配到 | 5 |
配置nginx测试优先级
server {
listen 80;
server_name location.test.com;
default_type text/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";
}
}测试Location优先级
# 匹配 =
[root@web01 ~]# curl location.test.com
configuration A
#匹配 ^~
[root@web01 ~]# curl location.test.com/images/1.gif
configuration D
# 匹配 ~
[root@web01 ~]# curl location.test.com/documents/
configuration C
#匹配 ~*
[root@web01 ~]# curl location.test.com/1.gif
configuration E
#匹配 /
[root@web01 ~]# curl location.test.com/index.html
configuration B
评论区