IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Nginx服务之常用基础模块 -> 正文阅读

[系统运维]Nginx服务之常用基础模块


一、Nginx目录索引


ngx_http_autoindex_module 模块处理以斜杠字符(‘/’)结尾的请求,并生成目录列表。
当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给模块。

1. 指令

# 启用或 禁用目录列表输出,on开启,off关闭。
Syntax: 	autoindex on | off;
Default: 	autoindex off;
Context: 	http, server, location

# 指定是否应在目录列表中输出确切的文件大小,on显示字节,off显示大概单位。
Syntax: 	autoindex_exact_size on | off;
Default: 	autoindex_exact_size on;
Context: 	http, server, location


# 指定目录列表中色时间是应以本地时区还是UTC输出。on本地时区,off UTC时区;
Syntax: 	autoindex_localtime on | off;
Default: 	autoindex_localtime off;
Context: 	http, server, location

2. 实操

[root@study ~]# cat  /etc/nginx/conf.d/autoindex.conf
server {
	listen 80;	# 监听80端口
	server_name study.com;	# 访问名称
	charset utf-8,gbk;	
	
	location / {
		root /module;
		autoindex on;				# 开启列表输出
		autoindex_exact_size off;	# 显示大概单位
		autoindex_localtime on;	# 本地时区
	}

}

# 准备对应得目录,
[root@study ~]# mkdir /module/{centos,ubuntu,redhat}/ -p

# 检测nginx语法
[root@study ~]# nginx -t

# 重启nginx服务
[root@study ~]# systemctl restart nginx

3. 效果显示
在这里插入图片描述

二、Nginx状态监控

ngx_http_stub_status_module 模块提供对应基本状态信息的访问。
默认情况下不构建此模块,使用–with-http_stub_status_module 配置参数启用它。

1. 指令

Syntax: 	stub_status;
Default: 	—
Context: 	server, location

2. 实操

[root@study ~]# cat  /etc/nginx/conf.d/autoindex.conf
server {
	listen 80;
	server_name ma.study.com;
	charset utf-8,gbk;
	
	location / {
		root /module;
		autoindex on;
		autoindex_exact_size off;
		autoindex_localtime on;
		stub_status;	# 状态监控
	}
}

3. 显示结果
在这里插入图片描述4. 参数解释

Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 

Active connections	# 当前活动客户端连接数,包括waiting等待连接数
accepts				# 已接收总的TCP连接数
handled				# 已处理总的TCP连接数
requests			# 客户端总的http请求数

Reading				# 当前nginx读取请求头的连接数
Writing				# 当前nginx将响应写回客户端的连接数
Waiting				# 当前等待请求的空闲客户端连接数

# 注意:一次TCP的连接,可以发起多次http的请求,如下参数可配置进行验证
keepalive_timeout 0;	# 类似于关闭长连接
keepalive_timeout 65;	# 65s没有活动则断开连接

三、Nginx访问控制

ngx_http_access_module 模块允许限制对某些客户端地址的访问。

1. 指令

# 允许配置语法
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

2. 实操

[root@study ~]# cat  /etc/nginx/conf.d/module.conf
server {
	listen 80;
	server_name module.study.com;
	
	location /nginx_status {
		stub_status;
		deny 10.0.0.1/32;	# 拒绝指定的地址或地址段
		allow all;			# 允许所有的地址
	}
}

3. 基于来源的IP地址做限制

# 1. 拒绝10.0.0.1 来源的IP访问,其他人允许
location /nginx_status {
		stub_status;
		deny 10.0.0.1/32;	# 拒绝指定的地址或地址段
		allow all;			# 允许所有的地址
	}

# 2. 允许10.0.0.1来源的IP访问,其他人全部拒绝
location /nginx_status {
		stub_status;
		allow 10.0.0.1/32;	
		deny all;			
	}

# 3. 实际配置监控nginx状态时,仅允许该服务器的回环地址访问127.0.0.1
location /nginx_status {
		stub_status;
		allow 127.0.0.1;	
		deny all;			
	}

四、Nginx资源限制

ngx_http_auth_basic_module 模块允许使用HTTP基本身份验证,验证用户名和密码来限制对资源的访问。

1. 指令

#使用HTTP基本身份验证协议启用用户名和密码验证。
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

2. 实操

# 1. 查看yum仓库有没有htpaawd包
[root@study ~]# yum provides htpasswd 	# 查看htpasswd属于哪一个包

# 2. 安装htpasswd包
[root@study ~]# yum install -y httpd-tools

# 3. 生成一个密码文件,密码文件的格式  name:password(加密)
[root@study ~]# htpasswd -c -b /etc/nginx/auth_conf study study123
Adding password for user study

# 4. 查看密码文件
[root@study ~]# cat /etc/nginx/auth_conf 
study:$apr1$vBXH4VUp$FZ5gurt4yRodqCdxiXwRN/

# 5. 配置nginx,限制对应的资源
[root@study ~]# cat /etc/nginx/conf.d/test1.study.com.conf 
server {
	listen 80;
	server_name test1.study.com;

	location /download {
		root /module;
		autoindex on;
		autoindex_exact_size off;
		autoindex_localtime on;

		auth_basic "Please Passwrod!!!";
		auth_basic_user_file /etc/nginx/auth_conf;
	}
}


# 6. 检测nginx 语法
[root@study ~]# nginx -t

# 7. 重启nginx
[root@study ~]# systemctl restart nginx 

3. 最终结果
在这里插入图片描述

五、Nginx访问限制

经常会遇到这种情况,服务器流量异常,负载过大等等,对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,请求数,进行限制;
ngx_http_limit_conn_module 模块用于限制定义key的连接数,特别是来自单个IP地址的连接数。
并非使用连接都被计算在内,仅当连接已经读取了整个请求时才计算连接。

1. 指令

Syntax: 	limit_conn_zone key zone=name:size;
Default: 	—
Context: 	http

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

2. 实操

# 设置共享内存区域和给定键值的最大允许连接数,超过此限制时,服务器将返回错误以回复请求。
# http标签段定义连接限制
# 注意:这里需要使用公网地址,可以测试
[root@study ~]# cat /etc/nginx/conf.d/test1.study.com.conf 
http { 
	limit_conn_zone $binary_remote_addr zone=addr:10m;
}
server {
	listen 80;
	server_name test1.study.com;

	limit_conn conn_zone 1;
	
	location / {
		root /code;
		index index.html;
	}
}

ngx_http_limit_req_module 模块用于限制定义key的请求的处理速率,特别单一的IP地址的请求的处理速率

1. 指令

# 模块名ngx_http_limit_req_module
Syntax: 	limit_req_zone key zone=name:size rate=rate [sync];
Default: 	—
Context: 	http

Syntax: 	limit_req zone=name [burst=number] [nodelay | delay=number];
Default: 	—
Context: 	http, server, location

2. 实操

# 定义限制的key(基于什么来做限制,IP地址)
[root@study conf.d]# cat test1.study.com.conf 
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;

server {
	listen 80;
	server_name test1.study.com;

	limit_req zone=req_zone burst=5 nodelay;
	limit_req_status 412;

	location / {
		root /code/test1;
		index index.html;
	}
}


# 进行简单的压力测试
[root@study conf.d]# ab -n 50 -c 20 http://test1.study.com/index.html


六、Nginx location

使用nginx location可以控制访问网站的路径,但一个server可以有多个location配置,多个location的优先级改如何区分?

1. Location 语法示例

location [=|^~|~|~*|!~|~!*|/] /uri/ {...
}

2. location 语法优先级排序

在这里插入图片描述

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-08-15 15:58:38  更:2021-08-15 15:59:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 21:17:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码