全球主机交流论坛

标题: 在阿军的LNMP上添加防CC,只能防轻量CC [打印本页]

作者: lazyzhu    时间: 2013-5-4 14:15
标题: 在阿军的LNMP上添加防CC,只能防轻量CC
本帖最后由 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


作者: vpswz    时间: 2013-5-4 14:16
沙发。。
作者: 过客    时间: 2013-5-4 14:17
板凳。。
作者: junhan    时间: 2013-5-4 14:19
└(^o^)┘
作者: ybs885    时间: 2013-5-4 14:21
哎有人CC ing
作者: imnpc    时间: 2013-5-4 14:53
支持下技术文章
作者: imnpc    时间: 2013-5-4 14:54
支持下技术文章
作者: lovettww    时间: 2013-5-4 15:05
cc来了
作者: skwinx    时间: 2013-5-4 15:06
尼玛,我提出的 return 444 我删除了文章又变成你的了。
作者: light    时间: 2013-5-4 15:07
支持
作者: lazyzhu    时间: 2013-5-4 15:21
skwinx 发表于 2013-5-4 15:06
尼玛,我提出的 return 444 我删除了文章又变成你的了。


见过不要脸的,没见过像你这样不要脸的
http://loc.201812.xyz/forum.php ... 089&pid=2607521
从nginx加入444一直在用了
作者: lazyzhu    时间: 2013-5-4 15:35
skwinx 发表于 2013-5-4 15:06
尼玛,我提出的 return 444 我删除了文章又变成你的了。

你不会不代表别人不会
http://loc.201812.xyz/forum.php ... 163&pid=2055233
http://loc.201812.xyz/forum.php ... 287&pid=2610814
作者: skwinx    时间: 2013-5-4 15:47
关键是我没看到谁把我的CC掉,这就是事实。你的倒是被C死了。
作者: james2098    时间: 2013-5-4 15:48
技术盲路过顶起来
作者: cquyf    时间: 2013-5-4 16:13
支持技术贴
作者: 来自未来的家伙    时间: 2013-5-4 16:26
技术高潮!
作者: klocal    时间: 2013-5-4 16:27
我一直很低调的做站 也不去得罪别人,而且我用的是空间,也没人会吃饱了 来CC 我 虽然用不上 先收藏
作者: catding    时间: 2013-5-4 16:32
收藏了。
作者: xfspace    时间: 2013-5-4 16:39
现在动不动都DDOS。。。
作者: wormcy    时间: 2013-5-4 16:45
给力
作者: 每次醒来    时间: 2013-5-4 16:45
本帖最后由 每次醒来 于 2013-5-4 17:39 编辑

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-cc-opt='-O3' --add-module=../limit_req2_nginx_module

这样对吗?  绿色我以前编译的 红色新添加.....
作者: lazyzhu    时间: 2013-5-4 16:47
每次醒来 发表于 2013-5-4 16:45
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-h ...

一行的话,去掉最后的反斜杠
作者: flydon    时间: 2013-5-4 17:08
mark
作者: 用户名    时间: 2013-5-4 17:24
MARK

作者: 醉卧网络君莫笑    时间: 2013-5-4 17:54
防轻量CC,多少算轻量?每分钟10000次能防吗?
作者: 单手摘月    时间: 2013-5-4 17:59
提示: 作者被禁止或删除 内容自动屏蔽
作者: lazyzhu    时间: 2013-5-4 19:07
单手摘月 发表于 2013-5-4 17:59
一直没懂这个干嘛用的

http://wiki.nginx.org/HttpCoreModule#try_files
作者: xiasl    时间: 2013-5-4 19:19
这个防范工具☭小流量攻击还行。
作者: lazyzhu    时间: 2013-5-4 19:29
xiasl 发表于 2013-5-4 19:19
这个防范工具☭小流量攻击还行。

是的,很局限的
有些CC攻击流量达到G口的话,没硬防不行的
只有没见过世面的人,才以为这样就无敌了

作者: 店小二    时间: 2013-5-4 19:38
灰大婶 能抗多少并发
作者: Zeraba    时间: 2013-5-4 19:42
唔 我喜欢这个 http://loc.201812.xyz/thread-154832-1-1.html 反正都是小流量CC了 出来MJJ页面会很有意思
作者: lazyzhu    时间: 2013-5-4 19:56
店小二 发表于 2013-5-4 19:38
灰大婶 能抗多少并发

咩咩叫我不要告诉你,
作者: mlmm    时间: 2013-5-5 01:48
提示: 作者被禁止或删除 内容自动屏蔽
作者: sdgl212    时间: 2013-5-5 11:34

支持
作者: cquyf    时间: 2013-5-25 11:19
记录下
作者: 气味    时间: 2013-5-25 12:23
llsmp.org 这家的包不太给力啊
作者: hzqim    时间: 2013-5-25 15:27
技术帝。




欢迎光临 全球主机交流论坛 (https://loc.201812.xyz/) Powered by Discuz! X3.4