OpenResty + Lua + Rdies 实现动态封禁 I

2020-02-27  本文已影响0人  Demons_LLL

一、准备工作

  1. 下载openresty -> http://openresty.org/cn/download.html
  2. 下载redis(这里测试-用单机redis)->http://www.redis.cn/download.html
    image.png

二、下载openresty

1. 下载
wget https://openresty.org/download/openresty-1.15.8.2.tar.gz
2. 解压
tar -zxf openresty-1.15.8.2.tar.gz
image.png

三、安装redis
这个就不说了,之前有教程 - > https://www.jianshu.com/p/a4b4d23725e6

四、安装openresty

提示:安装前的准备:
推荐您使用yum安装以下的开发库:
yum install pcre-devel openssl-devel gcc curl
具体其他系统见官网 - > [http://openresty.org/cn/installation.html](http://openresty.org/cn/installation.html)
cd openresty-1.15.8.2
./configure
make
sudo make install

五、 启动

 cd /usr/local/openresty/nginx/sbin/
 ./nginx 
----
查看进程
[root@localhost sbin]# ps aux|grep nginx 
root     23731  0.0  0.2   9544  2728 ?        Ss   Feb27   0:00 nginx: master process ./nginx
nobody   31772  0.0  0.2   9676  2576 ?        S    00:00   0:00 nginx: worker process
root     31978  0.0  0.0   4356   752 pts/1    S+   00:18   0:00 grep nginx
----
浏览器访问:
[http://192.168.199.124/](http://192.168.199.124/)
提示:ECS / 虚拟机 自己关闭防火墙.
image.png

六、 增加Lua + Redis

  1. nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    
    # lua库的地址(比如:redis) redis.lua 下载地址
   # wget -c https://github.com/openresty/lua-resty-redis/raw/master/lib/resty/redis.lua
    lua_package_path "/usr/local/openresty/lualib/redis/redis.lua;;";

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
    # nginx自带的反爬策略文件位置
        include /usr/local/openresty/ext/dos_cc.conf;

        location / {
            root   html;
            index  index.html index.htm;
            lua_code_cache off;
            access_by_lua_file /usr/local/openresty/ext/dos_cc.lua;
        }
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

基本默认配置.

dos_cc.lua -> 可参考 https://gist.github.com/Ceelog/39862d297d9c85e743b3b5111b7d44cb

配置完 重启nginx

./nginx -s reload
nginx:[warn] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/nginx.cof:87
注意:重启nginx会有警告不用管它
修改完成,这样就可以不需要重启nginx情况下实时访问lua程序

试验效果:


image.png
image.png

七、总结
以上,便是 OpenResty+Lua+Redis 实现的 IP 黑名单功能,具有如下优点:

1、配置简单、轻量,几乎对服务器性能不产生影响;
2、多台服务器可以通过Redis实例共享黑名单;
3、动态配置,可以手工或者通过某种自动化的方式设置 Redis 中的黑名单。

上一篇下一篇

猜你喜欢

热点阅读