1Nginx目录索引

ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求(就是处理location /),并生成目录列表。当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给模块ngx_http_autoindex_module。(即ngx_http_index_module模块找不到首页文件,就会交给ngx_http_autoindex_module把当前目录下的所有文件生成目录列表)

1.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

1.2.示例配置

[root@web ~]# cat /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name game.oldboy.com;
charset utf-8,gbk; #设定字符集,防止中文字符乱码显示。 location / {
root /code/game;
autoindex on; (需要找不到index.html主页文件,才能以目录树的结构显示)
autoindex_exact_size off; (off显示的是确切的大小)
}
}

2Nginx状态监控

ngx_http_stub_status_module模块提供对基本状态信息的访问。

默认情况下不构建此模块,应使用--with-http_stub_status_module 配置参数启用它 。

2.1.指令

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

2.2.示例配置

[root@web ~]# cat /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name game.oldboy.com;
access_log off; location /nginx_status {
stub_status;
}
}

2.3.此配置创建一个简单的网页,其基本状态数据可能如下所示:

2.4.提供以下状态信息

Active connections  # 当前活动客户端连接数,包括Waiting等待连接数。
accepts # 已接受总的TCP连接数。
handled # 已处理总的TCP连接数。
requests # 客户端总的http请求数。 Reading # 当前nginx读取请求头的连接数。
Writing # 当前nginx将响应写回客户端的连接数。
Waiting # 当前等待请求的空闲客户端连接数。 # 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接

3Nginx访问控制

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

3.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

3.2.示例配置,拒绝指定的IP访问该网站的/nginx_status, 其他IP全部允许访问

[root@web ~]# cat /etc/nginx/conf.d/module.conf
server {
listen 80;
server_name module.bgx.com; location /nginx_status {
stub_status;
deny 10.0.0.1/32; #拒绝指定的地址或地址段
allow all; #允许所有的地址
}
}
[root@web01 conf.d]# curl 10.0.0.7/basic_status
Active connections: 3
server accepts handled requests
6 6 7
Reading: 0 Writing: 1 Waiting: 2

3.3.示例配置,只允许指定的来源IP能访问/nginx_status, 其它网段全部拒绝

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

注意:deny和allow的顺序是有影响的

默认情况下,从第一条规则进行匹配

如果匹配成功,则不继续匹配下面的内容。

如果匹配不成功,则继续往下寻找能匹配成功的内容。

4Nginx资源限制

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

4.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

4.2.指定保存用户名和密码的文件,格式如下:

#可以使用htpasswd程序或"openssl passwd"命令生成对应的密码;
name1:passwd1
name2:passwd2 #使用htpaaswd创建新的密码文件, -c创建新文件 -b允许命令行输入密码
[root@xuliangwei ~]# yum install httpd-tools
[root@xuliangwei ~]# htpasswd -b -c /etc/nginx/auth_conf tuchuang 123456

4.3.示例配置,基于用户名和密码认证实践

server {
listen 80;
server_name module.bgx.com;
access_log off; location /nginx_status {
stub_status;
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file /etc/nginx/auth_conf;
}
}

5Nginx访问限制

经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力,从而影响业务,针对这种情况我们可以考虑对同一个ip的连接数,请求数、进行限制。

5.1.ngx_http_limit_conn_module模块用于限制定义key的连接数

ngx_http_limit_conn_module模块用于限制定义key的连接数,特别是来自单个IP地址的连接数。但并非所有连接都被计算在内,仅当连接已经读取了整个请求头时才计算连接。

5.1.1.指令

Syntax:  limit_conn_zone key zone=name:size;
Default: —
Context: http Syntax: limit_conn zone number;
Default: —
Context: http, server, location

5.1.2.设置共享内存区域和给定键值的最大允许连接数。超过此限制时,服务器将返回错误以回复请求

# http标签段定义连接限制
http{
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
}
server {
# 同一时刻只允许一个客户端连接
limit_conn conn_zone 1; location / {
root /code;
index index.html;
}

5.1.3.使用ab工具进行压力测试

[root@xuliangwei ~]# yum install -y httpd-tools
[root@xuliangwei ~]# ab -n 20 -c 2 http://127.0.0.1/index.html

5.1.4.nginx日志结果

2018/10/24 18:04:49 [error] 28656#28656: *1148 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com"
2018/10/24 18:04:49 [error] 28656#28656: *1155 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com"

5.2.ngx_http_limit_req_module模块用于限制定义key请求的处理速率

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

5.2.1.指令

#模块名ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location

5.2.2.设置共享内存区域和请求的最大突发大小。过多的请求被延迟,直到它们的数量超过最大的限制,在这种情况下请求以错误终止。

# http标签段定义请求限制, rate限制速率,限制一秒钟最多一个IP请求
http {
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
}
server {
listen 80;
server_name module.bgx.com;
# 1r/s只接收一个请求,其余请求拒绝处理并返回错误码给客户端
#limit_req zone=req_zone; # 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
limit_req zone=req_zone burst=3 nodelay;
location / {
root /code;
index index.html;
}
}

5.2.3.使用ab工具进行压力测试

[root@xuliangwei ~]# yum install -y httpd-tools
[root@xuliangwei ~]# ab -n 20 -c 2 http://127.0.0.1/index.html

5.2.4.nginx日志结果

2018/10/24 07:38:53 [error] 81020#0: *8 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"
2018/10/24 07:38:53 [error] 81020#0: *9 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"

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

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

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

6Nginx Location

使用Nginx Location可以控制访问网站的路径, 但一个server允许出现多个location配置, 那多个location出现冲突谁的优先级会更高呢

6.1.Location语法示例

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

6.2.Location语法优先级排列

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

6.3.配置网站验证Location优先级

[root@Nginx conf.d]# cat testserver.conf
server {
listen 80;
server_name module.oldboy.com;
location / {
default_type text/html;
return 200 "location /";
} location =/ {
default_type text/html;
return 200 "location =/";
} location ~ / {
default_type text/html;
return 200 "location ~/";
} # location ^~ / {
# default_type text/html;
# return 200 "location ^~";
# }
}

6.4.测试Location优先级

# 优先级最高符号=
[root@Nginx conf.d]# curl module.oldboy.com
location =/ # 注释掉精确匹配=, 重启Nginx
[root@Nginx ~]# curl module.oldboy.com
location ~/ # 注释掉~, 重启Nginx
[root@Nginx ~]# curl module.oldboy.com
location /

6.5.Locaiton规则配置应用场景

# 通用匹配,任何请求都会匹配到
location / {
...
} # 严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
...
} # 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
...
} # 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* .*\.(jpg|gif|png|js|css)$ {
...
} # 不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
...
}

9、nginx常用基础模块的更多相关文章

  1. Nginx 常用基础模块

    目录 Nginx 常用基础模块 Nginx日志管理 nginx日志切割 Nginx目录索引 Nginx状态监控 Nginx访问控制 Nginx访问限制 Nginx 请求限制配置实战 Nginx Loc ...

  2. 大神教你Nginx常用基础配置方案

    Nginx的fastcgi模块参数设置 Nginx 有两个配置文件fastcgi_params.fastcgi.conf,两者唯一的区别是,fastcgi.conf 多一个参数 SCRIPT_FILE ...

  3. Java高级架构师(一)第33节:Nginx常用核心模块指令

    error_log:错误日志级别 http://www.nginx.cn/doc/  Nginx中文文档 # 并发总数是 worker_processes 和 worker_connections 的 ...

  4. python 模块基础 和常用的模块

    模块的定义 一个模块就是以.py结尾的python 文件,用来从逻辑上组织python代码.注意,模块名和变量名一样开头不能用数字,可以是双下划线和字母. 为什么要用模块? 将一些复杂的需要重复使用的 ...

  5. nginx常用模块(三)

    Nginx常用模块(三) ngx_http_proxy_module模块配置(http或https协议代理) proxy_pass URL; 应用上下文:location, if in locatio ...

  6. Nginx 常用模块

    Nginx 常用模块 1. ngx_http_autoindex_module # ngx_http_autoindex_module模块处理以斜杠字符(' / ')结尾的请求,并生成一个目录列表. ...

  7. nginx基础模块

    http://www.nginx.cn/doc/ 基础模块 HTTP Core模块* HTTP Upstream 模块 HTTP Access 模块 HTTP Auth Basic 模块 HTTP A ...

  8. day63:Linux:nginx基础知识&nginx基础模块

    目录 1.nginx基础知识 1.1 什么是nginx 1.2 nginx应用场景 1.3 nginx组成结构 1.4 nginx安装部署 1.5 nginx目录结构 1.6 nginx配置文件 1. ...

  9. nginx常用模块

    Nginx模块介绍 核心模块:core module 标准模块:stand modules HTTP modules: Standard HTTP modules Optional HTTP modu ...

随机推荐

  1. java1.7新特性:try-with-resources

    转载:https://blog.csdn.net/fanxiaobin577328725/article/details/53067163 首先看代码: import org.junit.Test; ...

  2. linux Centos系统安装Pycharm并创建软链接

    一.环境:Linux|CentOS系统 二.目的:在Linux环境下使用Pycharm(版本2018.3.4) 三.声明:不可用于商业,仅用于交流使用! 1.下载Pycharm压缩包: cd ~ wg ...

  3. React Hook 学习

    1.官方文档 https://react.docschina.org/docs/hooks-intro.html 2.阮一峰 reactHook http://www.ruanyifeng.com/b ...

  4. docker 启动镜像报 WARNING: IPv4 forwarding is disabled. Networking will not work.

    centos7 解决办法: # vi /etc/sysctl.conf 添加如下代码:     net.ipv4.ip_forward=1 重启network服务 # systemctl restar ...

  5. Pandas导入导出&pickle文件模块

    Pandas可以读取与存储的文件格式有很多 像csv,excel,json,html等,详细请看官方文档https://pandas.pydata.org/pandas-docs/stable/use ...

  6. Job的运行过程

    Job的运行过程 Job的具体运行过程 a) 运行mapred程序 b) 本次运行将生成一个Job,于是JobClient向JobTracker(ResourceManager)申请一个JobId以标 ...

  7. Big Box

    #include <stdio.h> #define N 500 int height[N]; int n; int main() { scanf("%d", & ...

  8. python 字符串格式化 format

    用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...

  9. React + Antd开发模式下的Excel导入功能

    具体js如下,配合的是antd里面的upload组件,使用的是xlsx插件 npm :  npm install xlsx 插件链接: https://github.com/SheetJS/sheet ...

  10. 2019牛客暑期多校训练营(第八场)-C CDMA(递归构造)

    题目链接:https://ac.nowcoder.com/acm/contest/888/C 题意:输入m(为2的n次幂,n<=10),构造一个m*m的矩阵满足任意不同的两行的元素乘积和为0. ...