Nginx服务安全加固

谁借莪1个温暖的怀抱¢ 2022-10-23 03:00 291阅读 0赞

1、禁止频繁访问的ip访问nginx

生产环境中经常会遇到某个ip地址频繁异常的访问nginx网站,此时我们需要通过安全措施保护我们的服务器
部署nginx

  1. [root@localhost tools]# yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel
  2. [root@localhost tools]# tar xf nginx-1.11.2.tar.gz
  3. [root@localhost tools]# ls
  4. nginx-1.11.2 nginx-1.11.2.tar.gz
  5. [root@localhost tools]# cd nginx-1.11.2
  6. [root@localhost nginx-1.11.2]# ls
  7. auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
  8. [root@localhost nginx-1.11.2]# ./configure
  9. [root@localhost nginx-1.11.2]# make
  10. [root@localhost nginx-1.11.2]# make install

测试nginx服务

  1. [root@localhost ~]# curl -I 172.16.1.100
  2. HTTP/1.1 200 OK
  3. Server: nginx/1.11.2
  4. Date: Mon, 17 Aug 2020 09:36:29 GMT
  5. Content-Type: text/html
  6. Content-Length: 15
  7. Last-Modified: Mon, 17 Aug 2020 09:36:19 GMT
  8. Connection: keep-alive
  9. ETag: "5f3a4f93-f"
  10. Accept-Ranges: bytes

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

模拟172.16.1.100访问10次172.16.1.10

172.16.1.100

  1. [root@localhost ~]# ab -c 1 -n 10 http://172.16.1.10/
  2. This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. Benchmarking 172.16.1.10 (be patient).....done
  6. Server Software: nginx/1.11.2
  7. Server Hostname: 172.16.1.10
  8. Server Port: 80
  9. Document Path: /
  10. Document Length: 612 bytes
  11. Concurrency Level: 1
  12. Time taken for tests: 0.016 seconds
  13. Complete requests: 10
  14. Failed requests: 0
  15. Write errors: 0
  16. Total transferred: 8450 bytes
  17. HTML transferred: 6120 bytes
  18. Requests per second: 617.02 [#/sec] (mean)
  19. Time per request: 1.621 [ms] (mean)
  20. Time per request: 1.621 [ms] (mean, across all concurrent requests)
  21. Transfer rate: 509.16 [Kbytes/sec] received
  22. Connection Times (ms)
  23. min mean[+/-sd] median max
  24. Connect: 0 1 0.3 0 1
  25. Processing: 1 1 0.3 1 2
  26. Waiting: 0 1 0.3 1 1
  27. Total: 1 1 0.5 1 2
  28. ERROR: The median and mean for the initial connection time are more than twice the standard
  29. deviation apart. These results are NOT reliable.
  30. Percentage of the requests served within a certain time (ms)
  31. 50% 1
  32. 66% 1
  33. 75% 1
  34. 80% 2
  35. 90% 2
  36. 95% 2
  37. 98% 2
  38. 99% 2
  39. 100% 2 (longest request)

查看nginx日志

172.16.1.10

  1. [root@localhost ~]# tail /usr/local/nginx/logs/access.log
  2. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  3. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  4. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  5. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  6. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  7. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  8. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  9. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  10. 172.16.1.100 - - [26/Jul/2020:05:58:24 +0800] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3"
  11. 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

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

172.16.1.100

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

此时172.16.1.100再也不能访问nginx

nginx配置文件限制

172.16.1.10
在这里插入图片描述
172.16.1.100

  1. [root@localhost ~]# curl -I 172.16.1.10
  2. HTTP/1.1 403 Forbidden
  3. Server: nginx/1.11.2
  4. Date: Sat, 25 Jul 2020 23:12:06 GMT
  5. Content-Type: text/html
  6. Content-Length: 169
  7. Connection: keep-alive

总结

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

参考链接 :

禁止频繁访问的ip访问nginx :https://www.jianshu.com/p/48a8bbeaf76a

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Linux安全加固手册

    1      身份鉴别 1.1         密码安全策略 操作系统和数据库系统管理用户身份鉴别信息应具有不易被冒用的特点,口令应有复杂度要求并定期更换。 设置

    相关 MySQL安全加固方法分享

    前言: 数据库作为数据存储的载体,在程序开发中承担着至关重要的作用。近些年,随着各种安全事故的发生,数据安全性逐渐得到重视。等保评测或各类系统安全评测中也都有增加数据库安全相

    相关 Oracle数据库安全加固记录

    一个应用系统做等保,需要对数据库进行安全加固,根据流程需要先在测试环境进行测试通过后应用于生产环境,这里简单记录测试过程,审计内容是评测的重要点,但是生产环境也不便于开启,这里

    相关 IIS7安全加固

    限制目录执行权限 在“处理程序映射”中,把“编辑功能权限”中的“脚本”去掉,这样即使上传了木马文件在此目录,也是无法执行的。 删除不必要的脚本映射 在“处理程序映射”