linux安全篇:禁止频繁访问的ip访问nginx

admin4个月前笔记64

image.png实验环境:
版本:redhat6.5
ip:172.16.1.100,172.16.10
软件:nginx

172.16.1.10部署nginx
[root@localhost tools]# ls
nginx-1.11.2.tar.gz
[root@localhost tools]# yum  install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel
[root@localhost tools]# tar xf nginx-1.11.2.tar.gz 
[root@localhost tools]# ls
nginx-1.11.2  nginx-1.11.2.tar.gz
[root@localhost tools]# cd nginx-1.11.2
[root@localhost nginx-1.11.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.11.2]# ./configure
[root@localhost nginx-1.11.2]# make
[root@localhost nginx-1.11.2]# make install
测试nginx服务
[root@localhost ~]# curl -I 172.16.1.100
HTTP/1.1 200 OK
Server: nginx/1.11.2
Date: Mon, 17 Aug 2020 09:36:29 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Mon, 17 Aug 2020 09:36:19 GMT
Connection: keep-alive
ETag: "5f3a4f93-f"
Accept-Ranges: bytes

nginx 可以正常访问。
接下来,假设172.16.1.100是黑客主机,频繁访问nginx服务

模拟172.16.1.100访问10次172.16.1.10

172.16.1.100

[root@localhost ~]# ab -c 1 -n 10 http://172.16.1.10/
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 172.16.1.10 (be patient).....done


Server Software:        nginx/1.11.2
Server Hostname:        172.16.1.10
Server Port:            80

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      1
Time taken for tests:   0.016 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      8450 bytes
HTML transferred:       6120 bytes
Requests per second:    617.02 [#/sec] (mean)
Time per request:       1.621 [ms] (mean)
Time per request:       1.621 [ms] (mean, across all concurrent requests)
Transfer rate:          509.16 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.3      0       1
Processing:     1    1   0.3      1       2
Waiting:        0    1   0.3      1       1
Total:          1    1   0.5      1       2
ERROR: The median and mean for the initial connection time are more than twice the standard
       deviation apart. These results are NOT reliable.

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      2
  90%      2
  95%      2
  98%      2
  99%      2
 100%      2 (longest request)
查看nginx日志

172.16.1.10

[root@localhost ~]# tail /usr/local/nginx/logs/access.log 
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"

由此可见,一秒钟之内172.16.1.100访问了nginx10次,接下来禁止掉这个问题ip

通过iptables限制ip访问

172.16.1.10

[root@localhost ~]# iptables -I INPUT -s 172.16.1.100 -ptcp --dport 80 -j DROP

172.16.1.100

[root@localhost ~]# curl 172.16.1.10
curl: (7) Failed connect to 172.16.1.10:80; 连接超时

此时172.16.1.100再也不能访问nginx

nginx配置文件限制

172.16.1.10
image.png172.16.1.100

[root@localhost ~]# curl -I 172.16.1.10
HTTP/1.1 403 Forbidden
Server: nginx/1.11.2
Date: Sat, 25 Jul 2020 23:12:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
总结

以上就是两种简单的方法限制ip访问,还有许多方法可以利用工具进行ip限制。


相关文章

VMware中三种网络连接的区别

VMware中三种网络连接的区别

1、概述大家在安装完虚拟机后,默认安装了如下图的两块虚拟网卡——VMnet1和VMnet8,其中VMnet1是host网卡,用于host方式连接网络;VMnet8是NAT网卡,用于NAT方式连接网络的...

详解Redis内部运作机制

Redis数据库(Redis 如何表示一个数据库,数据库操作是如何实现的)当Redis服务器初始化的时候会创建 redis.h/REDIS_DEFAULT_DBNUM(后面简写 N ) 个数...

Nginx+Tomcat负载均衡配置

JAVA JDK安装#下载相应的jdk软件包,然后解压安装,我这里包名称为:jdk-7u25-linux-x64.tar.gztar -xzf  jdk-7u25-lin...

开发必备技术--docker(使用篇)

开发必备技术--docker(使用篇)

前言续接上一篇博文: 开发必备技术--docker(一) 这也是开学了,假期的最后一篇博文,后面的一些文章可能就是以图片,pdf文档的形式了,尤其后面设计到数学,算法类型的博文都是这种形式的,当然无所...

OAuth 2.0 的一个简单解释,一看就懂

OAuth 2.0 的一个简单解释,一看就懂

OAuth 2.0OAuth 2.0 是目前最流行的授权机制,用来授权第三方应用,获取用户数据。这个标准比较抽象,使用了很多术语,初学者不容易理解。其实说起来并不复杂,下面我就通过一个简单的类比,帮助...

聊聊服务器优化

聊聊服务器优化

我们开发的软件服务需要在服务器上运行,所以服务器性能代表了软件的性能上限,因此服务器性能调优是个十分重要的环节,然而大部分同学对服务器性能调优关注的较少,今天从3个部分对服务器性能调优进行介绍,分别是...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。