经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 其他 » 网络安全 » 查看文章
记一次博客被群压的经历
来源:cnblogs  作者:小柒2012  时间:2019/2/18 9:29:16  对本文有异议

前言

前段时间,博客和论坛都放到的阿里云新购的三年 T5 实例服务器上,等都转移过去才发现,所谓的 T5 实例只能满足10% 的 CPU 峰值。期间经历了各种卡顿、死机,最终又把博客单独迁移了回来。静态文件走 CDN,文章都 Redis,以为万事大吉了就。

群压

然并卵,有一天,群里有网友说要压测我的论坛,我说那肯定一压一个死,有本事来压我的博客啊,顺手便扔了博客网址,并@了全体人员。

然后,网友齐上阵,十八般武艺都拿出来了,有AB压测的,有使用 jmeter 测试的,更有甚者自己使用 Python、Java 写代码替我压测的,结果就是系统 CPU 爆表,访问博客陷入了漫长的等待。

分析

先说一下博客架构: Nginx + PHP-fpm + CND + Redis + RDS,静态文件走CDN,命中率基本在百分之八九十左右,动态请求走Nginx,然后交给 php-fpm 处理,博客文章进行了缓存处理,查询基本不会走数据库。

这里总结下原因,在网友压测的时候,登录系统,TOP 了一下,发现 PHP-fpm 进程 CPU 占比居高不下,毕竟1C1G的机器配置,相比于Nginx处理静态页面的能力,PHP-fpm 还是太弱鸡了,无论怎么优化,配置总会是瓶颈。

这里说明一下网友的压测,也就算是简单的流量攻击,其实就是模拟多个用户不停的进行访问(访问那些需要大量数据操作,就是需要大量CPU时间的页面),从而把服务压垮。

应对

其实对于压测这种场景,我们使用 OpenResty + Lua 限流就可以轻松解决。

编写 imit_req.lua 脚本:

  1. -- 平滑限制接口请求数
  2. local limit_req = require "resty.limit.req"
  3. -- 这里我们使用AB测试,-n访问1000次, -c并发100
  4. -- ab -n 1000 -c 100 http://121.142.155.213/
  5. -- 限制 ip 每秒只能调用 200 接口 burst设置为 0 则平滑限流
  6. local lim, err = limit_req.new("my_limit_req_store", 10, 0)
  7. if not lim then
  8. ngx.log(ngx.ERR,
  9. "failed to instantiate a resty.limit.req object: ", err)
  10. return ngx.exit(500)
  11. end
  12. -- IP维度的限流
  13. local key = ngx.var.binary_remote_addr
  14. local delay, err = lim:incoming(key, true)
  15. if not delay then
  16. if err == "rejected" then
  17. return ngx.exit(503)
  18. end
  19. ngx.log(ngx.ERR, "failed to limit req: ", err)
  20. return ngx.exit(500)
  21. end
  22. if delay >= 0.001 then
  23. -- the 2nd return value holds the number of excess requests
  24. -- per second for the specified key. for example, number 31
  25. -- means the current request rate is at 231 req/sec for the
  26. -- specified key.
  27. local excess = err
  28. -- the request exceeding the 200 req/sec but below 300 req/sec,
  29. -- so we intentionally delay it here a bit to conform to the
  30. -- 200 req/sec rate.
  31. ngx.sleep(delay) -- 延时处理
  32. end

导入 nginx.conf 配置:

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. sendfile on;
  5. keepalive_timeout 65;
  6. lua_shared_dict my_limit_req_store 100m;
  7. lua_shared_dict my_limit_conn_store 100m;
  8. lua_shared_dict my_limit_count_store 100m;
  9. server{
  10. listen 80;
  11. server_name blog.52itstyle.com;
  12. index index.php;
  13. root /mnt/domains/blog.52itstyle.com;
  14. location = /500.html {
  15. root /usr/local/openresty/nginx/html;
  16. }
  17. error_page 500 502 503 504 = /503/503.html;
  18. location ~ \.php$ {
  19. # 导入 lua 限流 配置
  20. access_by_lua_file /usr/local/openresty/nginx/lua/limit_req.lua;
  21. fastcgi_pass 127.0.0.1:9000;
  22. include fastcgi_params;
  23. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  24. }
  25. location ~ /\.ht {
  26. deny all;
  27. }
  28. }

划重点,脚本中:

  1. # 每秒访问超过2次就拒绝服务,跳转到503错误页面
  2. limit_req.new("my_limit_req_store", 2, 0)

总结

我经常听卖锁具的人说:“再好的锁,也只防好人,不防坏人!”,同样适用于网络,网友也只是娱乐一下而已,如果真有坏人想搞你,有无数种办法把你搞死死。

原文链接:http://www.cnblogs.com/smallSevens/p/10385968.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号