Openresty+Lua+Memcached反爬虫策略
2017-02-07 14:27
507 查看
http://www.07net01.com/2015/04/822090.html
直接用Openresty替换掉了Nginx,通过Nginx内嵌Lua配合一个Memcached实现一个不依赖后端反爬虫验证(类似于CloudFlare的验证码。Memcached中包含键值
然后写个python cron分析,比如10分钟内请求页面数超过100的用户,然后插入Memcached好了...
原文地址:Openresty+Lua+Memcached反爬虫策略, 感谢原作者分享。关键词:
直接用Openresty替换掉了Nginx,通过Nginx内嵌Lua配合一个Memcached实现一个不依赖后端反爬虫验证(类似于CloudFlare的验证码。Memcached中包含键值
identify_IP的用户都会被重定向到identify.php进行处理,可以在identify.php通过验证码或者js进行human验证,验证之后将
identify_IP删除,该
IP即可继续访问。
server { #... location / { index index.php; } location ~ \.php$ { content_by_lua ' uri = ngx.var.uri if uri == "/identify.php" then ngx.exec("@bypass") return end clientIP = ngx.var.remote_addr local memcached = require "resty.memcached" local memc, err = memcached:new() if not memc then ngx.say("failed to instantiate memc: ", err) return end local ok, err = memc:connect("127.0.0.1", 11211) if not ok then ngx.say("failed to connect: ", err) return end local res, flags, err = memc:get("identify_"..clientIP) if err then ngx.exec("@bypass") return end if res == "1" then ngx.exec("@identify") return end ngx.exec("@bypass") '; } location @bypass { #echo 'bypass'; #rewrite break fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location @identify { #echo 'identify'; #identify.php rewrite ^/(.*)$ /identify.php?url=$request_uri redirect; #redirect } location ~ /\.ht { deny all; } }
identify_IP键值可通过分析Nginx日志自动set,通过AWK筛选出10分钟的访问日志。
tac chd_access.log | awk 'BEGIN{ "date -d \"-10 minute\" +\"%H:%M:%S\"" | getline min5 } { if (substr($4, 14) > min5) print; else exit;}' | tac
然后写个python cron分析,比如10分钟内请求页面数超过100的用户,然后插入Memcached好了...
原文地址:Openresty+Lua+Memcached反爬虫策略, 感谢原作者分享。关键词:
相关文章推荐
- openresty luarocks 安装以及openssl 问题处理
- Openresty/Lua + Thrift + HBase
- 安装Nginx+Lua+OpenResty开发环境配置全过程实例
- 安装Nginx+Lua+OpenResty开发环境配置全过程实例
- Lapis - A web framework for Lua or MoonScript powered by OpenResty
- 安装openresty+redis+lua
- openresty lua zlib整合安装 让lua支持解压服务端压缩过的数据
- lua + openresty web API 框架
- Building openresty1.7.10.1 with luajit on windows using Cygwin, 在windows下用Cygwin下编译带Luajit的openresty
- Tengine 扩展 Openresty lua-nginx-module 模块
- openresty php luajit
- openresty + lua-resty-weedfs + weedfs + graphicsmagick动态生成缩略图(类似淘宝方案)
- 利用 openresty balancer_by_lua 实现一个简单的 ip_hash upstream 调度
- OpenResty+lua+GraphicsMagick生成缩略图
- openresty lua zlib整合安装 让lua支持解压服务端压缩过的数据
- 练手项目:用openresty + lua + jqurey 制作一个随机发牌网页
- openresty+lua在反向代理服务中的玩法 | WooYun知识库
- 安装Nginx+Lua+OpenResty开发环境配置全过程实例
- LNMP架构,phpf服务器和memcached缓存配置,openresty+memcached实现缓存