nginx针对静态页面防CC攻击
nginx 针对静态页面防CC 攻击一、架构Nginx 介绍Nginx 是一款由俄罗斯程序设计师Igor Sysoev所开发轻量级的网页服务器、反向代理服务器以及电子邮件(IMAP/POP3)代理服务
nginx 针对静态页面防CC 攻击
一、架构
Nginx 介绍
Nginx 是一款由俄罗斯程序设计师Igor Sysoev所开发轻量级的网页服务器、反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。
Nginx 的特点
Nginx 相较于Apache 、lighttpd 具有占有内存少,稳定性高等优势,并且依靠并发能力强,丰富的模块库以及友好灵活的配置而闻名。在Linux 操作系统下,nginx 使用epoll 事件模型,得益于此,nginx 在Linux 操作系统下效率相当高。
Nginx 的反向代理功能
nginx proxy 是nginx 的重要功能,利用proxy 基本可以实现一个完整的7层负载均衡,它有这些特色:
1、功能强大,性能超强,工作稳定。
2、后端转向与业务配置分离,非常灵活。
3、可以指定任意IP 和端口进行配置,与网络环境是不相干的。
4、可以针对后端返回的情况判断,不正常则重新请求另一台主机,并自动剔除不正常的主机。
5、可以分配权重,并且分配均匀。
6、可以实现多种分配策略。
7、上传文件使用异步处理方式,nginx 会先将文件接收下来,然后再转发到后端,这样可以减少后端服务器很多连接。
根据Nginx 的特点与反向代理的功能,使之成为静态页面防CC 攻击绝佳选择。根据我们以往的经验,一台Q9300、4GB 内存的主机,可以轻松防住7万链接的CC 攻击,此时服务器资源占用仍然相当低。
Nginx 防CC 攻击的架构:
,
二、服务器配置
服务器环境:CentOS 6.3 X64
(注:以下文档,蓝色为配置文件的内容,红色为运行的命令)
1、安装系统、编译环境等
最小化安装CentOS 。因为安装后我们要升级系统补丁,再安装必要组件,所以没必要安装其他组件。
安装完后,配置iptables ,打开80端口,或者直接关闭iptables 。
用 yum update -y 升级系统的组件
安装编译Nginx 必要的组件:
yum install -y gcc make sendmail pcre pcre-devel openssl openssl-devel nano screen lrzsz wget curl curl-devel sendmail mlocate openssh-clients man patch
添加www 用户组和用户,并且指定www 用户的shell 为/sbin/nologin
groupadd www && useradd -g www -s /sbin/nologin www
创建相关的目录
mkdir -p /var/log/nginx /var/proxy_temp_dir /var/proxy_cache_dir
#touch /var/log/nginx/test.com.log
2、编译nginx
下载nginx
cd ~
wget
tar zxf nginx-1.2.7.tar.gz
下载ngx_cache_purge
wget --no-check-certificate
"https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip" -O "ngx_cache_purge-master.zip"
unzip -x ngx_cache_purge-master.zip
下载nginx-upstream-jvm-route
wget
"http://nginx-upstream-jvm-route.googlecode.com/files/nginx-upstream-jvm-route-0.1.tar.gz" -O "nginx-upstream-jvm-route-0.1.tar.gz"
tar zxf nginx-upstream-jvm-route-0.1.tar.gz
进入nginx 源代码目录
cd nginx-1.2.7
打补丁
patch -p0 < ../nginx_upstream_jvm_route/jvm_route.patch
,编译
./configure --user=www --group=www --add-module=../nginx_upstream_jvm_route --add-module=../ngx_cache_purge-master --prefix=/usr/local/nginx
--with-http_stub_status_module --with-http_ssl_module
make && make install
3、配置nginx
进入nginx 的配置目录
cd /usr/local/nginx/conf/
vi nginx.conf
nginx.conf 的配置
user www www;
worker_processes 8;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
log_format access_log '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
,fastcgi_temp_file_write_size 128k;
##cache_start##
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /var/proxy_temp_dir;
#设置Web 缓存区名称为cache_one,内存缓存空间大小为200MB ,1天清理一次缓存,硬盘缓存空间大小为1GB 。
proxy_cache_path /var/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g;
##cache_end##
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
include upstream.conf;
include vhostconf/*.conf;
}
vi upstream.conf
(增加后端服务器时,需要在此添加后端服务器。添加或修改的主要内容是上游服务器的访问地址,以及指定upstream 名称。)
upstream www.test.com {
server 192.168.1.102:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.103:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.105:80 weight=1 max_fails=2 fail_timeout=30s;
jvm_route $cookie_JSESSIONID reverse;
}
创建vhostconf 目录
mkdir vhostconf
,配置相应的主机
(增加后端服务器时,需要在此添加后端服务器。主要修改的有绑定域名、proxy_pass对应upstream.conf 的地址)
vi vhostconf/test.com.conf
server {
listen 80;
server_name www.test.com;
location / {
#root /var/www/test.com;
#index index.jsp index.htm index.html;
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream 负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP 状态码设置不同的缓存时间
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
#以域名、URI 、参数组合成Web 缓存的Key 值,Nginx 根据Key 值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www.test.com;
expires 1d;
}
#用于清除缓存,假设一个URL 为http://192.168.8.42/test.txt,通过访问
location ~ /purge(/.*)
{
#设置只允许指定的IP 或IP 段才可以清除URL 缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#扩展名以.php 、.jsp 、.cgi 结尾的动态应用程序不缓存。
,location ~ .*.(php|jsp|cgi|asp|aspx)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://www.test.com;
}
location /nginx_status {
stub_status on;
access_log off;
allow 183.63.215.39;
deny all;
}
access_log /var/log/nginx/test.com.log access_log;
}
测试配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4、配置nginx 自启动
创建nginx 自启动脚本
vi /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
,# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid }
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
,stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1
esac
exit $RETVAL
加上x 权限
chmod x /etc/init.d/nginx
加上自启动
echo /etc/init.d/nginx start >> /etc/rc.local
启动nginx
/etc/init.d/nginx start
,三、优化
1、避免后端服务器出现大文件。通常CC 攻击会连接服务器的大文件,企图用尽服务器的带宽资源,因此,后端服务器不要放置大文件,对于大的图片,应对图片进行切片,切成多块以减小体积。
2、后端服务器尽量单独开一个非80端口(比如24392)给前端服务器,如果后端有硬件防火墙,要做直通。
3、如果后端服务器负荷仍然大,可以通过修改缓存时间等,提高命中率。
4、可以配置nginx 的防攻击模块,对于连接过于频繁的client 进行屏蔽。
5、可以对前端服务器进行双网卡捆绑,提高服务器带宽。
6、可以多台前端机器,采用keepalived 负载均衡