全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 8040|回复: 36
打印 上一主题 下一主题

在阿军的LNMP上添加防CC,只能防轻量CC

[复制链接]
跳转到指定楼层
1#
发表于 2013-5-4 14:15:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 lazyzhu 于 2013-5-4 19:23 编辑

由我blog文章改编而来
http://lazyzhu.com/Nginx/LNMP-compile-nginx-redis-module-to-build-redis-cache.html
这是最简单最没技术含量的,只要简单加两行配置就行

其实可以还有更强大的功能:
将CC攻击的IP自动添加进机房路由器防火墙(有条件的话)
将CC攻击的IP自动添加进iptables防火墙
将CC攻击的IP封禁自定义时间,过后自动释放
将被CC攻击的页面进行缓存,降低后端资源
将CC攻击的IP转向自定义网页,或随机网页
......


1. 停止LNMP服务
/root/lnmp stop
/etc/init.d/nginx stop
/etc/init.d/mysql stop
/etc/init.d/redis_6379 stop
/usr/local/php/sbin/php-fpm stop
killall nginx mysqld redis php-cgi



2. 备份LNMP配置
mkdir -p ~/src && cd ~/src
\cp -rf /usr/local/nginx/conf ~/src/conf.bak
\cp -rf /etc/init.d/nginx ~/src/nginx.bak


3.重编译nginx + limit_req2_nginx_module

wget http://zlib.net/zlib-1.2.8.tar.gz -O -|tar xz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz -O -|tar xz
wget http://www.openssl.org/source/openssl-1.0.1e.tar.gz -O -|tar xz
wget http://nginx.org/download/nginx-1.2.8.tar.gz -O -|tar xz
git clone https://github.com/monadbobo/limit_req2_nginx_module

rm -rf /usr/local/nginx
cd nginx-1.2.8/
./configure  --prefix=/usr/local/nginx \
--user=www --group=www \
--with-zlib=../zlib-1.2.8 \
--with-pcre=../pcre-8.32 \
--with-openssl=../openssl-1.0.1e \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-ipv6 \
--add-module=../limit_req2_nginx_module \
--with-cc-opt='-O3'
make && make install

cd ../
chkconfig nginx on
rm -rf /usr/local/nginx/conf
\cp -rf ~/src/conf.bak /usr/local/nginx/conf
\cp -rf /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak


4. 修改Nginx配置

log_format  lazyzhu.com  '$remote_addr - $remote_user [$time_local] $request '
             '$status $body_bytes_sent $http_referer '
             '$http_user_agent $http_x_forwarded_for';

limit_req2_zone $request_uri zone=lazyzhu.com.one:2m rate=5r/s;
limit_req2_zone $binary_remote_addr zone=lazyzhu.com.two:2m rate=30r/s;


server {
    listen       80;
    server_name  lazyzhu.com;
    root  /home/wwwroot/lazyzhu.com;
    location / {
        index index.html index.htm index.php default.html default.htm default.php;
    }
    include none.conf;
    ## folder disable limit
    location ^~ /(wp-admin|admin)/ {
        limit_req2  off;
        try_files     $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include       fcgi.conf;
    }
    ## file disable limit
    location ~ .*(admin|p)\.(php|php5)?$ {
        limit_req2  off;
        try_files     $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include       fcgi.conf;
    }

    location ~ .*\.(php|php5)?$ {
        limit_req2 zone=lazyzhu.com.one forbid_action=@444 nodelay;
        limit_req2 zone=lazyzhu.com.two forbid_action=@444 nodelay;
        try_files     $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include       fcgi.conf;
    }
    location @444 {
        return 444;
    }

    location ~ .*\.(js|css)?$ {
        expires  12h;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires  30d;
    }
    location /status {
        stub_status  on;
        access_log  off;
    }
    access_log  /home/wwwlogs/lazyzhu.com.log  lazyzhu.com;
}


5. 启动LNMP服务
/etc/init.d/nginx start
/etc/init.d/mysql start
/etc/init.d/php-fpm start

2#
发表于 2013-5-4 14:16:22 | 只看该作者
沙发。。
3#
发表于 2013-5-4 14:17:55 | 只看该作者
板凳。。
4#
发表于 2013-5-4 14:19:09 来自手机 | 只看该作者
└(^o^)┘
5#
发表于 2013-5-4 14:21:19 | 只看该作者
哎有人CC ing
6#
发表于 2013-5-4 14:53:18 | 只看该作者
支持下技术文章
7#
发表于 2013-5-4 14:54:48 | 只看该作者
支持下技术文章
8#
发表于 2013-5-4 15:05:43 | 只看该作者
cc来了
9#
发表于 2013-5-4 15:06:31 | 只看该作者
尼玛,我提出的 return 444 我删除了文章又变成你的了。
10#
发表于 2013-5-4 15:07:08 | 只看该作者
支持
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-9-25 00:10 , Processed in 0.222021 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表