ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求(就是处理location /),并生成目录列表。当ngx_http_index_module模块找不到索引文件时,一般会将请求传递给模块ngx_http_autoindex_module。(即ngx_http_index_module模块找不到首页文件,就会交给ngx_http_autoindex_module把当前目录下的全部文件生成目录列表)php
#启用或禁用目录列表输出,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
[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显示的是确切的大小) } }
ngx_http_stub_status_module模块提供对基本状态信息的访问。
默认状况下不构建此模块,应使用--with-http_stub_status_module 配置参数启用它 。css
Syntax: stub_status; Default: — Context: server, location
[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; } }
Active connections # 当前活动客户端链接数,包括Waiting等待链接数。 accepts # 已接受总的TCP链接数。 handled # 已处理总的TCP链接数。 requests # 客户端总的http请求数。 Reading # 当前nginx读取请求头的链接数。 Writing # 当前nginx将响应写回客户端的链接数。 Waiting # 当前等待请求的空闲客户端链接数。 # 注意, 一次TCP的链接,能够发起屡次http的请求, 以下参数可配置进行验证 keepalive_timeout 0; # 相似于关闭长链接 keepalive_timeout 65; # 65s没有活动则断开链接
ngx_http_access_module模块容许限制对某些客户端地址的访问。html
#容许配置语法 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
[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
[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的顺序是有影响的
默认状况下,从第一条规则进行匹配
若是匹配成功,则不继续匹配下面的内容。
若是匹配不成功,则继续往下寻找能匹配成功的内容。node
ngx_http_auth_basic_module模块容许使用HTTP基自己份验证,验证用户名和密码来限制对资源的访问。nginx
#使用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
#可使用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
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; } }
常常会遇到这种状况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力,从而影响业务,针对这种状况咱们能够考虑对同一个ip的链接数,请求数、进行限制。git
ngx_http_limit_conn_module模块用于限制定义key的链接数,特别是来自单个IP地址的链接数。但并不是全部链接都被计算在内,仅当链接已经读取了整个请求头时才计算链接。web
Syntax: limit_conn_zone key zone=name:size; Default: — Context: http Syntax: limit_conn zone number; Default: — Context: http, server, location
# http标签段定义链接限制 http{ limit_conn_zone $binary_remote_addr zone=conn_zone:10m; } server { # 同一时刻只容许一个客户端链接 limit_conn conn_zone 1; location / { root /code; index index.html; }
[root@xuliangwei ~]# yum install -y httpd-tools [root@xuliangwei ~]# ab -n 20 -c 2 http://127.0.0.1/index.html
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"
ngx_http_limit_req_module模块用于限制定义key请求的处理速率,特别单一的IP地址的请求的处理速率。sql
#模块名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
# 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; } }
[root@xuliangwei ~]# yum install -y httpd-tools [root@xuliangwei ~]# ab -n 20 -c 2 http://127.0.0.1/index.html
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的请求限制才是比较优的解决方案。服务器
使用Nginx Location能够控制访问网站的路径, 但一个server容许出现多个location配置, 那多个location出现冲突谁的优先级会更高呢curl
location [=|^~|~|~*|!~|!~*|/] /uri/ { ... }
匹配符 | 匹配规则 | 优先级 |
---|---|---|
= | 精确匹配 | 1 |
^~ | 以某个字符串开头 | 2 |
~ | 区分大小写的正则匹配 | 3 |
~* | 不区分大小写的正则匹配 | 4 |
!~ | 区分大小写不匹配的正则 | 5 |
!~* | 不区分大小写不匹配的正则 | 6 |
/ | 通用匹配,任何请求都会匹配到 | 7 |
[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 ^~"; # } }
# 优先级最高符号= [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 /
# 通用匹配,任何请求都会匹配到 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)$" { ... }