【10.31】nginx 访问控制
1.28-1.36 nginx 访问控制
1、Nginx访问控制 —— deny、allow
Nginx 的 deny 和 allow 指令是由 ngx_http_access_module 模块提供,Nginx 安装默认内置了该模块。
除非在安装时有指定 --without-http_access_module
- 语法: allow/deny address | CIDR | unix: | all
它表示,允许/拒绝某个 ip 或者一个 ip 段访问。
如果指定 unix:,那将允许 socket 的访问。
注意: unix 在是1.5.1中新加入的功能。
在 nginx 中,allow 和 deny 的规则是按顺序执行的。
- 示例
示例1:
location /
{
allow 192.168.0.0/24;
allow 127.0.0.1;
deny all;
}
说明: 这段配置值允许 192.168.0.0/24 网段和 127.0.0.1 的请求,其他来源 IP 全部拒绝。
访问测试:
[root@alexis-01 ~]# curl -x192.168.194.128:80 www.1.com/123 -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Sun, 03 Nov 2019 12:06:48 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123 -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:06:58 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
示例2:
location ~ "admin"
{
allow 192.168.194.0/24;
deny all;
}
说明: 访问的 uri 中包含 admin 的请求,只允许112.249.97.112 这个 IP 的请求。
访问测试:
[root@alexis-01 ~]# curl -x192.168.194.128:80 www.1.com/123/admin -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:11:13 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123/admin -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:11:25 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
2、基于 location 的访问控制
在生产环境中,我们会对某些特殊的请求进行限制,比如对网站的后台进行限制访问。
这就用到了location配置。
示例1:
location /alexis/
{
deny all;
}
说明: 针对 /alexis/ 目录,全部禁止访问,这里的 deny all 可以改为 return 403.
示例2:
location ~ ".bak|\.htp"
{
return 403;
}
说明: 访问的 uri 中包含 .bak 字样的或者包含 .htp 的直接返回 403 状态码
.bak 的 . 表示任意字符,.htp 表示包含 .htp 字符的。
访问测试:
- www.1.com/123.bak
- www.1.com/alexis/123/.htpalskdjf
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123.bak -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 11:56:00 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123.1bak -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 11:56:08 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123.b1ak -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 11:57:44 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123/.htpalsd -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 11:56:39 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123/.htdpalsd -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 11:56:50 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
示例3:
location ~ (data|cache|tmp|image|attachment).*\.php$
{
deny all;
}
说明: 请求的 uri 中包含 data、cache、tmp、image、attachment 并且以 .php 结尾的,全部禁止访问。
如果网站目可写,那么久不应该支持解析 php。
访问测试:
- www.1.com/alexis/cache/1.php
- www.1.com/image/123.phps
- www.1.com/alexis/datas/1.php
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/alexis/cache/1.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:15:56 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/image/123.phps -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:16:14 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/alexis/datas/1.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:16:25 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/alexis/dasta/1.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:16:39 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
3、Nginx 基于 $document_uri 的访问控制
这就用到了变量 $document_uri,根据前面所学内容,该变量等价于 $uri,其实也等价于 location 匹配。
示例1:
if ($document_uri ~ "/admin/")
{
return 403;
}
说明: 当请求的uri中包含 /admin/ 时,直接返回 403.
if 结构中不支持使用 allow 和 deny。
访问测试:
1、www.1.com/123/admin/1.php 匹配
2、www.1.com/admin123/1.php 不匹配
3、www.1.com/admin.php 不匹配
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123/admin/1.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:21:02 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/admin123/1.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:21:14 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/admin.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:21:40 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
示例2:
if ($document_uri = /admin.php)
{
return 403;
}
说明:请求的uri为 /admin.php 时返回403状态码。
访问测试:
1、www.1.com/admin.php 匹配
2、www.1.com/123/admin.php 不匹配
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/admin.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:23:00 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/123/admin.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:23:08 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/admin.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:26:11 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
示例3:
if ($document_uri ~ '/data/|/cache/.*\.php$')
{
return 403;
}
说明: 请求的 uri 包含 data 或者 cache 目录且以 php 结尾时,返回 403 状态码。
访问测试:
1、www.1.com/data/123.php 匹配
2、www.1.com/cache1/123.php 不匹配
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/data/123.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:29:17 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/cache/admin.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:29:26 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/cache1/123.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:29:42 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
4、nginx 基于 $request_uri 访问控制
$request_uri 比 $docuemnt_uri 多了请求的参数。
主要是针对请求的 uri 中的参数进行控制。
示例:
if ($request_uri ~ "gid=\d{9,12}")
{
return 403;
}
说明: \d{9,12} 是正则表达式,表示只要有连续 9 到 12 位数字,哪怕多出,例如 gid=1234567890 就符和要求。
访问测试:
1、 www.1.com/index.php?gid=1234567890 匹配
2、www.1.com/gid=123 不匹配
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/da2ta/1.php?gid=1234567890 -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:41:26 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/da2ta/1.php?gid=123a4567890 -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 12:41:36 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
背景知识:
曾经有一个客户的网站 cc 攻击,对方发起太多类似这样的请求:/read-123405150-1-1.html
实际上,这样的请求并不是正常的请求,网站会抛出一个页面,提示帖子不存在。
所以,可以直接针对这样的请求,return 403 状态码。
5、Nginx 基于 $user_agent 的访问控制
user_agent 大家并不陌生,可以简单理解成浏览器标识,包括一些蜘蛛爬虫都可以通过user_agent来辨识。
通过观察访问日志,可以发现一些搜索引擎的蜘蛛对网站访问特别频繁,它们并不友好。
为了减少服务器的压力,其实可以把除主流搜索引擎蜘蛛外的其他蜘蛛爬虫全部封掉。
另外,一些cc攻击,我们也可以通过观察它们的user_agent找到规律。
示例:
if ($http_user_agent ~ 'YisouSpider|MJ12bot/v1.4.2|YoudaoBot|Tomato')
{
return 403;
}
说明:user_agent包含以上关键词的请求,全部返回403状态码。
访问测试:
1、curl -A “123YisouSpider1.0”
2、curl -A “MJ12bot/v1.4.1”
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/index.php -A'Tomato' -I
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1
Date: Thu, 31 Dec 2019 13:55:15 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@alexis-01 ~]# curl -x127.0.0.1:80 www.1.com/index.php -A'Tomasto' -I
HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Sun, 03 Nov 2019 13:55:25 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
6、Nginx 基于 $http_referer 的访问控制
在前面讲解 rewrite 时,曾经用过该变量,当时实现了防盗链功能。
其实基于该变量,我们也可以做一些特殊的需求。
示例:
背景: 网站被黑挂马,搜索引擎收录的网页是有问题的,当通过搜索引擎点击到网站时,却显示一个博彩网站。
由于查找木马需要时间,不能马上解决,为了不影响用户体验,可以针对此类请求做一个特殊操作。
比如,可以把从百度访问的链接直接返回 404 状态码,或者返回一段 html 代码。
if ($http_referer ~ 'baidu.com')
{
return 407;
}
或者
if ($http_referer ~ 'baidu.com')
{
return 200 "<html><script>window.location.href='//$host$request_uri';</script></html>";
}
访问测试:
[root@alexis-01 ~]# curl -x127.0.0.1:80 -e 'http://www.baidu.com/asdfsdf' www.1.com/index.php -I
HTTP/1.1 407
Server: nginx/1.16.1
Date: Sun, 03 Nov 2019 14:25:51 GMT
Content-Length: 0
Connection: keep-alive
7、Nginx 的限速
可以通过 ngx_http_limit_conn_module 和 ngx_http_limit_req_module 模块来实现限速的功能。
该模块主要限制下载速度。
1、并发限制
配置示例,在 nginx.conf 中的 http 中进行配置:
http
{
...
limit_conn_zone $binary_remote_addr zone=alexis:10m;
limit_conn_status 503; //被限制的错误状态码
limit_conn_log_level error; //错误日志级别为 error
...
}
虚拟主机配置文件中配置以下内容:
server
{
...
limit_conn alexis 10;
...
}
ab 压力测试工具测试
设置访问 5 次,并发 5 次
[root@alexis-01 ~]# ab -n 5 -c 5 http://www.1.com/php-7.3.0.tar.bz2
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.1.com (be patient).....done
Server Software: nginx/1.16.1
Server Hostname: www.1.com
Server Port: 80
Document Path: /php-7.3.0.tar.bz2
Document Length: 197 bytes
Concurrency Level: 5
Time taken for tests: 0.043 seconds
Complete requests: 5
Failed requests: 2
(Connect: 0, Receive: 0, Length: 2, Exceptions: 0)
Write errors: 0
Non-2xx responses: 3
Total transferred: 29575391 bytes
HTML transferred: 29574363 bytes
Requests per second: 116.14 [#/sec] (mean)
Time per request: 43.053 [ms] (mean)
Time per request: 8.611 [ms] (mean, across all concurrent requests)
Transfer rate: 670852.62 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 0
Processing: 4 17 18.4 17 43
Waiting: 4 6 3.5 4 12
Total: 4 17 18.4 18 43
Percentage of the requests served within a certain time (ms)
50% 4
66% 31
75% 31
80% 43
90% 43
95% 43
98% 43
99% 43
100% 43 (longest request)
从日志来看,确实被限制了并发连接数,只允许 2 并发
[root@alexis-01 ~]# cat /tmp/1.log
127.0.0.1 - - [01/Nov/2019:22:56:01 +0800] "GET /php-7.3.0.tar.bz2 HTTP/1.0" 503 197 "-" "ApacheBench/2.3"
127.0.0.1 - - [01/Nov/2019:22:56:01 +0800] "GET /php-7.3.0.tar.bz2 HTTP/1.0" 503 197 "-" "ApacheBench/2.3"
127.0.0.1 - - [01/Nov/2019:22:56:01 +0800] "GET /php-7.3.0.tar.bz2 HTTP/1.0" 503 197 "-" "ApacheBench/2.3"
127.0.0.1 - - [01/Nov/2019:22:56:01 +0800] "GET /php-7.3.0.tar.bz2 HTTP/1.0" 200 14786886 "-" "ApacheBench/2.3"
127.0.0.1 - - [01/Nov/2019:22:56:01 +0800] "GET /php-7.3.0.tar.bz2 HTTP/1.0" 200 14786886 "-" "ApacheBench/2.3"
说明: 首先用 limit_conn_zone 定义了一个内存区块索引 alexis,大小为 10m,它以 $binary_remote_addr 作为 key。
$binary_remote_addr 就是远程连接的 ip,以每个 ip 作为对象。
该配置只能在 http 里面配置,不支持在 server 里配置。
limit_conn 定义限制并发连接数,针对 alexis 这个 zone,并发连接为 10 个。在这需要注意一下,这个 10 指的是单个 IP 的并发最多为 10 个。
2. 速度限制
location ~ /download/ {
...
limit_rate_after 512k;
limit_rate 150k;
...
}
说明: limit_rate_after 定义当一个文件下载到指定大小(本例中为512k)之后开始限速;
limit_rate 定义下载速度为 150k/s。
注意:这两个参数针对每个请求限速。
该模块主要用来限制请求数。
1、 limit_req_zone
语法: limit_req_zone $variable zone=name:size rate=rate;
默认值: none
配置段: http
设置一块共享内存限制域用来保存键值的状态参数。 特别是保存了当前超出请求的数量。
键的值就是指定的变量(空值不会被计算)。
如 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
说明:区域名称为one,大小为10m,平均处理的请求频率不能超过每秒一次,键值是客户端IP。
使用$binary_remote_addr变量, 可以将每条状态记录的大小减少到64个字节,这样1M的内存可以保存大约1万6千个64字节的记录。
如果限制域的存储空间耗尽了,对于后续所有请求,服务器都会返回 503 (Service Temporarily Unavailable)错误。
速度可以设置为每秒处理请求数和每分钟处理请求数,其值必须是整数,
所以如果你需要指定每秒处理少于1个的请求,2秒处理一个请求,可以使用 “30r/m”。
2、limit_req
语法: limit_req zone=name [burst=number] [nodelay];
默认值: —
配置段: http, server, location
设置对应的共享内存限制域和允许被处理的最大请求数阈值。
如果请求的频率超过了限制域配置的值,请求处理会被延迟,所以所有的请求都是以定义的频率被处理的。
超过频率限制的请求会被延迟,直到被延迟的请求数超过了定义的阈值,
这时,这个请求会被终止,并返回 503 (Service Temporarily Unavailable) 错误。
这个阈值的默认值为 0。
如:
limit_req_zone $binary_remote_addr zone=alexis:10m rate=1r/s;
server {
location /upload/ {
limit_req zonealexis=aming burst=5;
}
}
限制平均每秒不超过一个请求,同时允许超过频率限制的请求数不多于5个。
如果不希望超过的请求被延迟,可以用nodelay参数,如:
limit_req zone=alexis burst=5 nodelay;
示例
http {
limit_req_zone $binary_remote_addr zone=alexis:10m rate=1r/s;
server {
location ^~ /download/ {
limit_req zone=alexis burst=5;
}
}
}
设定白名单IP
如果是针对公司内部IP或者lo(127.0.0.1)不进行限速,如何做呢?这就要用到geo模块了。
假如,预把127.0.0.1和192.168.100.0/24网段设置为白名单,需要这样做。
在http { }里面增加:
geo $limited {
default 1;
127.0.0.1/32 0;
192.168.100.0/24 0;
}
map $limited $limit {
1 $binary_remote_addr;
0 "";
}
原来的 “limit_req_zone $binary_remote_addr ” 改为“limit_req_zone $limit”
完整示例:
http {
geo $limited {
default 1;
127.0.0.1/32 0;
192.168.100.0/24 0;
}
map $limited $limit {
1 $binary_remote_addr;
0 "";
}
limit_req_zone $limit zone=alexis:10m rate=1r/s;
server {
location ^~ /download/ {
limit_req zone=alexis burst=5;
}
}
}